Skip to content

Commit a379639

Browse files
committed
feat: Allow prefixing * paths
Allow prefixing * paths, for instance, foo_*: [foo, bar, baz], will result in foo_baz being the key for all object resolutions under baz. Signed-off-by: Pranshu Srivastava <[email protected]>
1 parent 31d6e8f commit a379639

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
@@ -193,6 +193,8 @@ spec:
193193
# whole objects may be copied into labels by prefixing with "*"
194194
# *anything will be copied into labels, with the highest sorted * strings first
195195
"*": [metadata, labels]
196+
# a prefix before the asterisk will be used as a label prefix
197+
"lorem_*": [metadata, annotations]
196198
"**": [metadata, annotations]
197199
198200
# or specific fields may be copied. these fields will always override values from *s
@@ -203,8 +205,12 @@ spec:
203205
Produces the following metrics:
204206

205207
```prometheus
206-
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
207-
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
208+
kube_customresource_ready_count{customresource_group="myteam.io", customresource_kind="Foo",
209+
customresource_version="v1", active="1",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-a",
210+
lorem_bar="baz",lorem_qux="quxx",} 2
211+
kube_customresource_ready_count{customresource_group="myteam.io", customresource_kind="Foo",
212+
customresource_version="v1", active="3",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-b",
213+
lorem_bar="baz",lorem_qux="quxx",} 4
208214
```
209215

210216
#### VerticalPodAutoscaler

pkg/customresourcestate/registry_factory.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -507,21 +507,24 @@ func addPathLabels(obj interface{}, labels map[string]valuePath, result map[stri
507507
// always do that first so other labels can override
508508
var stars []string
509509
for k := range labels {
510-
if strings.HasPrefix(k, "*") {
510+
if strings.HasPrefix(k, "*") || strings.HasSuffix(k, "*") {
511511
stars = append(stars, k)
512512
}
513513
}
514514
sort.Strings(stars)
515-
for _, k := range stars {
516-
m := labels[k].Get(obj)
515+
for _, star := range stars {
516+
m := labels[star].Get(obj)
517517
if kv, ok := m.(map[string]interface{}); ok {
518518
for k, v := range kv {
519+
if strings.HasSuffix(star, "*") {
520+
k = star[:len(star)-1] + k
521+
}
519522
result[store.SanitizeLabelName(k)] = fmt.Sprintf("%v", v)
520523
}
521524
}
522525
}
523526
for k, v := range labels {
524-
if strings.HasPrefix(k, "*") {
527+
if strings.HasPrefix(k, "*") || strings.HasSuffix(k, "*") {
525528
continue
526529
}
527530
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)