From 188bca656e7c8f924e43f408dbe9919c2b731ce3 Mon Sep 17 00:00:00 2001 From: Gustav Westling Date: Fri, 1 Oct 2021 17:04:08 +0000 Subject: [PATCH] =?UTF-8?q?score/apps:=20support=20more=20topology=20keys?= =?UTF-8?q?=20Adds=20support=20for=20additional=20=E2=80=9Capproved?= =?UTF-8?q?=E2=80=9D=20topology=20keys=20in=20the=20hasPodAntiAffinity=20s?= =?UTF-8?q?core?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sturdy --- user_id: 847dfd0c-49bf-40c5-8870-74a12fca0d60 view_id: 72cdb3b4-8d5d-4357-aa8c-3d10e910f7af workspace_id: ec482ab3-1b84-47fc-8b77-3974d49c7dbb --- score/apps/apps.go | 20 ++++++++++---- score/apps/apps_test.go | 60 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/score/apps/apps.go b/score/apps/apps.go index 4b907bfc..1b7659bc 100644 --- a/score/apps/apps.go +++ b/score/apps/apps.go @@ -117,11 +117,21 @@ func statefulsetHasAntiAffinity(statefulset appsv1.StatefulSet) (score scorecard return } -func hasPodAntiAffinity(selfLables internal.MapLables, affinity *corev1.Affinity) bool { +func hasPodAntiAffinity(selfLabels internal.MapLables, affinity *corev1.Affinity) bool { + approvedTopologyKeys := map[string]struct{}{ + "kubernetes.io/hostname": {}, + "topology.kubernetes.io/region": {}, + "topology.kubernetes.io/zone": {}, + + // Deprecated in Kubernetes v1.17 + "failure-domain.beta.kubernetes.io/region": {}, + "failure-domain.beta.kubernetes.io/zone": {}, + } + for _, pref := range affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution { - if pref.PodAffinityTerm.TopologyKey == "kubernetes.io/hostname" { + if _, ok := approvedTopologyKeys[pref.PodAffinityTerm.TopologyKey]; ok { if selector, err := metav1.LabelSelectorAsSelector(pref.PodAffinityTerm.LabelSelector); err == nil { - if selector.Matches(internal.MapLables(selfLables)) { + if selector.Matches(selfLabels) { return true } } @@ -129,9 +139,9 @@ func hasPodAntiAffinity(selfLables internal.MapLables, affinity *corev1.Affinity } for _, req := range affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution { - if req.TopologyKey == "kubernetes.io/hostname" { + if _, ok := approvedTopologyKeys[req.TopologyKey]; ok { if selector, err := metav1.LabelSelectorAsSelector(req.LabelSelector); err == nil { - if selector.Matches(internal.MapLables(selfLables)) { + if selector.Matches(selfLabels) { return true } } diff --git a/score/apps/apps_test.go b/score/apps/apps_test.go index 9a5e1c37..1994ea05 100644 --- a/score/apps/apps_test.go +++ b/score/apps/apps_test.go @@ -52,6 +52,66 @@ func antiAffinityTestCases() []testcase { }, expectedSkipped: false, }, + { + // OK! (required) ( topology.kubernetes.io/zone ) + expectedGrade: scorecard.GradeAllOK, + replicas: i(5), + affinity: &corev1.Affinity{ + PodAntiAffinity: &corev1.PodAntiAffinity{ + RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{ + { + TopologyKey: "topology.kubernetes.io/zone", + LabelSelector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "app": "foo", + }, + }, + }, + }, + }, + }, + expectedSkipped: false, + }, + { + // OK! (required) ( topology.kubernetes.io/region ) + expectedGrade: scorecard.GradeAllOK, + replicas: i(5), + affinity: &corev1.Affinity{ + PodAntiAffinity: &corev1.PodAntiAffinity{ + RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{ + { + TopologyKey: "topology.kubernetes.io/region", + LabelSelector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "app": "foo", + }, + }, + }, + }, + }, + }, + expectedSkipped: false, + }, + { + // Not OK! (required) ( some other topology key ) + expectedGrade: scorecard.GradeWarning, + replicas: i(5), + affinity: &corev1.Affinity{ + PodAntiAffinity: &corev1.PodAntiAffinity{ + RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{ + { + TopologyKey: "topology.kubernetes.io/what-is-this-key", + LabelSelector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "app": "foo", + }, + }, + }, + }, + }, + }, + expectedSkipped: false, + }, { // OK (preferred) expectedGrade: scorecard.GradeAllOK,