diff --git a/docs/customresourcestate-metrics.md b/docs/customresourcestate-metrics.md index 599ad218cf..845ddd1305 100644 --- a/docs/customresourcestate-metrics.md +++ b/docs/customresourcestate-metrics.md @@ -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 diff --git a/pkg/customresourcestate/registry_factory.go b/pkg/customresourcestate/registry_factory.go index 15ebdcbe72..d9cef0d23f 100644 --- a/pkg/customresourcestate/registry_factory.go +++ b/pkg/customresourcestate/registry_factory.go @@ -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, @@ -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 }, diff --git a/pkg/customresourcestate/registry_factory_test.go b/pkg/customresourcestate/registry_factory_test.go index d68f22b617..3207294c57 100644 --- a/pkg/customresourcestate/registry_factory_test.go +++ b/pkg/customresourcestate/registry_factory_test.go @@ -88,6 +88,10 @@ func init() { "status": "False", }, }, + "namespaces": Array{ + "foo", + "bar", + }, }, "metadata": Obj{ "name": "foo", @@ -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"),