Skip to content

Commit

Permalink
Use max multiplier
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangpengcheng committed May 9, 2024
1 parent 44140dc commit 00737e9
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 17 deletions.
34 changes: 25 additions & 9 deletions .ci/helm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,21 @@ function ci::verify_vpa_java_function() {
ci::verify_exclamation_function "persistent://public/default/input-vpa-java-topic" "persistent://public/default/output-vpa-java-topic" "test-message" "test-message!" 10
}

function ci::calculate_multiplies() {
base=$1
value=$2
quotient=$(($value / $base))
remainder=$(($value % $base))

# If there's any remainder, we need to round up
if [ $remainder -ne 0 ]; then
multiple=$(($quotient + 1))
else
multiple=$quotient
fi
echo $multiple
}

function ci::verify_vpa_with_resource_unit() {
name=$1
kind=$2
Expand All @@ -278,18 +293,19 @@ function ci::verify_vpa_with_resource_unit() {
kubectl wait -l name=$name --for=condition=RecommendationProvided --timeout=2m vpa
cpu=`kubectl get vpa $vpaName -o jsonpath='{.status.recommendation.containerRecommendations[0].target.cpu}'`
cpu_value=${cpu%m}
multiplier=$(ci::calculate_multiplies $baseCpu $cpu_value)

quotient=$(($cpu_value / $baseCpu))
remainder=$(($cpu_value % $baseCpu))
memory=`kubectl get vpa $vpaName -o jsonpath='{.status.recommendation.containerRecommendations[0].target.memory}'`
memory_value=${memory%k}
memory_value=$((memory_value*1024))
memoryMultiplier=$(ci::calculate_multiplies $baseMemory $memory_value)

# If there's any remainder, we need to round up
if [ $remainder -ne 0 ]; then
multiple=$(($quotient + 1))
else
multiple=$quotient
if [ $memoryMultiplier -gt $multiplier ]; then
multiplier=$memoryMultiplier
fi
targetCpu=`echo $(($baseCpu* $multiple ))m`
targetMemory=`echo $(($baseMemory * $multiple ))`

targetCpu=`echo $(($baseCpu* $multiple))m`
targetMemory=`echo $(($baseMemory * $multiple))`

resources='{"limits":{"cpu":"'$targetCpu'","memory":"'$targetMemory'"},"requests":{"cpu":"'$targetCpu'","memory":"'$targetMemory'"}}'

Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/trivy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ jobs:
with:
image-ref: 'function-mesh-operator:latest'
format: 'table'
exit-code: '1'
exit-code: '1'
severity: "MEDIUM,HIGH,CRITICAL"
11 changes: 8 additions & 3 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,15 @@ func calculateVPARecommendation(vpa *vpav1.VerticalPodAutoscaler, vpaSpec *v1alp
if recommend.Target.Cpu().MilliValue()%vpaSpec.ResourceUnit.CPU.MilliValue() != 0 {
multiple += 1
}
} else if recommend.Target.Memory() != nil { // set resources based on Memory
multiple = recommend.Target.Memory().MilliValue() / vpaSpec.ResourceUnit.Memory.MilliValue()
}
if recommend.Target.Memory() != nil && recommend.Target.Memory().Value() != 0 {
multipleMemory := recommend.Target.Memory().MilliValue() / vpaSpec.ResourceUnit.Memory.MilliValue()
if recommend.Target.Memory().MilliValue()%vpaSpec.ResourceUnit.Memory.MilliValue() != 0 {
multiple += 1
multipleMemory += 1
}
// use the larger multiple
if multipleMemory > multiple {
multiple = multipleMemory
}
}
}
Expand Down
44 changes: 40 additions & 4 deletions controllers/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Test_calculateVPARecommendation(t *testing.T) {
ContainerName: "*",
Target: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("0.2"),
corev1.ResourceMemory: resource.MustParse("1Gi"),
corev1.ResourceMemory: resource.MustParse("600Mi"),
},
},
},
Expand Down Expand Up @@ -86,7 +86,7 @@ func Test_calculateVPARecommendation(t *testing.T) {
ContainerName: "*",
Target: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("100m"),
corev1.ResourceMemory: resource.MustParse("1Gi"),
corev1.ResourceMemory: resource.MustParse("0.6Gi"),
},
},
},
Expand Down Expand Up @@ -183,6 +183,42 @@ func Test_calculateVPARecommendation(t *testing.T) {
},
},
},
{
name: "Use memory to calculate multiplier when it increase more than cpu",
args: args{
vpa: &vpav1.VerticalPodAutoscaler{
Status: vpav1.VerticalPodAutoscalerStatus{
Recommendation: &vpav1.RecommendedPodResources{
ContainerRecommendations: []vpav1.RecommendedContainerResources{
{
ContainerName: "*",
Target: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("0.2"),
corev1.ResourceMemory: resource.MustParse("1Gi"),
},
},
},
},
},
},
vpaSpec: &v1alpha1.VPASpec{
ResourceUnit: &v1alpha1.ResourceUnit{
CPU: resource.MustParse("200m"),
Memory: resource.MustParse("800Mi"),
},
},
},
want: &corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: *resource.NewScaledQuantity(400, resource.Milli),
corev1.ResourceMemory: *resource.NewScaledQuantity(2*800*1024*1024*1000, resource.Milli),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: *resource.NewScaledQuantity(400, resource.Milli),
corev1.ResourceMemory: *resource.NewScaledQuantity(2*800*1024*1024*1000, resource.Milli),
},
},
},
{
name: "Use the target memory when target memory is equal to the resource unit memory",
args: args{
Expand Down Expand Up @@ -360,8 +396,8 @@ func Test_calculateVPARecommendation(t *testing.T) {
},
want: &corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: *resource.NewScaledQuantity(200, resource.Milli),
corev1.ResourceMemory: *resource.NewScaledQuantity(800*1024*1024*1000, resource.Milli),
corev1.ResourceCPU: *resource.NewScaledQuantity(400, resource.Milli),
corev1.ResourceMemory: *resource.NewScaledQuantity(2*800*1024*1024*1000, resource.Milli),
},
},
},
Expand Down
1 change: 1 addition & 0 deletions controllers/spec/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package spec
import (
"bytes"
"context"

// used for template
_ "embed"
"encoding/json"
Expand Down

0 comments on commit 00737e9

Please sign in to comment.