Skip to content

Commit b5e7833

Browse files
committed
Implement kube_*_labels and kube_*_annotations metrics for pdb
This commit introduces kube_*_labels and kube_*_annotations metrics to poddisruptionbudget to keep in consistency with other k8s objects. Signed-off-by: Arunprasad Rajkumar <[email protected]>
1 parent 25066f2 commit b5e7833

File tree

4 files changed

+90
-6
lines changed

4 files changed

+90
-6
lines changed

docs/poddisruptionbudget-metrics.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
| Metric name| Metric type | Labels/tags | Status |
44
| ---------- | ----------- | ----------- | ----------- |
5+
| kube_poddisruptionbudget_annotations | Gauge | `poddisruptionbudget`=&lt;poddisruptionbudget-name&gt; <br> `namespace`=&lt;poddisruptionbudget-namespace&gt; <br> `annotation_PODDISRUPTIONBUDGET_ANNOTATION`=&lt;PODDISRUPTIONBUDGET_ANNOATION&gt; | EXPERIMENTAL |
6+
| kube_poddisruptionbudget_labels | Gauge | `poddisruptionbudget`=&lt;poddisruptionbudget-name&gt; <br> `namespace`=&lt;poddisruptionbudget-namespace&gt; <br> `label_PODDISRUPTIONBUDGET_LABEL`=&lt;PODDISRUPTIONBUDGET_ANNOATION&gt; | EXPERIMENTAL |
57
| kube_poddisruptionbudget_created | Gauge | `poddisruptionbudget`=&lt;pdb-name&gt; <br> `namespace`=&lt;pdb-namespace&gt; | STABLE
68
| kube_poddisruptionbudget_status_current_healthy | Gauge | `poddisruptionbudget`=&lt;pdb-name&gt; <br> `namespace`=&lt;pdb-namespace&gt; | STABLE
79
| kube_poddisruptionbudget_status_desired_healthy | Gauge | `poddisruptionbudget`=&lt;pdb-name&gt; <br> `namespace`=&lt;pdb-namespace&gt; | STABLE

internal/store/builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func (b *Builder) buildPersistentVolumeStores() []cache.Store {
325325
}
326326

327327
func (b *Builder) buildPodDisruptionBudgetStores() []cache.Store {
328-
return b.buildStoresFunc(podDisruptionBudgetMetricFamilies, &policy.PodDisruptionBudget{}, createPodDisruptionBudgetListWatch, b.useAPIServerCache)
328+
return b.buildStoresFunc(podDisruptionBudgetMetricFamilies(b.allowAnnotationsList["poddisruptionbudget"], b.allowLabelsList["poddisruptionbudget"]), &policy.PodDisruptionBudget{}, createPodDisruptionBudgetListWatch, b.useAPIServerCache)
329329
}
330330

331331
func (b *Builder) buildReplicaSetStores() []cache.Store {

internal/store/poddisruptionbudget.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,50 @@ import (
3232

3333
var (
3434
descPodDisruptionBudgetLabelsDefaultLabels = []string{"namespace", "poddisruptionbudget"}
35+
descPodDisruptionBudgetAnnotationsName = "kube_poddisruptionbudget_annotations"
36+
descPodDisruptionBudgetAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels."
37+
descPodDisruptionBudgetLabelsName = "kube_poddisruptionbudget_labels"
38+
descPodDisruptionBudgetLabelsHelp = "Kubernetes labels converted to Prometheus labels."
39+
)
3540

36-
podDisruptionBudgetMetricFamilies = []generator.FamilyGenerator{
41+
func podDisruptionBudgetMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator {
42+
return []generator.FamilyGenerator{
43+
*generator.NewFamilyGenerator(
44+
descPodDisruptionBudgetAnnotationsName,
45+
descPodDisruptionBudgetAnnotationsHelp,
46+
metric.Gauge,
47+
"",
48+
wrapPodDisruptionBudgetFunc(func(p *v1beta1.PodDisruptionBudget) *metric.Family {
49+
annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", p.Annotations, allowAnnotationsList)
50+
return &metric.Family{
51+
Metrics: []*metric.Metric{
52+
{
53+
LabelKeys: annotationKeys,
54+
LabelValues: annotationValues,
55+
Value: 1,
56+
},
57+
},
58+
}
59+
}),
60+
),
61+
*generator.NewFamilyGenerator(
62+
descPodDisruptionBudgetLabelsName,
63+
descPodDisruptionBudgetLabelsHelp,
64+
metric.Gauge,
65+
"",
66+
wrapPodDisruptionBudgetFunc(func(p *v1beta1.PodDisruptionBudget) *metric.Family {
67+
labelKeys, labelValues := createPrometheusLabelKeysValues("label", p.Labels, allowLabelsList)
68+
return &metric.Family{
69+
Metrics: []*metric.Metric{
70+
{
71+
LabelKeys: labelKeys,
72+
LabelValues: labelValues,
73+
Value: 1,
74+
},
75+
},
76+
}
77+
}),
78+
),
3779
*generator.NewFamilyGenerator(
3880
"kube_poddisruptionbudget_created",
3981
"Unix creation timestamp",
@@ -129,7 +171,7 @@ var (
129171
}),
130172
),
131173
}
132-
)
174+
}
133175

134176
func wrapPodDisruptionBudgetFunc(f func(*v1beta1.PodDisruptionBudget) *metric.Family) func(interface{}) *metric.Family {
135177
return func(obj interface{}) *metric.Family {

internal/store/poddisruptionbudget_test.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ import (
2929
func TestPodDisruptionBudgetStore(t *testing.T) {
3030
// Fixed metadata on type and help text. We prepend this to every expected
3131
// output so we only have to modify a single place when doing adjustments.
32-
const metadata = `
32+
const labelsAndAnnotationsMetaData = `
33+
# HELP kube_poddisruptionbudget_annotations Kubernetes annotations converted to Prometheus labels.
34+
# TYPE kube_poddisruptionbudget_annotations gauge
35+
# HELP kube_poddisruptionbudget_labels Kubernetes labels converted to Prometheus labels.
36+
# TYPE kube_poddisruptionbudget_labels gauge
37+
`
38+
const metadata = labelsAndAnnotationsMetaData + `
3339
# HELP kube_poddisruptionbudget_created Unix creation timestamp
3440
# TYPE kube_poddisruptionbudget_created gauge
3541
# HELP kube_poddisruptionbudget_status_current_healthy Current number of healthy pods
@@ -61,6 +67,8 @@ func TestPodDisruptionBudgetStore(t *testing.T) {
6167
},
6268
},
6369
Want: metadata + `
70+
kube_poddisruptionbudget_annotations{namespace="ns1",poddisruptionbudget="pdb1"} 1
71+
kube_poddisruptionbudget_labels{namespace="ns1",poddisruptionbudget="pdb1"} 1
6472
kube_poddisruptionbudget_created{namespace="ns1",poddisruptionbudget="pdb1"} 1.5e+09
6573
kube_poddisruptionbudget_status_current_healthy{namespace="ns1",poddisruptionbudget="pdb1"} 12
6674
kube_poddisruptionbudget_status_desired_healthy{namespace="ns1",poddisruptionbudget="pdb1"} 10
@@ -85,17 +93,49 @@ func TestPodDisruptionBudgetStore(t *testing.T) {
8593
},
8694
},
8795
Want: metadata + `
96+
kube_poddisruptionbudget_annotations{namespace="ns2",poddisruptionbudget="pdb2"} 1
97+
kube_poddisruptionbudget_labels{namespace="ns2",poddisruptionbudget="pdb2"} 1
8898
kube_poddisruptionbudget_status_current_healthy{namespace="ns2",poddisruptionbudget="pdb2"} 8
8999
kube_poddisruptionbudget_status_desired_healthy{namespace="ns2",poddisruptionbudget="pdb2"} 9
90100
kube_poddisruptionbudget_status_pod_disruptions_allowed{namespace="ns2",poddisruptionbudget="pdb2"} 0
91101
kube_poddisruptionbudget_status_expected_pods{namespace="ns2",poddisruptionbudget="pdb2"} 10
92102
kube_poddisruptionbudget_status_observed_generation{namespace="ns2",poddisruptionbudget="pdb2"} 1111
93103
`,
94104
},
105+
{
106+
AllowAnnotationsList: []string{
107+
"app.k8s.io/owner",
108+
},
109+
AllowLabelsList: []string{
110+
"app",
111+
},
112+
Obj: &v1beta1.PodDisruptionBudget{
113+
ObjectMeta: metav1.ObjectMeta{
114+
Name: "pdb_with_allowed_labels_and_annotations",
115+
Namespace: "ns",
116+
Annotations: map[string]string{
117+
"app.k8s.io/owner": "mysql-server",
118+
"foo": "bar",
119+
},
120+
Labels: map[string]string{
121+
"app": "mysql-server",
122+
"hello": "world",
123+
},
124+
},
125+
},
126+
Want: labelsAndAnnotationsMetaData + `
127+
kube_poddisruptionbudget_annotations{annotation_app_k8s_io_owner="mysql-server",namespace="ns",poddisruptionbudget="pdb_with_allowed_labels_and_annotations"} 1
128+
kube_poddisruptionbudget_labels{label_app="mysql-server",namespace="ns",poddisruptionbudget="pdb_with_allowed_labels_and_annotations"} 1
129+
`,
130+
MetricNames: []string{
131+
"kube_poddisruptionbudget_annotations",
132+
"kube_poddisruptionbudget_labels",
133+
},
134+
},
95135
}
96136
for i, c := range cases {
97-
c.Func = generator.ComposeMetricGenFuncs(podDisruptionBudgetMetricFamilies)
98-
c.Headers = generator.ExtractMetricFamilyHeaders(podDisruptionBudgetMetricFamilies)
137+
c.Func = generator.ComposeMetricGenFuncs(podDisruptionBudgetMetricFamilies(c.AllowAnnotationsList, c.AllowLabelsList))
138+
c.Headers = generator.ExtractMetricFamilyHeaders(podDisruptionBudgetMetricFamilies(c.AllowAnnotationsList, c.AllowLabelsList))
99139
if err := c.run(); err != nil {
100140
t.Errorf("unexpected collecting result in %vth run:\n%s", i, err)
101141
}

0 commit comments

Comments
 (0)