Skip to content

Commit

Permalink
feat(customresourcestate): allow string Array for labelFromPath
Browse files Browse the repository at this point in the history
  • Loading branch information
adberger committed Aug 17, 2023
1 parent a9bdda0 commit aef68f9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
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)
}
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

0 comments on commit aef68f9

Please sign in to comment.