From 17c69692c470ccc3001a622b3a1e841a8004f353 Mon Sep 17 00:00:00 2001 From: Haley Wang Date: Tue, 29 Oct 2024 13:55:05 +0800 Subject: [PATCH 1/4] fix(custom resource state metrics): corrctly convert status condition `Unknown` to a valid value --- docs/metrics/extend/customresourcestate-metrics.md | 2 +- pkg/customresourcestate/registry_factory.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/metrics/extend/customresourcestate-metrics.md b/docs/metrics/extend/customresourcestate-metrics.md index 9330478b8e..c28b04b0f8 100644 --- a/docs/metrics/extend/customresourcestate-metrics.md +++ b/docs/metrics/extend/customresourcestate-metrics.md @@ -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 * Percentages ending with a "%" are parsed to float diff --git a/pkg/customresourcestate/registry_factory.go b/pkg/customresourcestate/registry_factory.go index e4413d4dc2..39c261bd2e 100644 --- a/pkg/customresourcestate/registry_factory.go +++ b/pkg/customresourcestate/registry_factory.go @@ -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 } @@ -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 { @@ -732,6 +735,9 @@ func toFloat64(value interface{}, nilIsZero bool) (float64, error) { if normalized == "false" || normalized == "no" { return 0, nil } + if normalized == "unknown" { + return 0, nil + } // The string contains a RFC3339 timestamp if t, e := time.Parse(time.RFC3339, value.(string)); e == nil { return float64(t.Unix()), nil From 70088ed1eafe8e9aaa2d502678218ad99e05b287 Mon Sep 17 00:00:00 2001 From: Haley Wang Date: Mon, 4 Nov 2024 16:38:57 +0800 Subject: [PATCH 2/4] apply review comment --- docs/metrics/extend/customresourcestate-metrics.md | 3 ++- pkg/customresourcestate/registry_factory.go | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/metrics/extend/customresourcestate-metrics.md b/docs/metrics/extend/customresourcestate-metrics.md index c28b04b0f8..431fd0f1bc 100644 --- a/docs/metrics/extend/customresourcestate-metrics.md +++ b/docs/metrics/extend/customresourcestate-metrics.md @@ -337,7 +337,8 @@ 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`, `"false"`, `"no"` and `"unknown"` are mapped to `0.0` (all case-insensitive) + * `"true"` and `"yes"` are mapped to `1.0`, `"false"` and `"no"` are mapped to `0.0` (all case-insensitive) + * `"unknown"` is mapped to `-1.0` (all case-insensitive) * RFC3339 times are parsed to float timestamp * Quantities like "250m" or "512Gi" are parsed to float using * Percentages ending with a "%" are parsed to float diff --git a/pkg/customresourcestate/registry_factory.go b/pkg/customresourcestate/registry_factory.go index 39c261bd2e..26639e81ae 100644 --- a/pkg/customresourcestate/registry_factory.go +++ b/pkg/customresourcestate/registry_factory.go @@ -727,7 +727,7 @@ 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 @@ -736,7 +736,7 @@ func toFloat64(value interface{}, nilIsZero bool) (float64, error) { return 0, nil } if normalized == "unknown" { - return 0, nil + return -1, nil } // The string contains a RFC3339 timestamp if t, e := time.Parse(time.RFC3339, value.(string)); e == nil { From d7d2d89017015a2296bd74ebf26a30ee08d90084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20R=C3=BCger?= Date: Fri, 8 Nov 2024 10:32:47 +0100 Subject: [PATCH 3/4] Update docs/metrics/extend/customresourcestate-metrics.md --- docs/metrics/extend/customresourcestate-metrics.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/metrics/extend/customresourcestate-metrics.md b/docs/metrics/extend/customresourcestate-metrics.md index 431fd0f1bc..c28b04b0f8 100644 --- a/docs/metrics/extend/customresourcestate-metrics.md +++ b/docs/metrics/extend/customresourcestate-metrics.md @@ -337,8 +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`, `"false"` and `"no"` are mapped to `0.0` (all case-insensitive) - * `"unknown"` is mapped to `-1.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 * Percentages ending with a "%" are parsed to float From cad704d488926560de36d97cdfc195fe959770fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20R=C3=BCger?= Date: Fri, 8 Nov 2024 10:32:59 +0100 Subject: [PATCH 4/4] Update pkg/customresourcestate/registry_factory.go --- pkg/customresourcestate/registry_factory.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pkg/customresourcestate/registry_factory.go b/pkg/customresourcestate/registry_factory.go index 26639e81ae..5d2f22b068 100644 --- a/pkg/customresourcestate/registry_factory.go +++ b/pkg/customresourcestate/registry_factory.go @@ -732,12 +732,9 @@ func toFloat64(value interface{}, nilIsZero bool) (float64, error) { if normalized == "true" || normalized == "yes" { return 1, nil } - if normalized == "false" || normalized == "no" { + if normalized == "false" || normalized == "no" || normalized == "unknown" { return 0, nil } - if normalized == "unknown" { - return -1, nil - } // The string contains a RFC3339 timestamp if t, e := time.Parse(time.RFC3339, value.(string)); e == nil { return float64(t.Unix()), nil