Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow prefixing wildcard labelsFromPath #2052

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions docs/customresourcestate-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ spec:
# whole objects may be copied into labels by prefixing with "*"
# *anything will be copied into labels, with the highest sorted * strings first
"*": [metadata, labels]
# a prefix before the asterisk will be used as a label prefix
dgrisonnet marked this conversation as resolved.
Show resolved Hide resolved
"lorem_*": [metadata, annotations]
"**": [metadata, annotations]

# or specific fields may be copied. these fields will always override values from *s
Expand All @@ -203,8 +205,12 @@ spec:
Produces the following metrics:

```prometheus
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
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
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",
lorem_bar="baz",lorem_qux="quxx",} 2
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",
lorem_bar="baz",lorem_qux="quxx",} 4
```

#### VerticalPodAutoscaler
Expand Down
11 changes: 7 additions & 4 deletions pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,21 +507,24 @@ func addPathLabels(obj interface{}, labels map[string]valuePath, result map[stri
// always do that first so other labels can override
var stars []string
for k := range labels {
if strings.HasPrefix(k, "*") {
if strings.HasPrefix(k, "*") || strings.HasSuffix(k, "*") {
stars = append(stars, k)
}
}
sort.Strings(stars)
for _, k := range stars {
m := labels[k].Get(obj)
for _, star := range stars {
m := labels[star].Get(obj)
if kv, ok := m.(map[string]interface{}); ok {
for k, v := range kv {
if strings.HasSuffix(star, "*") {
k = star[:len(star)-1] + k
}
result[store.SanitizeLabelName(k)] = fmt.Sprintf("%v", v)
}
}
}
for k, v := range labels {
if strings.HasPrefix(k, "*") {
if strings.HasPrefix(k, "*") || strings.HasSuffix(k, "*") {
continue
}
value := v.Get(obj)
Expand Down
11 changes: 7 additions & 4 deletions pkg/customresourcestate/registry_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,15 @@ func Test_addPathLabels(t *testing.T) {
{name: "*", args: args{
obj: cr,
labels: map[string]valuePath{
"*1": mustCompilePath(t, "metadata", "annotations"),
"bar": mustCompilePath(t, "metadata", "labels", "foo"),
"*1": mustCompilePath(t, "metadata", "annotations"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to the actual PR, but why does *1 yield "qux": "quxx". Is this the way we tell the code to take the first annotation?

"bar": mustCompilePath(t, "metadata", "labels", "foo"),
"label_object_*": mustCompilePath(t, "metadata", "annotations"),
},
want: map[string]string{
"qux": "quxx",
"bar": "bar",
"qux": "quxx",
"bar": "bar",
"label_object_qux": "quxx",
"label_object_bar": "baz",
},
}},
}
Expand Down