Skip to content

Commit 17c6969

Browse files
committed
fix(custom resource state metrics): corrctly convert status condition Unknown to a valid value
1 parent eb5e347 commit 17c6969

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
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: 6 additions & 0 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 {
@@ -732,6 +735,9 @@ func toFloat64(value interface{}, nilIsZero bool) (float64, error) {
732735
if normalized == "false" || normalized == "no" {
733736
return 0, nil
734737
}
738+
if normalized == "unknown" {
739+
return 0, nil
740+
}
735741
// The string contains a RFC3339 timestamp
736742
if t, e := time.Parse(time.RFC3339, value.(string)); e == nil {
737743
return float64(t.Unix()), nil

0 commit comments

Comments
 (0)