Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support "True" and "False" as string in custom-resource-state #1963

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
jabdoa2 marked this conversation as resolved.
Show resolved Hide resolved
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