Skip to content

Commit

Permalink
internal/store: Rework label allow list for label metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
lilic committed Nov 20, 2020
1 parent 662d59e commit 0b76e7d
Show file tree
Hide file tree
Showing 45 changed files with 338 additions and 210 deletions.
54 changes: 27 additions & 27 deletions internal/store/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Builder struct {
shard int32
totalShards int
buildStoreFunc ksmtypes.BuildStoreFunc
allowLabels ksmtypes.AllowLabels
allowLabelsList map[string][]string
}

// NewBuilder returns a new builder.
Expand Down Expand Up @@ -138,7 +138,9 @@ func (b *Builder) DefaultGenerateStoreFunc() ksmtypes.BuildStoreFunc {

// WithAllowLabels configures which labels can be returned for metrics
func (b *Builder) WithAllowLabels(labels map[string][]string) {
b.allowLabels = sanitizeAllowLabels(labels)
if len(labels) > 0 {
b.allowLabelsList = labels
}
}

// Build initializes and registers all enabled stores.
Expand Down Expand Up @@ -214,31 +216,31 @@ func (b *Builder) buildConfigMapStore() cache.Store {
}

func (b *Builder) buildCronJobStore() cache.Store {
return b.buildStoreFunc(cronJobMetricFamilies, &batchv1beta1.CronJob{}, createCronJobListWatch)
return b.buildStoreFunc(cronJobMetricFamilies(b.allowLabelsList["cronjobs"]), &batchv1beta1.CronJob{}, createCronJobListWatch)
}

func (b *Builder) buildDaemonSetStore() cache.Store {
return b.buildStoreFunc(daemonSetMetricFamilies, &appsv1.DaemonSet{}, createDaemonSetListWatch)
return b.buildStoreFunc(daemonSetMetricFamilies(b.allowLabelsList["daemonsets"]), &appsv1.DaemonSet{}, createDaemonSetListWatch)
}

func (b *Builder) buildDeploymentStore() cache.Store {
return b.buildStoreFunc(deploymentMetricFamilies, &appsv1.Deployment{}, createDeploymentListWatch)
return b.buildStoreFunc(deploymentMetricFamilies(b.allowLabelsList["deployments"]), &appsv1.Deployment{}, createDeploymentListWatch)
}

func (b *Builder) buildEndpointsStore() cache.Store {
return b.buildStoreFunc(endpointMetricFamilies, &v1.Endpoints{}, createEndpointsListWatch)
return b.buildStoreFunc(endpointMetricFamilies(b.allowLabelsList["endpoints"]), &v1.Endpoints{}, createEndpointsListWatch)
}

func (b *Builder) buildHPAStore() cache.Store {
return b.buildStoreFunc(hpaMetricFamilies, &autoscaling.HorizontalPodAutoscaler{}, createHPAListWatch)
return b.buildStoreFunc(hpaMetricFamilies(b.allowLabelsList["horizontalpodautoscalers"]), &autoscaling.HorizontalPodAutoscaler{}, createHPAListWatch)
}

func (b *Builder) buildIngressStore() cache.Store {
return b.buildStoreFunc(ingressMetricFamilies, &networkingv1.Ingress{}, createIngressListWatch)
return b.buildStoreFunc(ingressMetricFamilies(b.allowLabelsList["ingresses"]), &networkingv1.Ingress{}, createIngressListWatch)
}

func (b *Builder) buildJobStore() cache.Store {
return b.buildStoreFunc(jobMetricFamilies, &batchv1.Job{}, createJobListWatch)
return b.buildStoreFunc(jobMetricFamilies(b.allowLabelsList["jobs"]), &batchv1.Job{}, createJobListWatch)
}

func (b *Builder) buildLimitRangeStore() cache.Store {
Expand All @@ -250,31 +252,31 @@ func (b *Builder) buildMutatingWebhookConfigurationStore() cache.Store {
}

func (b *Builder) buildNamespaceStore() cache.Store {
return b.buildStoreFunc(namespaceMetricFamilies, &v1.Namespace{}, createNamespaceListWatch)
return b.buildStoreFunc(namespaceMetricFamilies(b.allowLabelsList["namespaces"]), &v1.Namespace{}, createNamespaceListWatch)
}

func (b *Builder) buildNetworkPolicyStore() cache.Store {
return b.buildStoreFunc(networkpolicyMetricFamilies, &networkingv1.NetworkPolicy{}, createNetworkPolicyListWatch)
return b.buildStoreFunc(networkPolicyMetricFamilies(b.allowLabelsList["networkpolicies"]), &networkingv1.NetworkPolicy{}, createNetworkPolicyListWatch)
}

func (b *Builder) buildNodeStore() cache.Store {
return b.buildStoreFunc(nodeMetricFamilies, &v1.Node{}, createNodeListWatch)
return b.buildStoreFunc(nodeMetricFamilies(b.allowLabelsList["nodes"]), &v1.Node{}, createNodeListWatch)
}

func (b *Builder) buildPersistentVolumeClaimStore() cache.Store {
return b.buildStoreFunc(persistentVolumeClaimMetricFamilies, &v1.PersistentVolumeClaim{}, createPersistentVolumeClaimListWatch)
return b.buildStoreFunc(persistentVolumeClaimMetricFamilies(b.allowLabelsList["persistentvolumeclaims"]), &v1.PersistentVolumeClaim{}, createPersistentVolumeClaimListWatch)
}

func (b *Builder) buildPersistentVolumeStore() cache.Store {
return b.buildStoreFunc(persistentVolumeMetricFamilies, &v1.PersistentVolume{}, createPersistentVolumeListWatch)
return b.buildStoreFunc(persistentVolumeMetricFamilies(b.allowLabelsList["persistentvolumes"]), &v1.PersistentVolume{}, createPersistentVolumeListWatch)
}

func (b *Builder) buildPodDisruptionBudgetStore() cache.Store {
return b.buildStoreFunc(podDisruptionBudgetMetricFamilies, &policy.PodDisruptionBudget{}, createPodDisruptionBudgetListWatch)
}

func (b *Builder) buildReplicaSetStore() cache.Store {
return b.buildStoreFunc(replicaSetMetricFamilies, &appsv1.ReplicaSet{}, createReplicaSetListWatch)
return b.buildStoreFunc(replicaSetMetricFamilies(b.allowLabelsList["replicasets"]), &appsv1.ReplicaSet{}, createReplicaSetListWatch)
}

func (b *Builder) buildReplicationControllerStore() cache.Store {
Expand All @@ -286,27 +288,27 @@ func (b *Builder) buildResourceQuotaStore() cache.Store {
}

func (b *Builder) buildSecretStore() cache.Store {
return b.buildStoreFunc(secretMetricFamilies, &v1.Secret{}, createSecretListWatch)
return b.buildStoreFunc(secretMetricFamilies(b.allowLabelsList["secrets"]), &v1.Secret{}, createSecretListWatch)
}

func (b *Builder) buildServiceStore() cache.Store {
return b.buildStoreFunc(serviceMetricFamilies, &v1.Service{}, createServiceListWatch)
return b.buildStoreFunc(serviceMetricFamilies(b.allowLabelsList["services"]), &v1.Service{}, createServiceListWatch)
}

func (b *Builder) buildStatefulSetStore() cache.Store {
return b.buildStoreFunc(statefulSetMetricFamilies, &appsv1.StatefulSet{}, createStatefulSetListWatch)
return b.buildStoreFunc(statefulSetMetricFamilies(b.allowLabelsList["statefulsets"]), &appsv1.StatefulSet{}, createStatefulSetListWatch)
}

func (b *Builder) buildStorageClassStore() cache.Store {
return b.buildStoreFunc(storageClassMetricFamilies, &storagev1.StorageClass{}, createStorageClassListWatch)
return b.buildStoreFunc(storageClassMetricFamilies(b.allowLabelsList["storageclasses"]), &storagev1.StorageClass{}, createStorageClassListWatch)
}

func (b *Builder) buildPodStore() cache.Store {
return b.buildStoreFunc(podMetricFamilies, &v1.Pod{}, createPodListWatch)
return b.buildStoreFunc(podMetricFamilies(b.allowLabelsList["pods"]), &v1.Pod{}, createPodListWatch)
}

func (b *Builder) buildCsrStore() cache.Store {
return b.buildStoreFunc(csrMetricFamilies, &certv1.CertificateSigningRequest{}, createCSRListWatch)
return b.buildStoreFunc(csrMetricFamilies(b.allowLabelsList["certificatesigningrequests"]), &certv1.CertificateSigningRequest{}, createCSRListWatch)
}

func (b *Builder) buildValidatingWebhookConfigurationStore() cache.Store {
Expand All @@ -318,7 +320,7 @@ func (b *Builder) buildVolumeAttachmentStore() cache.Store {
}

func (b *Builder) buildVPAStore() cache.Store {
return b.buildStoreFunc(vpaMetricFamilies, &vpaautoscaling.VerticalPodAutoscaler{}, createVPAListWatchFunc(b.vpaClient))
return b.buildStoreFunc(vpaMetricFamilies(b.allowLabelsList["verticalpodautoscalers"]), &vpaautoscaling.VerticalPodAutoscaler{}, createVPAListWatchFunc(b.vpaClient))
}

func (b *Builder) buildLeases() cache.Store {
Expand All @@ -330,11 +332,9 @@ func (b *Builder) buildStore(
expectedType interface{},
listWatchFunc func(kubeClient clientset.Interface, ns string) cache.ListerWatcher,
) cache.Store {
filteredMetricFamilies := generator.FilterMetricFamilies(b.allowDenyList, metricFamilies)
filteredMetricFamiliesLabels := generator.FilterMetricFamiliesLabels(b.allowLabels, filteredMetricFamilies)
composedMetricGenFuncs := generator.ComposeMetricGenFuncs(filteredMetricFamiliesLabels)

familyHeaders := generator.ExtractMetricFamilyHeaders(filteredMetricFamiliesLabels)
metricFamilies = generator.FilterMetricFamilies(b.allowDenyList, metricFamilies)
composedMetricGenFuncs := generator.ComposeMetricGenFuncs(metricFamilies)
familyHeaders := generator.ExtractMetricFamilyHeaders(metricFamilies)

store := metricsstore.NewMetricsStore(
familyHeaders,
Expand Down
8 changes: 5 additions & 3 deletions internal/store/certificatesigningrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ var (
descCSRLabelsName = "kube_certificatesigningrequest_labels"
descCSRLabelsHelp = "Kubernetes labels converted to Prometheus labels."
descCSRLabelsDefaultLabels = []string{"certificatesigningrequest"}
)

csrMetricFamilies = []generator.FamilyGenerator{
func csrMetricFamilies(allowLabelsList []string) []generator.FamilyGenerator {
return []generator.FamilyGenerator{
*generator.NewFamilyGenerator(
descCSRLabelsName,
descCSRLabelsHelp,
metric.Gauge,
"",
wrapCSRFunc(func(j *certv1.CertificateSigningRequest) *metric.Family {
labelKeys, labelValues := kubeLabelsToPrometheusLabels(j.Labels)
labelKeys, labelValues := createLabelKeysValues(j.Labels, allowLabelsList)
return &metric.Family{
Metrics: []*metric.Metric{
{
Expand Down Expand Up @@ -103,7 +105,7 @@ var (
}),
),
}
)
}

func wrapCSRFunc(f func(*certv1.CertificateSigningRequest) *metric.Family) func(interface{}) *metric.Family {
return func(obj interface{}) *metric.Family {
Expand Down
16 changes: 8 additions & 8 deletions internal/store/certificatesigningrequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestCsrStore(t *testing.T) {
kube_certificatesigningrequest_created{certificatesigningrequest="certificate-test"} 1.5e+09
kube_certificatesigningrequest_condition{certificatesigningrequest="certificate-test",condition="approved"} 0
kube_certificatesigningrequest_condition{certificatesigningrequest="certificate-test",condition="denied"} 0
kube_certificatesigningrequest_labels{certificatesigningrequest="certificate-test",label_cert="test"} 1
kube_certificatesigningrequest_labels{certificatesigningrequest="certificate-test"} 1
kube_certificatesigningrequest_cert_length{certificatesigningrequest="certificate-test"} 0
`,
MetricNames: []string{"kube_certificatesigningrequest_created", "kube_certificatesigningrequest_condition", "kube_certificatesigningrequest_labels", "kube_certificatesigningrequest_cert_length"},
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestCsrStore(t *testing.T) {
kube_certificatesigningrequest_created{certificatesigningrequest="certificate-test"} 1.5e+09
kube_certificatesigningrequest_condition{certificatesigningrequest="certificate-test",condition="approved"} 0
kube_certificatesigningrequest_condition{certificatesigningrequest="certificate-test",condition="denied"} 1
kube_certificatesigningrequest_labels{certificatesigningrequest="certificate-test",label_cert="test"} 1
kube_certificatesigningrequest_labels{certificatesigningrequest="certificate-test"} 1
kube_certificatesigningrequest_cert_length{certificatesigningrequest="certificate-test"} 0
`,
MetricNames: []string{"kube_certificatesigningrequest_created", "kube_certificatesigningrequest_condition", "kube_certificatesigningrequest_labels", "kube_certificatesigningrequest_cert_length"},
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestCsrStore(t *testing.T) {
kube_certificatesigningrequest_created{certificatesigningrequest="certificate-test"} 1.5e+09
kube_certificatesigningrequest_condition{certificatesigningrequest="certificate-test",condition="approved"} 1
kube_certificatesigningrequest_condition{certificatesigningrequest="certificate-test",condition="denied"} 0
kube_certificatesigningrequest_labels{certificatesigningrequest="certificate-test",label_cert="test"} 1
kube_certificatesigningrequest_labels{certificatesigningrequest="certificate-test"} 1
kube_certificatesigningrequest_cert_length{certificatesigningrequest="certificate-test"} 0
`,
MetricNames: []string{"kube_certificatesigningrequest_created", "kube_certificatesigningrequest_condition", "kube_certificatesigningrequest_labels", "kube_certificatesigningrequest_cert_length"},
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestCsrStore(t *testing.T) {
kube_certificatesigningrequest_created{certificatesigningrequest="certificate-test"} 1.5e+09
kube_certificatesigningrequest_condition{certificatesigningrequest="certificate-test",condition="approved"} 1
kube_certificatesigningrequest_condition{certificatesigningrequest="certificate-test",condition="denied"} 0
kube_certificatesigningrequest_labels{certificatesigningrequest="certificate-test",label_cert="test"} 1
kube_certificatesigningrequest_labels{certificatesigningrequest="certificate-test"} 1
kube_certificatesigningrequest_cert_length{certificatesigningrequest="certificate-test"} 13
`,
MetricNames: []string{"kube_certificatesigningrequest_created", "kube_certificatesigningrequest_condition", "kube_certificatesigningrequest_labels", "kube_certificatesigningrequest_cert_length"},
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestCsrStore(t *testing.T) {
kube_certificatesigningrequest_created{certificatesigningrequest="certificate-test"} 1.5e+09
kube_certificatesigningrequest_condition{certificatesigningrequest="certificate-test",condition="approved"} 1
kube_certificatesigningrequest_condition{certificatesigningrequest="certificate-test",condition="denied"} 1
kube_certificatesigningrequest_labels{certificatesigningrequest="certificate-test",label_cert="test"} 1
kube_certificatesigningrequest_labels{certificatesigningrequest="certificate-test"} 1
kube_certificatesigningrequest_cert_length{certificatesigningrequest="certificate-test"} 0
`,
MetricNames: []string{"kube_certificatesigningrequest_created", "kube_certificatesigningrequest_condition", "kube_certificatesigningrequest_labels", "kube_certificatesigningrequest_cert_length"},
Expand Down Expand Up @@ -205,15 +205,15 @@ func TestCsrStore(t *testing.T) {
kube_certificatesigningrequest_created{certificatesigningrequest="certificate-test"} 1.5e+09
kube_certificatesigningrequest_condition{certificatesigningrequest="certificate-test",condition="approved"} 2
kube_certificatesigningrequest_condition{certificatesigningrequest="certificate-test",condition="denied"} 2
kube_certificatesigningrequest_labels{certificatesigningrequest="certificate-test",label_cert="test"} 1
kube_certificatesigningrequest_labels{certificatesigningrequest="certificate-test"} 1
kube_certificatesigningrequest_cert_length{certificatesigningrequest="certificate-test"} 0
`,
MetricNames: []string{"kube_certificatesigningrequest_created", "kube_certificatesigningrequest_condition", "kube_certificatesigningrequest_labels", "kube_certificatesigningrequest_cert_length"},
},
}
for i, c := range cases {
c.Func = generator.ComposeMetricGenFuncs(csrMetricFamilies)
c.Headers = generator.ExtractMetricFamilyHeaders(csrMetricFamilies)
c.Func = generator.ComposeMetricGenFuncs(csrMetricFamilies(nil))
c.Headers = generator.ExtractMetricFamilyHeaders(csrMetricFamilies(nil))
if err := c.run(); err != nil {
t.Errorf("unexpected error when collecting result in %vth run:\n%s", i, err)
}
Expand Down
8 changes: 5 additions & 3 deletions internal/store/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ var (
descCronJobLabelsName = "kube_cronjob_labels"
descCronJobLabelsHelp = "Kubernetes labels converted to Prometheus labels."
descCronJobLabelsDefaultLabels = []string{"namespace", "cronjob"}
)

cronJobMetricFamilies = []generator.FamilyGenerator{
func cronJobMetricFamilies(allowLabelsList []string) []generator.FamilyGenerator {
return []generator.FamilyGenerator{
*generator.NewFamilyGenerator(
descCronJobLabelsName,
descCronJobLabelsHelp,
metric.Gauge,
"",
wrapCronJobFunc(func(j *batchv1beta1.CronJob) *metric.Family {
labelKeys, labelValues := kubeLabelsToPrometheusLabels(j.Labels)
labelKeys, labelValues := createLabelKeysValues(j.Labels, allowLabelsList)
return &metric.Family{
Metrics: []*metric.Metric{
{
Expand Down Expand Up @@ -201,7 +203,7 @@ var (
}),
),
}
)
}

func wrapCronJobFunc(f func(*batchv1beta1.CronJob) *metric.Family) func(interface{}) *metric.Family {
return func(obj interface{}) *metric.Family {
Expand Down
10 changes: 5 additions & 5 deletions internal/store/cronjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestCronJobStore(t *testing.T) {
# TYPE kube_cronjob_status_active gauge
# TYPE kube_cronjob_status_last_schedule_time gauge
kube_cronjob_info{concurrency_policy="Forbid",cronjob="ActiveRunningCronJob1",namespace="ns1",schedule="0 */6 * * *"} 1
kube_cronjob_labels{cronjob="ActiveRunningCronJob1",label_app="example-active-running-1",namespace="ns1"} 1
kube_cronjob_labels{cronjob="ActiveRunningCronJob1",namespace="ns1"} 1
kube_cronjob_spec_starting_deadline_seconds{cronjob="ActiveRunningCronJob1",namespace="ns1"} 300
kube_cronjob_spec_suspend{cronjob="ActiveRunningCronJob1",namespace="ns1"} 0
kube_cronjob_status_active{cronjob="ActiveRunningCronJob1",namespace="ns1"} 2
Expand Down Expand Up @@ -185,7 +185,7 @@ func TestCronJobStore(t *testing.T) {
# TYPE kube_cronjob_status_active gauge
# TYPE kube_cronjob_status_last_schedule_time gauge
kube_cronjob_info{concurrency_policy="Forbid",cronjob="SuspendedCronJob1",namespace="ns1",schedule="0 */3 * * *"} 1
kube_cronjob_labels{cronjob="SuspendedCronJob1",label_app="example-suspended-1",namespace="ns1"} 1
kube_cronjob_labels{cronjob="SuspendedCronJob1",namespace="ns1"} 1
kube_cronjob_spec_starting_deadline_seconds{cronjob="SuspendedCronJob1",namespace="ns1"} 300
kube_cronjob_spec_suspend{cronjob="SuspendedCronJob1",namespace="ns1"} 1
kube_cronjob_status_active{cronjob="SuspendedCronJob1",namespace="ns1"} 0
Expand Down Expand Up @@ -235,16 +235,16 @@ func TestCronJobStore(t *testing.T) {
kube_cronjob_spec_suspend{cronjob="ActiveCronJob1NoLastScheduled",namespace="ns1"} 0
kube_cronjob_info{concurrency_policy="Forbid",cronjob="ActiveCronJob1NoLastScheduled",namespace="ns1",schedule="25 * * * *"} 1
kube_cronjob_created{cronjob="ActiveCronJob1NoLastScheduled",namespace="ns1"} 1.520766296e+09
kube_cronjob_labels{cronjob="ActiveCronJob1NoLastScheduled",label_app="example-active-no-last-scheduled-1",namespace="ns1"} 1
kube_cronjob_labels{cronjob="ActiveCronJob1NoLastScheduled",namespace="ns1"} 1
` +
fmt.Sprintf("kube_cronjob_next_schedule_time{cronjob=\"ActiveCronJob1NoLastScheduled\",namespace=\"ns1\"} %ve+09\n",
float64(ActiveCronJob1NoLastScheduledNextScheduleTime.Unix())/math.Pow10(9)),
MetricNames: []string{"kube_cronjob_next_schedule_time", "kube_cronjob_spec_starting_deadline_seconds", "kube_cronjob_status_active", "kube_cronjob_spec_suspend", "kube_cronjob_info", "kube_cronjob_created", "kube_cronjob_labels"},
},
}
for i, c := range cases {
c.Func = generator.ComposeMetricGenFuncs(cronJobMetricFamilies)
c.Headers = generator.ExtractMetricFamilyHeaders(cronJobMetricFamilies)
c.Func = generator.ComposeMetricGenFuncs(cronJobMetricFamilies(nil))
c.Headers = generator.ExtractMetricFamilyHeaders(cronJobMetricFamilies(nil))
if err := c.run(); err != nil {
t.Errorf("unexpected collecting result in %vth run:\n%s", i, err)
}
Expand Down
8 changes: 5 additions & 3 deletions internal/store/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ var (
descDaemonSetLabelsName = "kube_daemonset_labels"
descDaemonSetLabelsHelp = "Kubernetes labels converted to Prometheus labels."
descDaemonSetLabelsDefaultLabels = []string{"namespace", "daemonset"}
)

daemonSetMetricFamilies = []generator.FamilyGenerator{
func daemonSetMetricFamilies(allowLabelsList []string) []generator.FamilyGenerator {
return []generator.FamilyGenerator{
*generator.NewFamilyGenerator(
"kube_daemonset_created",
"Unix creation timestamp",
Expand Down Expand Up @@ -214,7 +216,7 @@ var (
metric.Gauge,
"",
wrapDaemonSetFunc(func(d *v1.DaemonSet) *metric.Family {
labelKeys, labelValues := kubeLabelsToPrometheusLabels(d.ObjectMeta.Labels)
labelKeys, labelValues := createLabelKeysValues(d.Labels, allowLabelsList)
return &metric.Family{
Metrics: []*metric.Metric{
{
Expand All @@ -227,7 +229,7 @@ var (
}),
),
}
)
}

func wrapDaemonSetFunc(f func(*v1.DaemonSet) *metric.Family) func(interface{}) *metric.Family {
return func(obj interface{}) *metric.Family {
Expand Down
Loading

0 comments on commit 0b76e7d

Please sign in to comment.