Added suffix -primary to antiaffinity values

Signed-off-by: Marcus Rodan <marcus.rodan@sinch.com>
This commit is contained in:
Marcus Rodan
2021-01-26 19:27:50 +01:00
parent 4f54901d08
commit 85af1abb26
3 changed files with 71 additions and 0 deletions
+24
View File
@@ -438,5 +438,29 @@ func (ct *ConfigTracker) ApplyPrimaryConfigs(spec corev1.PodSpec, refs map[strin
}
}
// update affinity
if affinity := spec.Affinity; affinity != nil {
if podAntiAffinity := affinity.PodAntiAffinity; podAntiAffinity != nil {
for _, preferredAntiAffinity := range podAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution {
term := preferredAntiAffinity.PodAffinityTerm
appendPrimarySuffixToValuesIfNeeded(term.TopologyKey, term.LabelSelector)
}
for _, requiredAntiAffinity := range podAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution {
appendPrimarySuffixToValuesIfNeeded(requiredAntiAffinity.TopologyKey, requiredAntiAffinity.LabelSelector)
}
}
}
return spec
}
func appendPrimarySuffixToValuesIfNeeded(topologyKey string, labelSelector *metav1.LabelSelector) {
if labelSelector != nil && topologyKey == "failure-domain.beta.kubernetes.io/zone" {
for _, matchExpression := range labelSelector.MatchExpressions {
for i := range matchExpression.Values {
matchExpression.Values[i] += "-primary"
}
}
}
}
+17
View File
@@ -39,6 +39,23 @@ func TestConfigIsDisabled(t *testing.T) {
}
}
func TestConfigTracker_AntiAffinity(t *testing.T) {
t.Run("deployment", func(t *testing.T) {
dc := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
mocks := newDeploymentFixture(dc)
mocks.initializeCanary(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]
assert.Equal(t, "podinfo-primary", value)
value = depPrimary.Spec.Template.Spec.Affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].LabelSelector.MatchExpressions[0].Values[0]
assert.Equal(t, "podinfo-primary", value)
})
}
func TestConfigTracker_ConfigMaps(t *testing.T) {
t.Run("deployment", func(t *testing.T) {
dc := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
+30
View File
@@ -556,6 +556,36 @@ func newDeploymentControllerTest(dc deploymentConfigs) *appsv1.Deployment {
},
},
},
Affinity: &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
{
PodAffinityTerm: corev1.PodAffinityTerm{
TopologyKey: "failure-domain.beta.kubernetes.io/zone",
LabelSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Values: []string{"podinfo"},
},
},
},
},
},
},
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
{
TopologyKey: "failure-domain.beta.kubernetes.io/zone",
LabelSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Values: []string{"podinfo"},
},
},
},
},
},
},
},
},
},
},