Skip to content

Commit

Permalink
Merge pull request kubernetes#1963 from jabdoa2/support_string_bool_i…
Browse files Browse the repository at this point in the history
…n_custom_resource_state

support "True" and "False" as string in custom-resource-state
  • Loading branch information
k8s-ci-robot committed Feb 2, 2023
2 parents 5250ea4 + e26d682 commit 5da24c2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
7 changes: 7 additions & 0 deletions pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,13 @@ func toFloat64(value interface{}, nilIsZero bool) (float64, error) {
}
return 0, nil
case string:
normalized := strings.ToLower(value.(string))
if normalized == "true" || normalized == "yes" {
return 1, nil
}
if normalized == "false" || normalized == "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 5da24c2

Please sign in to comment.