Skip to content

Commit

Permalink
support "True" and "False" as string in custom-resource-state for ope…
Browse files Browse the repository at this point in the history
…rator status conditions
  • Loading branch information
jabdoa2 committed Jan 26, 2023
1 parent bc7b5b6 commit 2b5460f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
6 changes: 6 additions & 0 deletions pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
35 changes: 32 additions & 3 deletions pkg/customresourcestate/registry_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func init() {
},
},
"uptime": 43.21,
"conditions": Array{
"condition_values": Array{
Obj{
"name": "a",
"value": 45,
Expand All @@ -74,6 +74,16 @@ func init() {
"value": 66,
},
},
"conditions": Array{
Obj{
"type": "Ready",
"status": "True",
},
Obj{
"type": "Provisioned",
"status": "False",
},
},
},
"metadata": Obj{
"name": "foo",
Expand Down Expand Up @@ -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"),
},
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit 2b5460f

Please sign in to comment.