Skip to content

Commit

Permalink
feat(CustomResourceState): Support quantities
Browse files Browse the repository at this point in the history
  • Loading branch information
mrueg committed Feb 10, 2023
1 parent 1acb4df commit 10f8a9c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/customresourcestate-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ Supported types are:
* 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)
* 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
* finally the string is parsed to float using https://pkg.go.dev/strconv#ParseFloat which should support all common number formats. If that fails an error is yielded

##### Example for status conditions on Kubernetes Controllers
Expand Down
7 changes: 7 additions & 0 deletions pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strings"
"time"

"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/klog/v2"

Expand Down Expand Up @@ -684,16 +685,22 @@ func toFloat64(value interface{}, nilIsZero bool) (float64, error) {
}
return 0, nil
case string:
// The string contains a boolean as a string
normalized := strings.ToLower(value.(string))
if normalized == "true" || normalized == "yes" {
return 1, nil
}
if normalized == "false" || normalized == "no" {
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
}
// The string contains a quantity with a suffix like "25m" (milli) or "5Gi" (binarySI)
if t, e := resource.ParseQuantity(value.(string)); e == nil {
return t.AsApproximateFloat64(), nil
}
return strconv.ParseFloat(value.(string), 64)
case byte:
v = float64(vv)
Expand Down
19 changes: 18 additions & 1 deletion pkg/customresourcestate/registry_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ func init() {
"ready": 4,
},
},
"uptime": 43.21,
"uptime": 43.21,
"quantity_milli": "250m",
"quantity_binarySI": "5Gi",
"condition_values": Array{
Obj{
"name": "a",
Expand Down Expand Up @@ -213,6 +215,21 @@ func Test_values(t *testing.T) {
}, wantResult: []eachValue{
newEachValue(t, 1656374400),
}},
{name: "quantity_milli", each: &compiledGauge{
compiledCommon: compiledCommon{
path: mustCompilePath(t, "status", "quantity_milli"),
},
}, wantResult: []eachValue{
newEachValue(t, 0.25),
}},
{name: "quantity_binarySI", each: &compiledGauge{
compiledCommon: compiledCommon{
path: mustCompilePath(t, "status", "quantity_binarySI"),
},
}, wantResult: []eachValue{
newEachValue(t, 5.36870912e+09),
}},

{name: "boolean_string", each: &compiledGauge{
compiledCommon: compiledCommon{
path: mustCompilePath(t, "spec", "paused"),
Expand Down

0 comments on commit 10f8a9c

Please sign in to comment.