Skip to content

Commit

Permalink
fix: convert memory by quantity.Value
Browse files Browse the repository at this point in the history
  • Loading branch information
WangZzzhe authored and waynepeking348 committed May 27, 2024
1 parent 5e1814b commit 480f2d7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
8 changes: 6 additions & 2 deletions pkg/controller/overcommit/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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",
Expand Down
28 changes: 26 additions & 2 deletions pkg/controller/overcommit/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ func TestNodeOvercommitResource(t *testing.T) {
},
},
expectRes: "18720m",
expectMemRes: "35109737398272m",
expectMemRes: "35109737398",
},
{
name: "guaranteed cpu none",
Expand All @@ -635,7 +635,7 @@ func TestNodeOvercommitResource(t *testing.T) {
},
},
expectRes: "18720m",
expectMemRes: "35109737398272m",
expectMemRes: "35109737398",
},
{
name: "wrong guaranteed cpu",
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions pkg/util/native/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 480f2d7

Please sign in to comment.