Skip to content

Commit

Permalink
score/apps: support more topology keys
Browse files Browse the repository at this point in the history
Adds support for additional “approved” topology keys in the hasPodAntiAffinity score

--- sturdy ---
user_id: 847dfd0c-49bf-40c5-8870-74a12fca0d60
view_id: 72cdb3b4-8d5d-4357-aa8c-3d10e910f7af
workspace_id: ec482ab3-1b84-47fc-8b77-3974d49c7dbb
  • Loading branch information
zegl committed Oct 1, 2021
1 parent 5b64b0d commit 188bca6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
20 changes: 15 additions & 5 deletions score/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,31 @@ 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
}
}
}
}

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
}
}
Expand Down
60 changes: 60 additions & 0 deletions score/apps/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 188bca6

Please sign in to comment.