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 2395656cc90..000f96eb9d1 100644 --- a/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go +++ b/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go @@ -460,17 +460,7 @@ func (q *Quantity) AsApproximateFloat64() float64 { return base } - // multiply by the appropriate exponential scale - switch q.Format { - case DecimalExponent, DecimalSI: - return base * math.Pow10(exponent) - default: - // fast path for exponents that can fit in 64 bits - if exponent > 0 && exponent < 7 { - return base * float64(int64(1)<<(exponent*10)) - } - return base * math.Pow(2, float64(exponent*10)) - } + return base * math.Pow10(exponent) } // AsInt64 returns a representation of the current value as an int64 if a fast conversion 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 cb459b69d45..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 @@ -1207,11 +1207,11 @@ func TestQuantityAsApproximateFloat64(t *testing.T) { {decQuantity(1024, 0, BinarySI), 1024}, {decQuantity(8*1024, 0, BinarySI), 8 * 1024}, {decQuantity(7*1024*1024, 0, BinarySI), 7 * 1024 * 1024}, - {decQuantity(7*1024*1024, 1, BinarySI), (7 * 1024 * 1024) * 1024}, - {decQuantity(7*1024*1024, 4, BinarySI), (7 * 1024 * 1024) * (1024 * 1024 * 1024 * 1024)}, - {decQuantity(7*1024*1024, 8, BinarySI), (7 * 1024 * 1024) * (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)}, - {decQuantity(7*1024*1024, -1, BinarySI), (7 * 1024 * 1024) / float64(1024)}, - {decQuantity(7*1024*1024, -8, BinarySI), (7 * 1024 * 1024) / float64(1024*1024*1024*1024*1024*1024*1024*1024)}, + {decQuantity(7*1024*1024, 1, BinarySI), (7 * 1024 * 1024) * 10}, + {decQuantity(7*1024*1024, 4, BinarySI), (7 * 1024 * 1024) * 10000}, + {decQuantity(7*1024*1024, 8, BinarySI), (7 * 1024 * 1024) * 100000000}, + {decQuantity(7*1024*1024, -1, BinarySI), (7 * 1024 * 1024) * math.Pow10(-1)}, // '* Pow10' and '/ float(10)' do not round the same way + {decQuantity(7*1024*1024, -8, BinarySI), (7 * 1024 * 1024) / float64(100000000)}, {decQuantity(1024, 0, DecimalSI), 1024}, {decQuantity(8*1024, 0, DecimalSI), 8 * 1024}, @@ -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),