Skip to content

Commit

Permalink
Merge pull request #1581 from mindw/add_configmap_labels_ann
Browse files Browse the repository at this point in the history
Expose configmap labels and annotations
  • Loading branch information
k8s-ci-robot committed Sep 21, 2021
2 parents 6a70201 + 58559bf commit 0d86ab6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/configmap-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

| Metric name| Metric type | Labels/tags | Status |
| ---------- | ----------- | ----------- | ----------- |
| kube_configmap_annotations | Gauge | `configmap`=&lt;configmap-name&gt; <br> `namespace`=&lt;configmap-namespace&gt; <br> `annotation_CONFIGMAP_ANNOTATION`=&lt;CONFIGMAP_ANNOTATION&gt; | EXPERIMENTAL
| kube_configmap_labels | Gauge | `configmap`=&lt;configmap-name&gt; <br> `namespace`=&lt;configmap-namespace&gt; <br> `label_CONFIGMAP_LABEL`=&lt;CONFIGMAP_LABEL&gt; | STABLE
| kube_configmap_info | Gauge | `configmap`=&lt;configmap-name&gt; <br> `namespace`=&lt;configmap-namespace&gt; | STABLE |
| kube_configmap_created | Gauge | `configmap`=&lt;configmap-name&gt; <br> `namespace`=&lt;configmap-namespace&gt; | STABLE |
| kube_configmap_metadata_resource_version | Gauge | `configmap`=&lt;configmap-name&gt; <br> `namespace`=&lt;configmap-namespace&gt; | EXPERIMENTAL |
2 changes: 1 addition & 1 deletion internal/store/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
42 changes: 40 additions & 2 deletions internal/store/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -82,7 +120,7 @@ var (
}),
),
}
)
}

func createConfigMapListWatch(kubeClient clientset.Interface, ns string) cache.ListerWatcher {
return &cache.ListWatch{
Expand Down
31 changes: 28 additions & 3 deletions internal/store/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}
Expand Down

0 comments on commit 0d86ab6

Please sign in to comment.