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(customresourcestate): allow string Array for labelFromPath #2150

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions docs/customresourcestate-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,9 @@ Examples:
# indexing an array
[spec, order, "0", value] # spec.order[0].value = true

# indexing a string array
["."]

# finding an element in a list by key=value
[status, conditions, "[name=a]", value] # status.conditions[0].value = 45

Expand Down
6 changes: 5 additions & 1 deletion pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func newCompiledMetric(m Metric) (compiledMetric, error) {
}
valueFromPath, err := compilePath(m.StateSet.ValueFrom)
if err != nil {
return nil, fmt.Errorf("each.gauge.valueFrom: %w", err)
return nil, fmt.Errorf("each.stateSet.valueFrom: %w", err)
Copy link
Member

Choose a reason for hiding this comment

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

Good catch!

}
return &compiledStateSet{
compiledCommon: *cc,
Expand Down Expand Up @@ -630,6 +630,10 @@ func compilePath(path []string) (out valuePath, _ error) {
return fmt.Errorf("list index out of range: %s", part)
}
return s[i]
} else if s, ok := m.(string); ok {
if strings.Contains(path[len(path)-1], ".") {
return s
}
}
return nil
},
Expand Down
15 changes: 15 additions & 0 deletions pkg/customresourcestate/registry_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func init() {
"status": "False",
},
},
"namespaces": Array{
"foo",
"bar",
},
},
"metadata": Obj{
"name": "foo",
Expand Down Expand Up @@ -161,6 +165,17 @@ func Test_values(t *testing.T) {
}

tests := []tc{
{name: "info labels from namespaces", each: &compiledInfo{
compiledCommon: compiledCommon{
path: mustCompilePath(t, "status", "namespaces"),
labelFromPath: map[string]valuePath{
"namespace": mustCompilePath(t, "."),
},
},
}, wantResult: []eachValue{
newEachValue(t, 1, "namespace", "bar"),
newEachValue(t, 1, "namespace", "foo"),
}},
{name: "single", each: &compiledGauge{
compiledCommon: compiledCommon{
path: mustCompilePath(t, "spec", "replicas"),
Expand Down