From 2b5460f0c23db0a39277d36e0afaa3c932f1c9a7 Mon Sep 17 00:00:00 2001 From: Jan Kantert Date: Thu, 26 Jan 2023 17:20:23 +0100 Subject: [PATCH] support "True" and "False" as string in custom-resource-state for operator status conditions --- pkg/customresourcestate/registry_factory.go | 6 ++++ .../registry_factory_test.go | 35 +++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/pkg/customresourcestate/registry_factory.go b/pkg/customresourcestate/registry_factory.go index 7e9eca535c..bc1d458a6c 100644 --- a/pkg/customresourcestate/registry_factory.go +++ b/pkg/customresourcestate/registry_factory.go @@ -635,6 +635,12 @@ func toFloat64(value interface{}, nilIsZero bool) (float64, error) { } return 0, nil case string: + if value.(string) == "True" || value.(string) == "true" || value.(string) == "Yes" || value.(string) == "yes" { + return 1, nil + } + if value.(string) == "False" || value.(string) == "false" || value.(string) == "No" || value.(string) == "no" { + return 0, nil + } if t, e := time.Parse(time.RFC3339, value.(string)); e == nil { return float64(t.Unix()), nil } diff --git a/pkg/customresourcestate/registry_factory_test.go b/pkg/customresourcestate/registry_factory_test.go index 1b42a57029..b1824a7647 100644 --- a/pkg/customresourcestate/registry_factory_test.go +++ b/pkg/customresourcestate/registry_factory_test.go @@ -64,7 +64,7 @@ func init() { }, }, "uptime": 43.21, - "conditions": Array{ + "condition_values": Array{ Obj{ "name": "a", "value": 45, @@ -74,6 +74,16 @@ func init() { "value": 66, }, }, + "conditions": Array{ + Obj{ + "type": "Ready", + "status": "True", + }, + Obj{ + "type": "Provisioned", + "status": "False", + }, + }, }, "metadata": Obj{ "name": "foo", @@ -175,7 +185,7 @@ func Test_values(t *testing.T) { }}, {name: "array", each: &compiledGauge{ compiledCommon: compiledCommon{ - path: mustCompilePath(t, "status", "conditions"), + path: mustCompilePath(t, "status", "condition_values"), labelFromPath: map[string]valuePath{ "name": mustCompilePath(t, "name"), }, @@ -233,6 +243,25 @@ func Test_values(t *testing.T) { newEachValue(t, 0, "phase", "bar"), newEachValue(t, 1, "phase", "foo"), }}, + {name: "status_conditions", each: &compiledGauge{ + compiledCommon: compiledCommon{ + path: mustCompilePath(t, "status", "conditions", "[type=Ready]", "status"), + }, + }, wantResult: []eachValue{ + newEachValue(t, 1), + }}, + {name: "status_conditions_all", each: &compiledGauge{ + compiledCommon: compiledCommon{ + path: mustCompilePath(t, "status", "conditions"), + labelFromPath: map[string]valuePath{ + "type": mustCompilePath(t, "type"), + }, + }, + ValueFrom: mustCompilePath(t, "status"), + }, wantResult: []eachValue{ + newEachValue(t, 0, "type", "Provisioned"), + newEachValue(t, 1, "type", "Ready"), + }}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -389,7 +418,7 @@ func Test_valuePath_Get(t *testing.T) { } tests := []testCase{ tt("obj", float64(1), "spec", "replicas"), - tt("array", float64(66), "status", "conditions", "[name=b]", "value"), + tt("array", float64(66), "status", "condition_values", "[name=b]", "value"), tt("array index", true, "spec", "order", "0", "value"), tt("string", "bar", "metadata", "labels", "foo"), tt("match number", false, "spec", "order", "[id=3]", "value"),