diff --git a/docs/configmap-metrics.md b/docs/configmap-metrics.md index a143e82657..6e14046746 100644 --- a/docs/configmap-metrics.md +++ b/docs/configmap-metrics.md @@ -2,6 +2,8 @@ | Metric name| Metric type | Labels/tags | Status | | ---------- | ----------- | ----------- | ----------- | +| kube_configmap_annotations | Gauge | `configmap`=<configmap-name>
`namespace`=<configmap-namespace>
`annotation_CONFIGMAP_ANNOTATION`=<CONFIGMAP_ANNOTATION> | EXPERIMENTAL +| kube_configmap_labels | Gauge | `configmap`=<configmap-name>
`namespace`=<configmap-namespace>
`label_CONFIGMAP_LABEL`=<CONFIGMAP_LABEL> | STABLE | kube_configmap_info | Gauge | `configmap`=<configmap-name>
`namespace`=<configmap-namespace> | STABLE | | kube_configmap_created | Gauge | `configmap`=<configmap-name>
`namespace`=<configmap-namespace> | STABLE | | kube_configmap_metadata_resource_version | Gauge | `configmap`=<configmap-name>
`namespace`=<configmap-namespace> | EXPERIMENTAL | diff --git a/internal/store/builder.go b/internal/store/builder.go index 573ef5d53a..bcd09d734a 100644 --- a/internal/store/builder.go +++ b/internal/store/builder.go @@ -238,7 +238,7 @@ func availableResources() []string { } func (b *Builder) buildConfigMapStores() []*metricsstore.MetricsStore { - return b.buildStoresFunc(configMapMetricFamilies, &v1.ConfigMap{}, createConfigMapListWatch, b.useAPIServerCache) + return b.buildStoresFunc(configMapMetricFamilies(b.allowAnnotationsList["configmaps"], b.allowLabelsList["configmaps"]), &v1.ConfigMap{}, createConfigMapListWatch, b.useAPIServerCache) } func (b *Builder) buildCronJobStores() []*metricsstore.MetricsStore { diff --git a/internal/store/configmap.go b/internal/store/configmap.go index 530e3f3036..1cf03cb73d 100644 --- a/internal/store/configmap.go +++ b/internal/store/configmap.go @@ -32,8 +32,46 @@ import ( var ( descConfigMapLabelsDefaultLabels = []string{"namespace", "configmap"} +) - configMapMetricFamilies = []generator.FamilyGenerator{ +func configMapMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { + return []generator.FamilyGenerator{ + *generator.NewFamilyGenerator( + "kube_configmap_annotations", + "Kubernetes annotations converted to Prometheus labels.", + metric.Gauge, + "", + wrapConfigMapFunc(func(c *v1.ConfigMap) *metric.Family { + annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", c.Annotations, allowAnnotationsList) + return &metric.Family{ + Metrics: []*metric.Metric{ + { + LabelKeys: annotationKeys, + LabelValues: annotationValues, + Value: 1, + }, + }, + } + }), + ), + *generator.NewFamilyGenerator( + "kube_configmap_labels", + "Kubernetes labels converted to Prometheus labels.", + metric.Gauge, + "", + wrapConfigMapFunc(func(c *v1.ConfigMap) *metric.Family { + labelKeys, labelValues := createPrometheusLabelKeysValues("label", c.Labels, allowLabelsList) + return &metric.Family{ + Metrics: []*metric.Metric{ + { + LabelKeys: labelKeys, + LabelValues: labelValues, + Value: 1, + }, + }, + } + }), + ), *generator.NewFamilyGenerator( "kube_configmap_info", "Information about configmap.", @@ -82,7 +120,7 @@ var ( }), ), } -) +} func createConfigMapListWatch(kubeClient clientset.Interface, ns string) cache.ListerWatcher { return &cache.ListWatch{ diff --git a/internal/store/configmap_test.go b/internal/store/configmap_test.go index ce8c01563f..0cbc08d3bc 100644 --- a/internal/store/configmap_test.go +++ b/internal/store/configmap_test.go @@ -31,21 +31,46 @@ func TestConfigMapStore(t *testing.T) { cases := []generateMetricsTestCase{ { + AllowAnnotationsList: []string{ + "app.k8s.io/owner", + }, + AllowLabelsList: []string{ + "app", + }, Obj: &v1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: "configmap1", Namespace: "ns1", ResourceVersion: "BBBBB", + Annotations: map[string]string{ + "app": "mysql-server", + "app.k8s.io/owner": "@foo", + }, + Labels: map[string]string{ + "excluded": "me", + "app": "mysql-server", + }, }, }, Want: ` + # HELP kube_configmap_annotations Kubernetes annotations converted to Prometheus labels. + # HELP kube_configmap_labels Kubernetes labels converted to Prometheus labels. # HELP kube_configmap_info Information about configmap. # HELP kube_configmap_metadata_resource_version Resource version representing a specific version of the configmap. + # TYPE kube_configmap_annotations gauge + # TYPE kube_configmap_labels gauge # TYPE kube_configmap_info gauge # TYPE kube_configmap_metadata_resource_version gauge + kube_configmap_annotations{annotation_app_k8s_io_owner="@foo",configmap="configmap1",namespace="ns1"} 1 + kube_configmap_labels{configmap="configmap1",label_app="mysql-server",namespace="ns1"} 1 kube_configmap_info{configmap="configmap1",namespace="ns1"} 1 `, - MetricNames: []string{"kube_configmap_info", "kube_configmap_metadata_resource_version"}, + MetricNames: []string{ + "kube_configmap_annotations", + "kube_configmap_labels", + "kube_configmap_info", + "kube_configmap_metadata_resource_version", + }, }, { Obj: &v1.ConfigMap{ @@ -71,8 +96,8 @@ func TestConfigMapStore(t *testing.T) { }, } for i, c := range cases { - c.Func = generator.ComposeMetricGenFuncs(configMapMetricFamilies) - c.Headers = generator.ExtractMetricFamilyHeaders(configMapMetricFamilies) + c.Func = generator.ComposeMetricGenFuncs(configMapMetricFamilies(c.AllowAnnotationsList, c.AllowLabelsList)) + c.Headers = generator.ExtractMetricFamilyHeaders(configMapMetricFamilies(c.AllowAnnotationsList, c.AllowLabelsList)) if err := c.run(); err != nil { t.Errorf("unexpected collecting result in %vth run:\n%s", i, err) }