Skip to content

Commit

Permalink
Fixed antiaffinity suffixing
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Rodan <[email protected]>
  • Loading branch information
marcus-rodan-sinch committed Feb 4, 2021
1 parent 118cac5 commit 349d781
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/canary/deployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,24 +460,27 @@ 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)
}
}
}

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
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/canary/deployment_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package canary
import (
"context"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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"))
})
}
22 changes: 22 additions & 0 deletions pkg/canary/deployment_fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand All @@ -583,6 +595,16 @@ func newDeploymentControllerTest(dc deploymentConfigs) *appsv1.Deployment {
},
},
},
{
LabelSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "app",
Values: []string{"arbitrary-app"},
},
},
},
},
},
},
},
Expand Down

0 comments on commit 349d781

Please sign in to comment.