Skip to content

Commit 2b5460f

Browse files
committed
support "True" and "False" as string in custom-resource-state for operator status conditions
1 parent bc7b5b6 commit 2b5460f

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

pkg/customresourcestate/registry_factory.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,12 @@ func toFloat64(value interface{}, nilIsZero bool) (float64, error) {
635635
}
636636
return 0, nil
637637
case string:
638+
if value.(string) == "True" || value.(string) == "true" || value.(string) == "Yes" || value.(string) == "yes" {
639+
return 1, nil
640+
}
641+
if value.(string) == "False" || value.(string) == "false" || value.(string) == "No" || value.(string) == "no" {
642+
return 0, nil
643+
}
638644
if t, e := time.Parse(time.RFC3339, value.(string)); e == nil {
639645
return float64(t.Unix()), nil
640646
}

pkg/customresourcestate/registry_factory_test.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func init() {
6464
},
6565
},
6666
"uptime": 43.21,
67-
"conditions": Array{
67+
"condition_values": Array{
6868
Obj{
6969
"name": "a",
7070
"value": 45,
@@ -74,6 +74,16 @@ func init() {
7474
"value": 66,
7575
},
7676
},
77+
"conditions": Array{
78+
Obj{
79+
"type": "Ready",
80+
"status": "True",
81+
},
82+
Obj{
83+
"type": "Provisioned",
84+
"status": "False",
85+
},
86+
},
7787
},
7888
"metadata": Obj{
7989
"name": "foo",
@@ -175,7 +185,7 @@ func Test_values(t *testing.T) {
175185
}},
176186
{name: "array", each: &compiledGauge{
177187
compiledCommon: compiledCommon{
178-
path: mustCompilePath(t, "status", "conditions"),
188+
path: mustCompilePath(t, "status", "condition_values"),
179189
labelFromPath: map[string]valuePath{
180190
"name": mustCompilePath(t, "name"),
181191
},
@@ -233,6 +243,25 @@ func Test_values(t *testing.T) {
233243
newEachValue(t, 0, "phase", "bar"),
234244
newEachValue(t, 1, "phase", "foo"),
235245
}},
246+
{name: "status_conditions", each: &compiledGauge{
247+
compiledCommon: compiledCommon{
248+
path: mustCompilePath(t, "status", "conditions", "[type=Ready]", "status"),
249+
},
250+
}, wantResult: []eachValue{
251+
newEachValue(t, 1),
252+
}},
253+
{name: "status_conditions_all", each: &compiledGauge{
254+
compiledCommon: compiledCommon{
255+
path: mustCompilePath(t, "status", "conditions"),
256+
labelFromPath: map[string]valuePath{
257+
"type": mustCompilePath(t, "type"),
258+
},
259+
},
260+
ValueFrom: mustCompilePath(t, "status"),
261+
}, wantResult: []eachValue{
262+
newEachValue(t, 0, "type", "Provisioned"),
263+
newEachValue(t, 1, "type", "Ready"),
264+
}},
236265
}
237266
for _, tt := range tests {
238267
t.Run(tt.name, func(t *testing.T) {
@@ -389,7 +418,7 @@ func Test_valuePath_Get(t *testing.T) {
389418
}
390419
tests := []testCase{
391420
tt("obj", float64(1), "spec", "replicas"),
392-
tt("array", float64(66), "status", "conditions", "[name=b]", "value"),
421+
tt("array", float64(66), "status", "condition_values", "[name=b]", "value"),
393422
tt("array index", true, "spec", "order", "0", "value"),
394423
tt("string", "bar", "metadata", "labels", "foo"),
395424
tt("match number", false, "spec", "order", "[id=3]", "value"),

0 commit comments

Comments
 (0)