Skip to content

Commit

Permalink
Merge pull request #2175 from opeco17/feature/allow-annotations-and-l…
Browse files Browse the repository at this point in the history
…abels-for-resource-quota

feat: enable metric-annotations-allowlist and metric-labels-allowlist for ResourceQuota
  • Loading branch information
k8s-ci-robot committed Aug 28, 2023
2 parents 6b1daa7 + 634c04e commit 7324e70
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/resourcequota-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
| ---------- | ----------- | ----------- | ----------- |
| kube_resourcequota | Gauge | `resourcequota`=&lt;quota-name&gt; <br> `namespace`=&lt;namespace&gt; <br> `resource`=&lt;ResourceName&gt; <br> `type`=&lt;quota-type&gt; | STABLE |
| kube_resourcequota_created | Gauge | `resourcequota`=&lt;quota-name&gt; <br> `namespace`=&lt;namespace&gt; | STABLE |
| kube_resourcequota_annotations | Gauge | `resourcequota`=&lt;quota-name&gt; <br> `namespace`=&lt;namespace&gt; <br> `annotation_RESOURCE_QUOTA_ANNOTATION`=&lt;RESOURCE_QUOTA_ANNOTATION&gt; | EXPERIMENTAL |
| kube_resourcequota_labels | Gauge | `resourcequota`=&lt;quota-name&gt; <br> `namespace`=&lt;namespace&gt; <br> `label_RESOURCE_QUOTA_LABEL`=&lt;RESOURCE_QUOTA_LABEL&gt; | EXPERIMENTAL |
2 changes: 1 addition & 1 deletion internal/store/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ func (b *Builder) buildReplicationControllerStores() []cache.Store {
}

func (b *Builder) buildResourceQuotaStores() []cache.Store {
return b.buildStoresFunc(resourceQuotaMetricFamilies, &v1.ResourceQuota{}, createResourceQuotaListWatch, b.useAPIServerCache)
return b.buildStoresFunc(resourceQuotaMetricFamilies(b.allowAnnotationsList["resourcequotas"], b.allowLabelsList["resourcequotas"]), &v1.ResourceQuota{}, createResourceQuotaListWatch, b.useAPIServerCache)
}

func (b *Builder) buildSecretStores() []cache.Store {
Expand Down
54 changes: 52 additions & 2 deletions internal/store/resourcequota.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ import (
)

var (
descResourceQuotaAnnotationsName = "kube_resourcequota_annotations"
descResourceQuotaAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels."
descResourceQuotaLabelsName = "kube_resourcequota_labels"
descResourceQuotaLabelsHelp = "Kubernetes labels converted to Prometheus labels."
descResourceQuotaLabelsDefaultLabels = []string{"namespace", "resourcequota"}
)

resourceQuotaMetricFamilies = []generator.FamilyGenerator{
func resourceQuotaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator {
return []generator.FamilyGenerator{
*generator.NewFamilyGeneratorWithStability(
"kube_resourcequota_created",
"Unix creation timestamp",
Expand Down Expand Up @@ -87,8 +93,52 @@ var (
}
}),
),
*generator.NewFamilyGeneratorWithStability(
descResourceQuotaAnnotationsName,
descResourceQuotaAnnotationsHelp,
metric.Gauge,
basemetrics.ALPHA,
"",
wrapResourceQuotaFunc(func(d *v1.ResourceQuota) *metric.Family {
if len(allowAnnotationsList) == 0 {
return &metric.Family{}
}
annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", d.Annotations, allowAnnotationsList)
return &metric.Family{
Metrics: []*metric.Metric{
{
LabelKeys: annotationKeys,
LabelValues: annotationValues,
Value: 1,
},
},
}
}),
),
*generator.NewFamilyGeneratorWithStability(
descResourceQuotaLabelsName,
descResourceQuotaLabelsHelp,
metric.Gauge,
basemetrics.STABLE,
"",
wrapResourceQuotaFunc(func(d *v1.ResourceQuota) *metric.Family {
if len(allowLabelsList) == 0 {
return &metric.Family{}
}
labelKeys, labelValues := createPrometheusLabelKeysValues("label", d.Labels, allowLabelsList)
return &metric.Family{
Metrics: []*metric.Metric{
{
LabelKeys: labelKeys,
LabelValues: labelValues,
Value: 1,
},
},
}
}),
),
}
)
}

func wrapResourceQuotaFunc(f func(*v1.ResourceQuota) *metric.Family) func(interface{}) *metric.Family {
return func(obj interface{}) *metric.Family {
Expand Down
36 changes: 34 additions & 2 deletions internal/store/resourcequota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ func TestResourceQuotaStore(t *testing.T) {
// output so we only have to modify a single place when doing adjustments.
const metadata = `
# HELP kube_resourcequota [STABLE] Information about resource quota.
# HELP kube_resourcequota_annotations Kubernetes annotations converted to Prometheus labels.
# TYPE kube_resourcequota gauge
# HELP kube_resourcequota_created [STABLE] Unix creation timestamp
# HELP kube_resourcequota_labels [STABLE] Kubernetes labels converted to Prometheus labels.
# TYPE kube_resourcequota_annotations gauge
# TYPE kube_resourcequota_created gauge
# TYPE kube_resourcequota_labels gauge
`
cases := []generateMetricsTestCase{
// Verify populating base metric and that metric for unset fields are skipped.
Expand Down Expand Up @@ -132,10 +136,38 @@ func TestResourceQuotaStore(t *testing.T) {
kube_resourcequota{namespace="testNS",resource="storage",resourcequota="quotaTest",type="used"} 9e+09
`,
},
// Verify kube_resourcequota_annotations and kube_resourcequota_labels are shown.
{
AllowAnnotationsList: []string{
"foo",
},
AllowLabelsList: []string{
"hello",
},
Obj: &v1.ResourceQuota{
ObjectMeta: metav1.ObjectMeta{
Name: "quotaTest",
CreationTimestamp: metav1.Time{Time: time.Unix(1500000000, 0)},
Namespace: "testNS",
Annotations: map[string]string{
"foo": "bar",
},
Labels: map[string]string{
"hello": "world",
},
},
Status: v1.ResourceQuotaStatus{},
},
Want: metadata + `
kube_resourcequota_annotations{annotation_foo="bar",namespace="testNS",resourcequota="quotaTest"} 1
kube_resourcequota_created{namespace="testNS",resourcequota="quotaTest"} 1.5e+09
kube_resourcequota_labels{label_hello="world",namespace="testNS",resourcequota="quotaTest"} 1
`,
},
}
for i, c := range cases {
c.Func = generator.ComposeMetricGenFuncs(resourceQuotaMetricFamilies)
c.Headers = generator.ExtractMetricFamilyHeaders(resourceQuotaMetricFamilies)
c.Func = generator.ComposeMetricGenFuncs(resourceQuotaMetricFamilies(c.AllowAnnotationsList, c.AllowLabelsList))
c.Headers = generator.ExtractMetricFamilyHeaders(resourceQuotaMetricFamilies(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 7324e70

Please sign in to comment.