Skip to content

Commit 480f2d7

Browse files
WangZzzhewaynepeking348
authored andcommitted
fix: convert memory by quantity.Value
1 parent 5e1814b commit 480f2d7

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

pkg/controller/overcommit/node/node.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ func (nc *NodeOvercommitController) nodeOvercommitResource(
555555
klog.V(5).Infof("node %s annotation %s missing", node.Name, originalCapacityKey)
556556
return "", ""
557557
}
558+
558559
nodeAllocatable, err := resource.ParseQuantity(nodeAllocatableAnnotation)
559560
if err != nil {
560561
klog.Error(err)
@@ -577,10 +578,13 @@ func (nc *NodeOvercommitController) nodeOvercommitResource(
577578

578579
guaranteedQuantity := resource.NewQuantity(int64(guaranteedResource), resource.DecimalSI)
579580
nodeAllocatable.Sub(*guaranteedQuantity)
580-
nodeAllocatable = native.MultiplyMilliQuantity(nodeAllocatable, overcommitRatio)
581+
// Using quantity.Value may lead to a loss of precision, but it can cover larger values than MilliValue.
582+
// memory is converted to int64 using quantity.Value in the cache of kube-scheduler,
583+
// adopt the same approach here.
584+
nodeAllocatable = native.MultiplyResourceQuantity(resourceName, nodeAllocatable, overcommitRatio)
581585
nodeAllocatable.Add(*guaranteedQuantity)
582586
nodeCapacity.Sub(*guaranteedQuantity)
583-
nodeCapacity = native.MultiplyMilliQuantity(nodeCapacity, overcommitRatio)
587+
nodeCapacity = native.MultiplyResourceQuantity(resourceName, nodeCapacity, overcommitRatio)
584588
nodeCapacity.Add(*guaranteedQuantity)
585589

586590
klog.V(5).Infof("node %s overcommitRatio: %v, guaranteedResource: %v, final allocatable: %v, capacity: %v",

pkg/controller/overcommit/node/node_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ func TestNodeOvercommitResource(t *testing.T) {
608608
},
609609
},
610610
expectRes: "18720m",
611-
expectMemRes: "35109737398272m",
611+
expectMemRes: "35109737398",
612612
},
613613
{
614614
name: "guaranteed cpu none",
@@ -635,7 +635,7 @@ func TestNodeOvercommitResource(t *testing.T) {
635635
},
636636
},
637637
expectRes: "18720m",
638-
expectMemRes: "35109737398272m",
638+
expectMemRes: "35109737398",
639639
},
640640
{
641641
name: "wrong guaranteed cpu",
@@ -714,6 +714,30 @@ func TestNodeOvercommitResource(t *testing.T) {
714714
expectRes: "",
715715
expectMemRes: "",
716716
},
717+
{
718+
name: "large memory allocatable",
719+
cpuOvercommit: "1.2",
720+
memOvercommit: "2",
721+
kcnr: &v1alpha12.CustomNodeResource{
722+
ObjectMeta: metav1.ObjectMeta{
723+
Name: "testNode1",
724+
Annotations: map[string]string{},
725+
},
726+
},
727+
node: &corev1.Node{
728+
ObjectMeta: metav1.ObjectMeta{
729+
Name: "testNode1",
730+
Annotations: map[string]string{
731+
"katalyst.kubewharf.io/original_allocatable_cpu": "15600m",
732+
"katalyst.kubewharf.io/original_capacity_cpu": "16000m",
733+
"katalyst.kubewharf.io/original_allocatable_memory": "1Ei",
734+
"katalyst.kubewharf.io/original_capacity_memory": "1Ei",
735+
},
736+
},
737+
},
738+
expectRes: "18720m",
739+
expectMemRes: "2Ei",
740+
},
717741
}
718742

719743
for _, tc := range testCases {

pkg/util/native/resources.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ func MultiplyMilliQuantity(quantity resource.Quantity, y float64) resource.Quant
300300
if 0 == y {
301301
return *resource.NewMilliQuantity(0, quantity.Format)
302302
}
303+
if 1 == y {
304+
return quantity
305+
}
303306

304307
milliValue := quantity.MilliValue()
305308
if 0 == milliValue {
@@ -315,6 +318,9 @@ func MultiplyQuantity(quantity resource.Quantity, y float64) resource.Quantity {
315318
if 0 == y {
316319
return *resource.NewQuantity(0, quantity.Format)
317320
}
321+
if 1 == y {
322+
return quantity
323+
}
318324

319325
value := quantity.Value()
320326
if 0 == value {

0 commit comments

Comments
 (0)