From 349d78108537d883b46fc75558eac8a09990d9f3 Mon Sep 17 00:00:00 2001 From: Marcus Rodan Date: Thu, 4 Feb 2021 17:00:10 +0100 Subject: [PATCH] Fixed antiaffinity suffixing Signed-off-by: Marcus Rodan --- pkg/canary/deployment_controller.go | 11 +++++++---- pkg/canary/deployment_controller_test.go | 5 +++++ pkg/canary/deployment_fixture_test.go | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/pkg/canary/deployment_controller.go b/pkg/canary/deployment_controller.go index 463b0d8da..65afc7ca7 100644 --- a/pkg/canary/deployment_controller.go +++ b/pkg/canary/deployment_controller.go @@ -460,11 +460,11 @@ func (c *DeploymentController) getPrimaryDeploymentTemplateSpec(canaryDep *appsv if affinity := spec.Affinity; affinity != nil { if podAntiAffinity := affinity.PodAntiAffinity; podAntiAffinity != nil { for _, preferredAntiAffinity := range podAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution { - c.appendPrimarySuffixToValuesIfNeeded(preferredAntiAffinity.PodAffinityTerm.LabelSelector) + c.appendPrimarySuffixToValuesIfNeeded(preferredAntiAffinity.PodAffinityTerm.LabelSelector, canaryDep) } for _, requiredAntiAffinity := range podAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution { - c.appendPrimarySuffixToValuesIfNeeded(requiredAntiAffinity.LabelSelector) + c.appendPrimarySuffixToValuesIfNeeded(requiredAntiAffinity.LabelSelector, canaryDep) } } } @@ -472,12 +472,15 @@ func (c *DeploymentController) getPrimaryDeploymentTemplateSpec(canaryDep *appsv return spec } -func (c *DeploymentController) appendPrimarySuffixToValuesIfNeeded(labelSelector *metav1.LabelSelector) { +func (c *DeploymentController) appendPrimarySuffixToValuesIfNeeded(labelSelector *metav1.LabelSelector, canaryDep *appsv1.Deployment) { if labelSelector != nil { for _, matchExpression := range labelSelector.MatchExpressions { if contains(c.labels, matchExpression.Key) { for i := range matchExpression.Values { - matchExpression.Values[i] += "-primary" + if matchExpression.Values[i] == canaryDep.Name { + matchExpression.Values[i] += "-primary" + break + } } } } diff --git a/pkg/canary/deployment_controller_test.go b/pkg/canary/deployment_controller_test.go index decf006a9..e90198853 100644 --- a/pkg/canary/deployment_controller_test.go +++ b/pkg/canary/deployment_controller_test.go @@ -19,6 +19,7 @@ package canary import ( "context" "fmt" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -274,8 +275,12 @@ func TestDeploymentController_AntiAffinity(t *testing.T) { value := depPrimary.Spec.Template.Spec.Affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm.LabelSelector.MatchExpressions[0].Values[0] assert.Equal(t, "podinfo-primary", value) + value = depPrimary.Spec.Template.Spec.Affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution[1].PodAffinityTerm.LabelSelector.MatchExpressions[0].Values[0] + assert.False(t, strings.HasSuffix(value, "-primary")) value = depPrimary.Spec.Template.Spec.Affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].LabelSelector.MatchExpressions[0].Values[0] assert.Equal(t, "podinfo-primary", value) + value = depPrimary.Spec.Template.Spec.Affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution[1].LabelSelector.MatchExpressions[0].Values[0] + assert.False(t, strings.HasSuffix(value, "-primary")) }) } diff --git a/pkg/canary/deployment_fixture_test.go b/pkg/canary/deployment_fixture_test.go index 05bc19590..14e5eff76 100644 --- a/pkg/canary/deployment_fixture_test.go +++ b/pkg/canary/deployment_fixture_test.go @@ -571,6 +571,18 @@ func newDeploymentControllerTest(dc deploymentConfigs) *appsv1.Deployment { }, }, }, + { + PodAffinityTerm: corev1.PodAffinityTerm{ + LabelSelector: &metav1.LabelSelector{ + MatchExpressions: []metav1.LabelSelectorRequirement{ + { + Key: "app", + Values: []string{"arbitrary-app"}, + }, + }, + }, + }, + }, }, RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{ { @@ -583,6 +595,16 @@ func newDeploymentControllerTest(dc deploymentConfigs) *appsv1.Deployment { }, }, }, + { + LabelSelector: &metav1.LabelSelector{ + MatchExpressions: []metav1.LabelSelectorRequirement{ + { + Key: "app", + Values: []string{"arbitrary-app"}, + }, + }, + }, + }, }, }, },