Skip to content

Commit 77e564b

Browse files
authored
Merge pull request #2536 from Haleygo/fix-custom-metric-value-conversion
fix(custom resource state metrics): correctly convert status condition `Unknown` to a valid value
2 parents 0bcfd5b + cad704d commit 77e564b

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

docs/metrics/extend/customresourcestate-metrics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ Supported types are:
337337
* `nil` is generally mapped to `0.0` if NilIsZero is `true`, otherwise it will throw an error
338338
* for bool `true` is mapped to `1.0` and `false` is mapped to `0.0`
339339
* for string the following logic applies
340-
* `"true"` and `"yes"` are mapped to `1.0` and `"false"` and `"no"` are mapped to `0.0` (all case-insensitive)
340+
* `"true"` and `"yes"` are mapped to `1.0`, `"false"`, `"no"` and `"unknown"` are mapped to `0.0` (all case-insensitive)
341341
* RFC3339 times are parsed to float timestamp
342342
* Quantities like "250m" or "512Gi" are parsed to float using <https://github.com/kubernetes/apimachinery/blob/master/pkg/api/resource/quantity.go>
343343
* Percentages ending with a "%" are parsed to float

pkg/customresourcestate/registry_factory.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,11 @@ type compiledCommon struct {
131131
func (c compiledCommon) Path() valuePath {
132132
return c.path
133133
}
134+
134135
func (c compiledCommon) LabelFromPath() map[string]valuePath {
135136
return c.labelFromPath
136137
}
138+
137139
func (c compiledCommon) Type() metric.Type {
138140
return c.t
139141
}
@@ -477,6 +479,7 @@ func (e eachValue) DefaultLabels(defaults map[string]string) {
477479
}
478480
}
479481
}
482+
480483
func (e eachValue) ToMetric() *metric.Metric {
481484
var keys, values []string
482485
for k := range e.Labels {
@@ -724,12 +727,12 @@ func toFloat64(value interface{}, nilIsZero bool) (float64, error) {
724727
}
725728
return 0, nil
726729
case string:
727-
// The string contains a boolean as a string
730+
// The string is a boolean or `"unknown"` according to https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#Condition
728731
normalized := strings.ToLower(value.(string))
729732
if normalized == "true" || normalized == "yes" {
730733
return 1, nil
731734
}
732-
if normalized == "false" || normalized == "no" {
735+
if normalized == "false" || normalized == "no" || normalized == "unknown" {
733736
return 0, nil
734737
}
735738
// The string contains a RFC3339 timestamp

0 commit comments

Comments
 (0)