Skip to content

Commit

Permalink
Merge pull request #1623 from arajkumar/pdb_label_and_annotation
Browse files Browse the repository at this point in the history
Implement kube_*_labels and kube_*_annotations metrics for pdb
  • Loading branch information
k8s-ci-robot authored Oct 28, 2021
2 parents 2724b83 + b5e7833 commit e2328cf
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/poddisruptionbudget-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_poddisruptionbudget_annotations | Gauge | `poddisruptionbudget`=&lt;poddisruptionbudget-name&gt; <br> `namespace`=&lt;poddisruptionbudget-namespace&gt; <br> `annotation_PODDISRUPTIONBUDGET_ANNOTATION`=&lt;PODDISRUPTIONBUDGET_ANNOATION&gt; | EXPERIMENTAL |
| kube_poddisruptionbudget_labels | Gauge | `poddisruptionbudget`=&lt;poddisruptionbudget-name&gt; <br> `namespace`=&lt;poddisruptionbudget-namespace&gt; <br> `label_PODDISRUPTIONBUDGET_LABEL`=&lt;PODDISRUPTIONBUDGET_ANNOATION&gt; | EXPERIMENTAL |
| kube_poddisruptionbudget_created | Gauge | `poddisruptionbudget`=&lt;pdb-name&gt; <br> `namespace`=&lt;pdb-namespace&gt; | STABLE
| kube_poddisruptionbudget_status_current_healthy | Gauge | `poddisruptionbudget`=&lt;pdb-name&gt; <br> `namespace`=&lt;pdb-namespace&gt; | STABLE
| kube_poddisruptionbudget_status_desired_healthy | Gauge | `poddisruptionbudget`=&lt;pdb-name&gt; <br> `namespace`=&lt;pdb-namespace&gt; | STABLE
Expand Down
2 changes: 1 addition & 1 deletion internal/store/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func (b *Builder) buildPersistentVolumeStores() []cache.Store {
}

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

func (b *Builder) buildReplicaSetStores() []cache.Store {
Expand Down
46 changes: 44 additions & 2 deletions internal/store/poddisruptionbudget.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,50 @@ import (

var (
descPodDisruptionBudgetLabelsDefaultLabels = []string{"namespace", "poddisruptionbudget"}
descPodDisruptionBudgetAnnotationsName = "kube_poddisruptionbudget_annotations"
descPodDisruptionBudgetAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels."
descPodDisruptionBudgetLabelsName = "kube_poddisruptionbudget_labels"
descPodDisruptionBudgetLabelsHelp = "Kubernetes labels converted to Prometheus labels."
)

podDisruptionBudgetMetricFamilies = []generator.FamilyGenerator{
func podDisruptionBudgetMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator {
return []generator.FamilyGenerator{
*generator.NewFamilyGenerator(
descPodDisruptionBudgetAnnotationsName,
descPodDisruptionBudgetAnnotationsHelp,
metric.Gauge,
"",
wrapPodDisruptionBudgetFunc(func(p *v1beta1.PodDisruptionBudget) *metric.Family {
annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", p.Annotations, allowAnnotationsList)
return &metric.Family{
Metrics: []*metric.Metric{
{
LabelKeys: annotationKeys,
LabelValues: annotationValues,
Value: 1,
},
},
}
}),
),
*generator.NewFamilyGenerator(
descPodDisruptionBudgetLabelsName,
descPodDisruptionBudgetLabelsHelp,
metric.Gauge,
"",
wrapPodDisruptionBudgetFunc(func(p *v1beta1.PodDisruptionBudget) *metric.Family {
labelKeys, labelValues := createPrometheusLabelKeysValues("label", p.Labels, allowLabelsList)
return &metric.Family{
Metrics: []*metric.Metric{
{
LabelKeys: labelKeys,
LabelValues: labelValues,
Value: 1,
},
},
}
}),
),
*generator.NewFamilyGenerator(
"kube_poddisruptionbudget_created",
"Unix creation timestamp",
Expand Down Expand Up @@ -129,7 +171,7 @@ var (
}),
),
}
)
}

func wrapPodDisruptionBudgetFunc(f func(*v1beta1.PodDisruptionBudget) *metric.Family) func(interface{}) *metric.Family {
return func(obj interface{}) *metric.Family {
Expand Down
46 changes: 43 additions & 3 deletions internal/store/poddisruptionbudget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ import (
func TestPodDisruptionBudgetStore(t *testing.T) {
// Fixed metadata on type and help text. We prepend this to every expected
// output so we only have to modify a single place when doing adjustments.
const metadata = `
const labelsAndAnnotationsMetaData = `
# HELP kube_poddisruptionbudget_annotations Kubernetes annotations converted to Prometheus labels.
# TYPE kube_poddisruptionbudget_annotations gauge
# HELP kube_poddisruptionbudget_labels Kubernetes labels converted to Prometheus labels.
# TYPE kube_poddisruptionbudget_labels gauge
`
const metadata = labelsAndAnnotationsMetaData + `
# HELP kube_poddisruptionbudget_created Unix creation timestamp
# TYPE kube_poddisruptionbudget_created gauge
# HELP kube_poddisruptionbudget_status_current_healthy Current number of healthy pods
Expand Down Expand Up @@ -61,6 +67,8 @@ func TestPodDisruptionBudgetStore(t *testing.T) {
},
},
Want: metadata + `
kube_poddisruptionbudget_annotations{namespace="ns1",poddisruptionbudget="pdb1"} 1
kube_poddisruptionbudget_labels{namespace="ns1",poddisruptionbudget="pdb1"} 1
kube_poddisruptionbudget_created{namespace="ns1",poddisruptionbudget="pdb1"} 1.5e+09
kube_poddisruptionbudget_status_current_healthy{namespace="ns1",poddisruptionbudget="pdb1"} 12
kube_poddisruptionbudget_status_desired_healthy{namespace="ns1",poddisruptionbudget="pdb1"} 10
Expand All @@ -85,17 +93,49 @@ func TestPodDisruptionBudgetStore(t *testing.T) {
},
},
Want: metadata + `
kube_poddisruptionbudget_annotations{namespace="ns2",poddisruptionbudget="pdb2"} 1
kube_poddisruptionbudget_labels{namespace="ns2",poddisruptionbudget="pdb2"} 1
kube_poddisruptionbudget_status_current_healthy{namespace="ns2",poddisruptionbudget="pdb2"} 8
kube_poddisruptionbudget_status_desired_healthy{namespace="ns2",poddisruptionbudget="pdb2"} 9
kube_poddisruptionbudget_status_pod_disruptions_allowed{namespace="ns2",poddisruptionbudget="pdb2"} 0
kube_poddisruptionbudget_status_expected_pods{namespace="ns2",poddisruptionbudget="pdb2"} 10
kube_poddisruptionbudget_status_observed_generation{namespace="ns2",poddisruptionbudget="pdb2"} 1111
`,
},
{
AllowAnnotationsList: []string{
"app.k8s.io/owner",
},
AllowLabelsList: []string{
"app",
},
Obj: &v1beta1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: "pdb_with_allowed_labels_and_annotations",
Namespace: "ns",
Annotations: map[string]string{
"app.k8s.io/owner": "mysql-server",
"foo": "bar",
},
Labels: map[string]string{
"app": "mysql-server",
"hello": "world",
},
},
},
Want: labelsAndAnnotationsMetaData + `
kube_poddisruptionbudget_annotations{annotation_app_k8s_io_owner="mysql-server",namespace="ns",poddisruptionbudget="pdb_with_allowed_labels_and_annotations"} 1
kube_poddisruptionbudget_labels{label_app="mysql-server",namespace="ns",poddisruptionbudget="pdb_with_allowed_labels_and_annotations"} 1
`,
MetricNames: []string{
"kube_poddisruptionbudget_annotations",
"kube_poddisruptionbudget_labels",
},
},
}
for i, c := range cases {
c.Func = generator.ComposeMetricGenFuncs(podDisruptionBudgetMetricFamilies)
c.Headers = generator.ExtractMetricFamilyHeaders(podDisruptionBudgetMetricFamilies)
c.Func = generator.ComposeMetricGenFuncs(podDisruptionBudgetMetricFamilies(c.AllowAnnotationsList, c.AllowLabelsList))
c.Headers = generator.ExtractMetricFamilyHeaders(podDisruptionBudgetMetricFamilies(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 e2328cf

Please sign in to comment.