From ce455b7af9f1e41e7697d65051da36bbf1addee2 Mon Sep 17 00:00:00 2001 From: jiangpengcheng Date: Mon, 6 May 2024 19:13:41 +0800 Subject: [PATCH] Fix ci --- .ci/helm.sh | 2 +- .../cases/mongodb-source/verify.sh | 2 +- controllers/common.go | 26 +++++++++---------- controllers/common_test.go | 9 +++++++ controllers/spec/vpa.go | 26 ++++++++----------- 5 files changed, 35 insertions(+), 30 deletions(-) diff --git a/.ci/helm.sh b/.ci/helm.sh index 4acd788e..febbfc7d 100644 --- a/.ci/helm.sh +++ b/.ci/helm.sh @@ -489,7 +489,7 @@ function ci::verify_elasticsearch_sink() { function ci::verify_mongodb_source() { timesleep=$1 - kubectl exec mongo-dbz-0 -c mongo -- mongo -u debezium -p dbz --authenticationDatabase admin localhost:27017/inventory --eval 'db.products.update({"_id":NumberLong(104)},{$set:{weight:1.25}})' + kubectl exec mongo-dbz-0 -c mongo -- mongosh -u debezium -p dbz --authenticationDatabase admin localhost:27017/inventory --eval 'db.products.update({"_id":NumberLong(104)},{$set:{weight:1.25}})' sleep "$timesleep" kubectl logs --tail=-1 -l compute.functionmesh.io/name=source-sample | grep "records sent" if [ $? -eq 0 ]; then diff --git a/.ci/tests/integration/cases/mongodb-source/verify.sh b/.ci/tests/integration/cases/mongodb-source/verify.sh index 2d9c0b38..6065d802 100644 --- a/.ci/tests/integration/cases/mongodb-source/verify.sh +++ b/.ci/tests/integration/cases/mongodb-source/verify.sh @@ -45,7 +45,7 @@ function install_mongodb_server() { # install mongodb server kubectl apply -f "${mongodb_file}" num=0 - while [[ ${num} -lt 3 ]]; do + while [[ ${num} -lt 1 ]]; do sleep 5 kubectl get pods num=$(kubectl get pods -l role=mongo | wc -l) diff --git a/controllers/common.go b/controllers/common.go index 636848b5..b2c9b4a5 100644 --- a/controllers/common.go +++ b/controllers/common.go @@ -121,9 +121,6 @@ func observeVPA(ctx context.Context, r client.Reader, name types.NamespacedName, spec.UpdateVPAUpdatePolicy(updatePolicy, vpaSpec.ResourceUnit) resourcePolicy := vpaSpec.ResourcePolicy containerName := spec.GetVPAContainerName(&vpa.ObjectMeta) - if resourcePolicy == nil { - resourcePolicy = &vpav1.PodResourcePolicy{} - } spec.UpdateResourcePolicy(resourcePolicy, containerName) if !reflect.DeepEqual(updatePolicy, vpa.Spec.UpdatePolicy) || !reflect.DeepEqual(resourcePolicy, vpa.Spec.ResourcePolicy) { @@ -147,18 +144,21 @@ func calculateVPARecommendation(vpa *vpav1.VerticalPodAutoscaler, vpaSpec *v1alp if vpaSpec.ResourceUnit == nil || vpaSpec.ResourceUnit.CPU.MilliValue() == 0 && vpaSpec.ResourceUnit.Memory.MilliValue() == 0 { return nil } + containerName := spec.GetVPAContainerName(&vpa.ObjectMeta) if vpa.Status.Recommendation != nil && vpa.Status.Recommendation.ContainerRecommendations != nil { for _, recommend := range vpa.Status.Recommendation.ContainerRecommendations { - // set resource based on CPU - if recommend.Target.Cpu() != nil && recommend.Target.Cpu().Value() != 0 { - multiple = recommend.Target.Cpu().MilliValue() / vpaSpec.ResourceUnit.CPU.MilliValue() - 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().MilliValue()%vpaSpec.ResourceUnit.Memory.MilliValue() != 0 { - multiple += 1 + if containerName == recommend.ContainerName { + // set resource based on CPU + if recommend.Target.Cpu() != nil && recommend.Target.Cpu().Value() != 0 { + multiple = recommend.Target.Cpu().MilliValue() / vpaSpec.ResourceUnit.CPU.MilliValue() + 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().MilliValue()%vpaSpec.ResourceUnit.Memory.MilliValue() != 0 { + multiple += 1 + } } } } diff --git a/controllers/common_test.go b/controllers/common_test.go index 09531eeb..89578dfa 100644 --- a/controllers/common_test.go +++ b/controllers/common_test.go @@ -47,6 +47,7 @@ func Test_calculateVPARecommendation(t *testing.T) { Recommendation: &vpav1.RecommendedPodResources{ ContainerRecommendations: []vpav1.RecommendedContainerResources{ { + ContainerName: "*", Target: corev1.ResourceList{ corev1.ResourceCPU: resource.MustParse("0.2"), corev1.ResourceMemory: resource.MustParse("1Gi"), @@ -82,6 +83,7 @@ func Test_calculateVPARecommendation(t *testing.T) { Recommendation: &vpav1.RecommendedPodResources{ ContainerRecommendations: []vpav1.RecommendedContainerResources{ { + ContainerName: "*", Target: corev1.ResourceList{ corev1.ResourceCPU: resource.MustParse("100m"), corev1.ResourceMemory: resource.MustParse("1Gi"), @@ -117,6 +119,7 @@ func Test_calculateVPARecommendation(t *testing.T) { Recommendation: &vpav1.RecommendedPodResources{ ContainerRecommendations: []vpav1.RecommendedContainerResources{ { + ContainerName: "*", Target: corev1.ResourceList{ corev1.ResourceCPU: resource.MustParse("400m"), corev1.ResourceMemory: resource.MustParse("1Gi"), @@ -152,6 +155,7 @@ func Test_calculateVPARecommendation(t *testing.T) { Recommendation: &vpav1.RecommendedPodResources{ ContainerRecommendations: []vpav1.RecommendedContainerResources{ { + ContainerName: "*", Target: corev1.ResourceList{ corev1.ResourceCPU: resource.MustParse("0.45"), corev1.ResourceMemory: resource.MustParse("1Gi"), @@ -187,6 +191,7 @@ func Test_calculateVPARecommendation(t *testing.T) { Recommendation: &vpav1.RecommendedPodResources{ ContainerRecommendations: []vpav1.RecommendedContainerResources{ { + ContainerName: "*", Target: corev1.ResourceList{ corev1.ResourceMemory: resource.MustParse("800Mi"), }, @@ -221,6 +226,7 @@ func Test_calculateVPARecommendation(t *testing.T) { Recommendation: &vpav1.RecommendedPodResources{ ContainerRecommendations: []vpav1.RecommendedContainerResources{ { + ContainerName: "*", Target: corev1.ResourceList{ corev1.ResourceMemory: resource.MustParse("629145600"), // 600Mi }, @@ -255,6 +261,7 @@ func Test_calculateVPARecommendation(t *testing.T) { Recommendation: &vpav1.RecommendedPodResources{ ContainerRecommendations: []vpav1.RecommendedContainerResources{ { + ContainerName: "*", Target: corev1.ResourceList{ corev1.ResourceMemory: resource.MustParse("1600Mi"), }, @@ -289,6 +296,7 @@ func Test_calculateVPARecommendation(t *testing.T) { Recommendation: &vpav1.RecommendedPodResources{ ContainerRecommendations: []vpav1.RecommendedContainerResources{ { + ContainerName: "*", Target: corev1.ResourceList{ corev1.ResourceMemory: resource.MustParse("1.7Gi"), }, @@ -323,6 +331,7 @@ func Test_calculateVPARecommendation(t *testing.T) { Recommendation: &vpav1.RecommendedPodResources{ ContainerRecommendations: []vpav1.RecommendedContainerResources{ { + ContainerName: "*", Target: corev1.ResourceList{ corev1.ResourceCPU: resource.MustParse("200m"), corev1.ResourceMemory: resource.MustParse("1Gi"), diff --git a/controllers/spec/vpa.go b/controllers/spec/vpa.go index c516e8ed..f95d0799 100644 --- a/controllers/spec/vpa.go +++ b/controllers/spec/vpa.go @@ -34,9 +34,6 @@ func MakeVPA(objectMeta *metav1.ObjectMeta, targetRef *autov2.CrossVersionObject objectMeta.Labels[LabelCustomResourceUnit] = "true" } resourcePolicy := vpa.ResourcePolicy - if resourcePolicy == nil { - resourcePolicy = &vpav1.PodResourcePolicy{} - } UpdateResourcePolicy(resourcePolicy, containerName) return &vpav1.VerticalPodAutoscaler{ @@ -78,9 +75,9 @@ func UpdateVPAUpdatePolicy(updatePolicy *vpav1.PodUpdatePolicy, resourceUnit *v1 } func UpdateResourcePolicy(resourcePolicy *vpav1.PodResourcePolicy, containerName string) { - var containerPolicies []vpav1.ContainerResourcePolicy - containerScalingMode := vpav1.ContainerScalingModeAuto if resourcePolicy != nil { + var containerPolicies []vpav1.ContainerResourcePolicy + containerScalingMode := vpav1.ContainerScalingModeAuto if resourcePolicy.ContainerPolicies == nil { resourcePolicy.ContainerPolicies = []vpav1.ContainerResourcePolicy{} } @@ -91,16 +88,15 @@ func UpdateResourcePolicy(resourcePolicy *vpav1.PodResourcePolicy, containerName break } } - } - - // if resource policy is not set, set the default policy, so the vpa policy won't be applied to other containers - if len(containerPolicies) == 0 { - containerPolicies = []vpav1.ContainerResourcePolicy{ - { - ContainerName: containerName, - Mode: &containerScalingMode, - }, + // if resource policy is not set, set the default policy, so the vpa policy won't be applied to other containers + if len(containerPolicies) == 0 { + containerPolicies = []vpav1.ContainerResourcePolicy{ + { + ContainerName: containerName, + Mode: &containerScalingMode, + }, + } } + resourcePolicy.ContainerPolicies = containerPolicies } - resourcePolicy.ContainerPolicies = containerPolicies }