Skip to content

Commit a3489c0

Browse files
authored
Merge pull request #2052 from rexagod/prefixed-star-paths
feat: Allow prefixing wildcard `labelsFromPath`
2 parents 97c710b + a379639 commit a3489c0

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

docs/customresourcestate-metrics.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ spec:
198198
# whole objects may be copied into labels by prefixing with "*"
199199
# *anything will be copied into labels, with the highest sorted * strings first
200200
"*": [metadata, labels]
201+
# a prefix before the asterisk will be used as a label prefix
202+
"lorem_*": [metadata, annotations]
201203
"**": [metadata, annotations]
202204
203205
# or specific fields may be copied. these fields will always override values from *s
@@ -208,8 +210,12 @@ spec:
208210
Produces the following metrics:
209211

210212
```prometheus
211-
kube_customresource_ready_count{customresource_group="myteam.io", customresource_kind="Foo", customresource_version="v1", active="1",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-a"} 2
212-
kube_customresource_ready_count{customresource_group="myteam.io", customresource_kind="Foo", customresource_version="v1", active="3",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-b"} 4
213+
kube_customresource_ready_count{customresource_group="myteam.io", customresource_kind="Foo",
214+
customresource_version="v1", active="1",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-a",
215+
lorem_bar="baz",lorem_qux="quxx",} 2
216+
kube_customresource_ready_count{customresource_group="myteam.io", customresource_kind="Foo",
217+
customresource_version="v1", active="3",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-b",
218+
lorem_bar="baz",lorem_qux="quxx",} 4
213219
```
214220

215221
#### Non-map Arrays

pkg/customresourcestate/registry_factory.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -517,21 +517,24 @@ func addPathLabels(obj interface{}, labels map[string]valuePath, result map[stri
517517
// always do that first so other labels can override
518518
var stars []string
519519
for k := range labels {
520-
if strings.HasPrefix(k, "*") {
520+
if strings.HasPrefix(k, "*") || strings.HasSuffix(k, "*") {
521521
stars = append(stars, k)
522522
}
523523
}
524524
sort.Strings(stars)
525-
for _, k := range stars {
526-
m := labels[k].Get(obj)
525+
for _, star := range stars {
526+
m := labels[star].Get(obj)
527527
if kv, ok := m.(map[string]interface{}); ok {
528528
for k, v := range kv {
529+
if strings.HasSuffix(star, "*") {
530+
k = star[:len(star)-1] + k
531+
}
529532
result[store.SanitizeLabelName(k)] = fmt.Sprintf("%v", v)
530533
}
531534
}
532535
}
533536
for k, v := range labels {
534-
if strings.HasPrefix(k, "*") {
537+
if strings.HasPrefix(k, "*") || strings.HasSuffix(k, "*") {
535538
continue
536539
}
537540
value := v.Get(obj)

pkg/customresourcestate/registry_factory_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,15 @@ func Test_addPathLabels(t *testing.T) {
134134
{name: "*", args: args{
135135
obj: cr,
136136
labels: map[string]valuePath{
137-
"*1": mustCompilePath(t, "metadata", "annotations"),
138-
"bar": mustCompilePath(t, "metadata", "labels", "foo"),
137+
"*1": mustCompilePath(t, "metadata", "annotations"),
138+
"bar": mustCompilePath(t, "metadata", "labels", "foo"),
139+
"label_object_*": mustCompilePath(t, "metadata", "annotations"),
139140
},
140141
want: map[string]string{
141-
"qux": "quxx",
142-
"bar": "bar",
142+
"qux": "quxx",
143+
"bar": "bar",
144+
"label_object_qux": "quxx",
145+
"label_object_bar": "baz",
143146
},
144147
}},
145148
}

0 commit comments

Comments
 (0)