Adds suffix for spread constraints

Added unit test assertions

Changed comment

Signed-off-by: Marcus Rodan <marcus.rodan@sinch.com>
This commit is contained in:
Marcus Rodan
2021-02-08 19:47:58 +01:00
parent 5cb343d89f
commit 8cb147920b
3 changed files with 42 additions and 5 deletions
+5
View File
@@ -456,6 +456,11 @@ func (c *DeploymentController) scale(cd *flaggerv1.Canary, replicas int32) error
func (c *DeploymentController) getPrimaryDeploymentTemplateSpec(canaryDep *appsv1.Deployment, refs map[string]ConfigRef) corev1.PodSpec {
spec := c.configTracker.ApplyPrimaryConfigs(canaryDep.Spec.Template.Spec, refs)
// update TopologySpreadConstraints
for _, topologySpreadConstraint := range spec.TopologySpreadConstraints {
c.appendPrimarySuffixToValuesIfNeeded(topologySpreadConstraint.LabelSelector, canaryDep)
}
// update affinity
if affinity := spec.Affinity; affinity != nil {
if podAntiAffinity := affinity.PodAntiAffinity; podAntiAffinity != nil {
+15 -5
View File
@@ -264,7 +264,7 @@ func TestDeploymentController_Finalize(t *testing.T) {
}
}
func TestDeploymentController_AntiAffinity(t *testing.T) {
func TestDeploymentController_AntiAffinityAndTopologySpreadConstraints(t *testing.T) {
t.Run("deployment", func(t *testing.T) {
dc := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
mocks := newDeploymentFixture(dc)
@@ -273,14 +273,24 @@ func TestDeploymentController_AntiAffinity(t *testing.T) {
depPrimary, err := mocks.kubeClient.AppsV1().Deployments("default").Get(context.TODO(), "podinfo-primary", metav1.GetOptions{})
require.NoError(t, err)
value := depPrimary.Spec.Template.Spec.Affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm.LabelSelector.MatchExpressions[0].Values[0]
spec := depPrimary.Spec.Template.Spec
preferredConstraints := spec.Affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution
value := preferredConstraints[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]
value = preferredConstraints[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]
requiredConstraints := spec.Affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution
value = requiredConstraints[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]
value = requiredConstraints[1].LabelSelector.MatchExpressions[0].Values[0]
assert.False(t, strings.HasSuffix(value, "-primary"))
topologySpreadConstraints := spec.TopologySpreadConstraints
value = topologySpreadConstraints[0].LabelSelector.MatchExpressions[0].Values[0]
assert.Equal(t, "podinfo-primary", value)
value = topologySpreadConstraints[1].LabelSelector.MatchExpressions[0].Values[0]
assert.False(t, strings.HasSuffix(value, "-primary"))
})
}
+22
View File
@@ -616,6 +616,28 @@ func newDeploymentControllerTest(dc deploymentConfigs) *appsv1.Deployment {
},
},
},
TopologySpreadConstraints: []corev1.TopologySpreadConstraint{
{
LabelSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "app",
Values: []string{"podinfo"},
},
},
},
},
{
LabelSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "app",
Values: []string{"arbitrary-app"},
},
},
},
},
},
},
},
},