Skip to content

Commit

Permalink
Merge pull request #2536 from Haleygo/fix-custom-metric-value-conversion
Browse files Browse the repository at this point in the history
fix(custom resource state metrics): correctly convert status condition `Unknown` to a valid value
  • Loading branch information
k8s-ci-robot authored Nov 8, 2024
2 parents 0bcfd5b + cad704d commit 77e564b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/metrics/extend/customresourcestate-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ Supported types are:
* `nil` is generally mapped to `0.0` if NilIsZero is `true`, otherwise it will throw an error
* for bool `true` is mapped to `1.0` and `false` is mapped to `0.0`
* for string the following logic applies
* `"true"` and `"yes"` are mapped to `1.0` and `"false"` and `"no"` are mapped to `0.0` (all case-insensitive)
* `"true"` and `"yes"` are mapped to `1.0`, `"false"`, `"no"` and `"unknown"` are mapped to `0.0` (all case-insensitive)
* RFC3339 times are parsed to float timestamp
* Quantities like "250m" or "512Gi" are parsed to float using <https://github.com/kubernetes/apimachinery/blob/master/pkg/api/resource/quantity.go>
* Percentages ending with a "%" are parsed to float
Expand Down
7 changes: 5 additions & 2 deletions pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ type compiledCommon struct {
func (c compiledCommon) Path() valuePath {
return c.path
}

func (c compiledCommon) LabelFromPath() map[string]valuePath {
return c.labelFromPath
}

func (c compiledCommon) Type() metric.Type {
return c.t
}
Expand Down Expand Up @@ -477,6 +479,7 @@ func (e eachValue) DefaultLabels(defaults map[string]string) {
}
}
}

func (e eachValue) ToMetric() *metric.Metric {
var keys, values []string
for k := range e.Labels {
Expand Down Expand Up @@ -724,12 +727,12 @@ func toFloat64(value interface{}, nilIsZero bool) (float64, error) {
}
return 0, nil
case string:
// The string contains a boolean as a string
// The string is a boolean or `"unknown"` according to https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#Condition
normalized := strings.ToLower(value.(string))
if normalized == "true" || normalized == "yes" {
return 1, nil
}
if normalized == "false" || normalized == "no" {
if normalized == "false" || normalized == "no" || normalized == "unknown" {
return 0, nil
}
// The string contains a RFC3339 timestamp
Expand Down

0 comments on commit 77e564b

Please sign in to comment.