diff --git a/pkg/scheduler/metrics/resources/resources_test.go b/pkg/scheduler/metrics/resources/resources_test.go index 71a396f1f83..a1621414730 100644 --- a/pkg/scheduler/metrics/resources/resources_test.go +++ b/pkg/scheduler/metrics/resources/resources_test.go @@ -71,7 +71,7 @@ func Test_podResourceCollector_Handler(t *testing.T) { "custom": resource.MustParse("0"), }, Limits: v1.ResourceList{ - "memory": resource.MustParse("2G"), + "memory": resource.MustParse("2.5Gi"), "custom": resource.MustParse("6"), }, }}, @@ -95,7 +95,7 @@ func Test_podResourceCollector_Handler(t *testing.T) { expected := `# HELP kube_pod_resource_limit [ALPHA] Resources limit for workloads on the cluster, broken down by pod. This shows the resource usage the scheduler and kubelet expect per pod for resources along with the unit for the resource if any. # TYPE kube_pod_resource_limit gauge kube_pod_resource_limit{namespace="test",node="node-one",pod="foo",priority="",resource="custom",scheduler="",unit=""} 6 -kube_pod_resource_limit{namespace="test",node="node-one",pod="foo",priority="",resource="memory",scheduler="",unit="bytes"} 2e+09 +kube_pod_resource_limit{namespace="test",node="node-one",pod="foo",priority="",resource="memory",scheduler="",unit="bytes"} 2.68435456e+09 # HELP kube_pod_resource_request [ALPHA] Resources requested by workloads on the cluster, broken down by pod. This shows the resource usage the scheduler and kubelet expect per pod for resources along with the unit for the resource if any. # TYPE kube_pod_resource_request gauge kube_pod_resource_request{namespace="test",node="node-one",pod="foo",priority="",resource="cpu",scheduler="",unit="cores"} 2 diff --git a/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go b/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go index 1927afb8acb..000f96eb9d1 100644 --- a/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go +++ b/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go @@ -459,6 +459,7 @@ func (q *Quantity) AsApproximateFloat64() float64 { if exponent == 0 { return base } + return base * math.Pow10(exponent) } diff --git a/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity_test.go b/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity_test.go index 7e70d2466ad..3442689c45c 100644 --- a/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity_test.go @@ -1260,6 +1260,40 @@ func TestQuantityAsApproximateFloat64(t *testing.T) { } } +func TestStringQuantityAsApproximateFloat64(t *testing.T) { + table := []struct { + in string + out float64 + }{ + {"2Ki", 2048}, + {"1.1Ki", 1126.4e+0}, + {"1Mi", 1.048576e+06}, + {"2Gi", 2.147483648e+09}, + } + + for _, item := range table { + t.Run(item.in, func(t *testing.T) { + in, err := ParseQuantity(item.in) + if err != nil { + t.Fatal(err) + } + out := in.AsApproximateFloat64() + if out != item.out { + t.Fatalf("expected %v, got %v", item.out, out) + } + if in.d.Dec != nil { + if i, ok := in.AsInt64(); ok { + q := intQuantity(i, 0, in.Format) + out := q.AsApproximateFloat64() + if out != item.out { + t.Fatalf("as int quantity: expected %v, got %v", item.out, out) + } + } + } + }) + } +} + func benchmarkQuantities() []Quantity { return []Quantity{ intQuantity(1024*1024*1024, 0, BinarySI),