Skip to content

Commit d799f40

Browse files
committed
feat(customresourcestate): Support percentages
1 parent 26950a4 commit d799f40

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

docs/customresourcestate-metrics.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ Supported types are:
255255
* `"true"` and `"yes"` are mapped to `1.0` and `"false"` and `"no"` are mapped to `0.0` (all case insensitive)
256256
* RFC3339 times are parsed to float timestamp
257257
* Quantities like "250m" or "512Gi" are parsed to float using https://github.com/kubernetes/apimachinery/blob/master/pkg/api/resource/quantity.go
258+
* Percentages ending with a "%" are parsed to float
258259
* 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
259260

260261
##### Example for status conditions on Kubernetes Controllers

pkg/customresourcestate/registry_factory.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"k8s.io/apimachinery/pkg/api/resource"
2929
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
30+
"k8s.io/apimachinery/pkg/util/validation"
3031
"k8s.io/klog/v2"
3132

3233
"k8s.io/kube-state-metrics/v2/pkg/metric"
@@ -707,6 +708,12 @@ func toFloat64(value interface{}, nilIsZero bool) (float64, error) {
707708
if t, e := resource.ParseQuantity(value.(string)); e == nil {
708709
return t.AsApproximateFloat64(), nil
709710
}
711+
// The string contains a percentage with a suffix "%"
712+
if e := validation.IsValidPercent(value.(string)); len(e) == 0 {
713+
t, e := strconv.ParseFloat(strings.TrimRight(value.(string), "%"), 64)
714+
return t / 100, e
715+
}
716+
710717
return strconv.ParseFloat(value.(string), 64)
711718
case byte:
712719
v = float64(vv)

pkg/customresourcestate/registry_factory_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func init() {
6767
"uptime": 43.21,
6868
"quantity_milli": "250m",
6969
"quantity_binarySI": "5Gi",
70+
"percentage": "28%",
7071
"condition_values": Array{
7172
Obj{
7273
"name": "a",
@@ -97,6 +98,7 @@ func init() {
9798
"qux": "quxx",
9899
"bar": "baz",
99100
},
101+
"percentage": "39%",
100102
"creationTimestamp": "2022-06-28T00:00:00Z",
101103
},
102104
})
@@ -241,6 +243,24 @@ func Test_values(t *testing.T) {
241243
}, wantResult: []eachValue{
242244
newEachValue(t, 5.36870912e+09),
243245
}},
246+
{name: "percentage", each: &compiledGauge{
247+
compiledCommon: compiledCommon{
248+
path: mustCompilePath(t, "status", "percentage"),
249+
},
250+
}, wantResult: []eachValue{
251+
newEachValue(t, 0.28),
252+
}},
253+
{name: "path-relative valueFrom percentage", each: &compiledGauge{
254+
compiledCommon: compiledCommon{
255+
path: mustCompilePath(t, "metadata"),
256+
labelFromPath: map[string]valuePath{
257+
"name": mustCompilePath(t, "name"),
258+
},
259+
},
260+
ValueFrom: mustCompilePath(t, "percentage"),
261+
}, wantResult: []eachValue{
262+
newEachValue(t, 0.39, "name", "foo"),
263+
}},
244264
{name: "boolean_string", each: &compiledGauge{
245265
compiledCommon: compiledCommon{
246266
path: mustCompilePath(t, "spec", "paused"),

0 commit comments

Comments
 (0)