From 480f2d739097fccb1e4e4a609d51638f08f7a79a Mon Sep 17 00:00:00 2001 From: "wangzhe.21" Date: Tue, 21 May 2024 15:42:24 +0800 Subject: [PATCH] fix: convert memory by quantity.Value --- pkg/controller/overcommit/node/node.go | 8 ++++-- pkg/controller/overcommit/node/node_test.go | 28 +++++++++++++++++++-- pkg/util/native/resources.go | 6 +++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/pkg/controller/overcommit/node/node.go b/pkg/controller/overcommit/node/node.go index 9cef94f93..af1ba34fa 100644 --- a/pkg/controller/overcommit/node/node.go +++ b/pkg/controller/overcommit/node/node.go @@ -555,6 +555,7 @@ func (nc *NodeOvercommitController) nodeOvercommitResource( klog.V(5).Infof("node %s annotation %s missing", node.Name, originalCapacityKey) return "", "" } + nodeAllocatable, err := resource.ParseQuantity(nodeAllocatableAnnotation) if err != nil { klog.Error(err) @@ -577,10 +578,13 @@ func (nc *NodeOvercommitController) nodeOvercommitResource( guaranteedQuantity := resource.NewQuantity(int64(guaranteedResource), resource.DecimalSI) nodeAllocatable.Sub(*guaranteedQuantity) - nodeAllocatable = native.MultiplyMilliQuantity(nodeAllocatable, overcommitRatio) + // Using quantity.Value may lead to a loss of precision, but it can cover larger values than MilliValue. + // memory is converted to int64 using quantity.Value in the cache of kube-scheduler, + // adopt the same approach here. + nodeAllocatable = native.MultiplyResourceQuantity(resourceName, nodeAllocatable, overcommitRatio) nodeAllocatable.Add(*guaranteedQuantity) nodeCapacity.Sub(*guaranteedQuantity) - nodeCapacity = native.MultiplyMilliQuantity(nodeCapacity, overcommitRatio) + nodeCapacity = native.MultiplyResourceQuantity(resourceName, nodeCapacity, overcommitRatio) nodeCapacity.Add(*guaranteedQuantity) klog.V(5).Infof("node %s overcommitRatio: %v, guaranteedResource: %v, final allocatable: %v, capacity: %v", diff --git a/pkg/controller/overcommit/node/node_test.go b/pkg/controller/overcommit/node/node_test.go index 3391fed75..b3d3143de 100644 --- a/pkg/controller/overcommit/node/node_test.go +++ b/pkg/controller/overcommit/node/node_test.go @@ -608,7 +608,7 @@ func TestNodeOvercommitResource(t *testing.T) { }, }, expectRes: "18720m", - expectMemRes: "35109737398272m", + expectMemRes: "35109737398", }, { name: "guaranteed cpu none", @@ -635,7 +635,7 @@ func TestNodeOvercommitResource(t *testing.T) { }, }, expectRes: "18720m", - expectMemRes: "35109737398272m", + expectMemRes: "35109737398", }, { name: "wrong guaranteed cpu", @@ -714,6 +714,30 @@ func TestNodeOvercommitResource(t *testing.T) { expectRes: "", expectMemRes: "", }, + { + name: "large memory allocatable", + cpuOvercommit: "1.2", + memOvercommit: "2", + kcnr: &v1alpha12.CustomNodeResource{ + ObjectMeta: metav1.ObjectMeta{ + Name: "testNode1", + Annotations: map[string]string{}, + }, + }, + node: &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "testNode1", + Annotations: map[string]string{ + "katalyst.kubewharf.io/original_allocatable_cpu": "15600m", + "katalyst.kubewharf.io/original_capacity_cpu": "16000m", + "katalyst.kubewharf.io/original_allocatable_memory": "1Ei", + "katalyst.kubewharf.io/original_capacity_memory": "1Ei", + }, + }, + }, + expectRes: "18720m", + expectMemRes: "2Ei", + }, } for _, tc := range testCases { diff --git a/pkg/util/native/resources.go b/pkg/util/native/resources.go index bb4e92f5c..b62aa8c07 100644 --- a/pkg/util/native/resources.go +++ b/pkg/util/native/resources.go @@ -300,6 +300,9 @@ func MultiplyMilliQuantity(quantity resource.Quantity, y float64) resource.Quant if 0 == y { return *resource.NewMilliQuantity(0, quantity.Format) } + if 1 == y { + return quantity + } milliValue := quantity.MilliValue() if 0 == milliValue { @@ -315,6 +318,9 @@ func MultiplyQuantity(quantity resource.Quantity, y float64) resource.Quantity { if 0 == y { return *resource.NewQuantity(0, quantity.Format) } + if 1 == y { + return quantity + } value := quantity.Value() if 0 == value {