Skip to content

Commit 70d039b

Browse files
committed
Add wildcard option to labels-metric-allow-list
1 parent bd6335b commit 70d039b

7 files changed

Lines changed: 78 additions & 10 deletions

File tree

docs/cli-arguments.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Usage of ./kube-state-metrics:
3939
--logtostderr log to standard error instead of files (default true)
4040
--metric-allowlist string Comma-separated list of metrics to be exposed. This list comprises of exact metric names and/or regex patterns. The allowlist and denylist are mutually exclusive.
4141
--metric-denylist string Comma-separated list of metrics not to be enabled. This list comprises of exact metric names and/or regex patterns. The allowlist and denylist are mutually exclusive.
42-
--metric-labels-allowlist string Comma-separated list of additional Kubernetes label keys that will be used in the resource' labels metric. By default the metric contains only name and namespace labels. To include additional labels provide a list of resource names in their plural form and Kubernetes label keys you would like to allow for them (Example: '=namespaces=[k8s-label-1,k8s-label-n,...],pods=[app],...)'
42+
--metric-labels-allowlist string Comma-separated list of additional Kubernetes label keys that will be used in the resource' labels metric. By default the metric contains only name and namespace labels. To include additional labels provide a list of resource names in their plural form and Kubernetes label keys you would like to allow for them (Example: '=namespaces=[k8s-label-1,k8s-label-n,...],pods=[app],...)'. A single '*' can be provided per resource instead to allow any labels, but that has severe performance implications (Example: '=pods=[*]').
4343
--namespaces string Comma-separated list of namespaces to be enabled. Defaults to ""
4444
--one_output If true, only write logs to their native severity level (vs also writing to each lower severity level)
4545
--pod string Name of the pod that contains the kube-state-metrics container. When set, it is expected that --pod and --pod-namespace are both set. Most likely this should be passed via the downward API. This is used for auto-detecting sharding. If set, this has preference over statically configured sharding. This is experimental, it may be removed without notice.

internal/store/pod_test.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626

2727
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
28+
"k8s.io/kube-state-metrics/v2/pkg/options"
2829
)
2930

3031
func TestPodStore(t *testing.T) {
@@ -1492,11 +1493,55 @@ func TestPodStore(t *testing.T) {
14921493
"kube_pod_runtimeclass_name_info",
14931494
},
14941495
},
1496+
{
1497+
Obj: &v1.Pod{
1498+
ObjectMeta: metav1.ObjectMeta{
1499+
Name: "pod1",
1500+
Namespace: "ns1",
1501+
UID: "uid1",
1502+
Labels: map[string]string{
1503+
"app": "example",
1504+
},
1505+
},
1506+
Spec: v1.PodSpec{},
1507+
},
1508+
AllowLabelsList: []string{"wildcard-not-first", options.LabelWildcard},
1509+
Want: `
1510+
# HELP kube_pod_labels Kubernetes labels converted to Prometheus labels.
1511+
# TYPE kube_pod_labels gauge
1512+
kube_pod_labels{namespace="ns1",pod="pod1",uid="uid1"} 1
1513+
`,
1514+
MetricNames: []string{
1515+
"kube_pod_labels",
1516+
},
1517+
},
1518+
{
1519+
Obj: &v1.Pod{
1520+
ObjectMeta: metav1.ObjectMeta{
1521+
Name: "pod1",
1522+
Namespace: "ns1",
1523+
UID: "uid1",
1524+
Labels: map[string]string{
1525+
"app": "example",
1526+
},
1527+
},
1528+
Spec: v1.PodSpec{},
1529+
},
1530+
AllowLabelsList: []string{options.LabelWildcard},
1531+
Want: `
1532+
# HELP kube_pod_labels Kubernetes labels converted to Prometheus labels.
1533+
# TYPE kube_pod_labels gauge
1534+
kube_pod_labels{label_app="example",namespace="ns1",pod="pod1",uid="uid1"} 1
1535+
`,
1536+
MetricNames: []string{
1537+
"kube_pod_labels",
1538+
},
1539+
},
14951540
}
14961541

14971542
for i, c := range cases {
1498-
c.Func = generator.ComposeMetricGenFuncs(podMetricFamilies(nil))
1499-
c.Headers = generator.ExtractMetricFamilyHeaders(podMetricFamilies(nil))
1543+
c.Func = generator.ComposeMetricGenFuncs(podMetricFamilies(c.AllowLabelsList))
1544+
c.Headers = generator.ExtractMetricFamilyHeaders(podMetricFamilies(c.AllowLabelsList))
15001545
if err := c.run(); err != nil {
15011546
t.Errorf("unexpected collecting result in %vth run:\n%s", i, err)
15021547
}

internal/store/testutils.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ import (
3030
)
3131

3232
type generateMetricsTestCase struct {
33-
Obj interface{}
34-
MetricNames []string
35-
Want string
36-
Headers []string
37-
Func func(interface{}) []metric.FamilyInterface
33+
Obj interface{}
34+
MetricNames []string
35+
AllowLabelsList []string
36+
Want string
37+
Headers []string
38+
Func func(interface{}) []metric.FamilyInterface
3839
}
3940

4041
func (testCase *generateMetricsTestCase) run() error {

internal/store/utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"k8s.io/apimachinery/pkg/util/validation"
2828

2929
"k8s.io/kube-state-metrics/v2/pkg/metric"
30+
"k8s.io/kube-state-metrics/v2/pkg/options"
3031
)
3132

3233
var (
@@ -177,6 +178,10 @@ func createLabelKeysValues(allKubeLabels map[string]string, allowList []string)
177178
allowedKubeLabels := make(map[string]string)
178179

179180
if len(allowList) > 0 {
181+
if allowList[0] == options.LabelWildcard {
182+
return kubeLabelsToPrometheusLabels(allKubeLabels)
183+
}
184+
180185
for _, l := range allowList {
181186
v, found := allKubeLabels[l]
182187
if found {

pkg/options/options.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ func (o *Options) AddFlags() {
9090
o.flags.Var(&o.Namespaces, "namespaces", fmt.Sprintf("Comma-separated list of namespaces to be enabled. Defaults to %q", &DefaultNamespaces))
9191
o.flags.Var(&o.MetricAllowlist, "metric-allowlist", "Comma-separated list of metrics to be exposed. This list comprises of exact metric names and/or regex patterns. The allowlist and denylist are mutually exclusive.")
9292
o.flags.Var(&o.MetricDenylist, "metric-denylist", "Comma-separated list of metrics not to be enabled. This list comprises of exact metric names and/or regex patterns. The allowlist and denylist are mutually exclusive.")
93-
o.flags.Var(&o.LabelsAllowList, "metric-labels-allowlist", "Comma-separated list of additional Kubernetes label keys that will be used in the resource' labels metric. By default the metric contains only name and namespace labels. To include additional labels provide a list of resource names in their plural form and Kubernetes label keys you would like to allow for them (Example: '=namespaces=[k8s-label-1,k8s-label-n,...],pods=[app],...)'")
94-
93+
o.flags.Var(&o.LabelsAllowList, "metric-labels-allowlist", "Comma-separated list of additional Kubernetes label keys that will be used in the resource' labels metric. By default the metric contains only name and namespace labels. To include additional labels provide a list of resource names in their plural form and Kubernetes label keys you would like to allow for them (Example: '=namespaces=[k8s-label-1,k8s-label-n,...],pods=[app],...)'. A single '*' can be provided per resource instead to allow any labels, but that has severe performance implications (Example: '=pods=[*]').")
9594
o.flags.Int32Var(&o.Shard, "shard", int32(0), "The instances shard nominal (zero indexed) within the total number of shards. (default 0)")
9695
o.flags.IntVar(&o.TotalShards, "total-shards", 1, "The total number of shards. Sharding is disabled when total shards is set to 1.")
9796

pkg/options/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ func (n *NamespaceList) Type() string {
130130
return "string"
131131
}
132132

133+
// LabelWildcard allowlists any label
134+
const LabelWildcard = "*"
135+
133136
// LabelsAllowList represents a list of allowed labels for metrics.
134137
type LabelsAllowList map[string][]string
135138

pkg/options/types_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ func TestLabelsAllowListSet(t *testing.T) {
187187
},
188188
"pods": {}}),
189189
},
190+
{
191+
Desc: "with wildcard",
192+
Value: "cronjobs=[*],pods=[*,foo],namespaces=[bar,*]",
193+
Wanted: LabelsAllowList(map[string][]string{
194+
"cronjobs": {
195+
"*",
196+
},
197+
"pods": {
198+
"*",
199+
"foo",
200+
},
201+
"namespaces": {
202+
"bar",
203+
"*"}}),
204+
},
190205
}
191206

192207
for _, test := range tests {

0 commit comments

Comments
 (0)