Repository: zegl/kube-score
Pull request #80 (2019-01-09 16:24:11)

score/apps: warn for missing anti affinity when replicas is not expli…
…citly set


Patch:
diff --git a/score/apps/apps.go b/score/apps/apps.go
index eac97cb..11078fa 100644
--- a/score/apps/apps.go
+++ b/score/apps/apps.go
@@ -16,7 +16,9 @@ func Register(allChecks *checks.Checks) {
 
 func deploymentHasAntiAffinity(deployment appsv1.Deployment) (score scorecard.TestScore) {
 	// Ignore if the deployment only has a single replica
-	if deployment.Spec.Replicas == nil || *deployment.Spec.Replicas < 2 {
+	// If replicas is not explicitly set, we'll still warn if the anti affinity is missing
+	// as that might indicate use of a Horizontal Pod Autoscaler
+	if deployment.Spec.Replicas != nil && *deployment.Spec.Replicas < 2 {
 		score.Grade = scorecard.GradeAllOK
 		score.AddComment("", "Skipped", "Skipped because the deployment has less than 2 replicas")
 		return
@@ -46,7 +48,9 @@ func deploymentHasAntiAffinity(deployment appsv1.Deployment) (score scorecard.Te
 
 func statefulsetHasAntiAffinity(statefulset appsv1.StatefulSet) (score scorecard.TestScore) {
 	// Ignore if the statefulset only has a single replica
-	if statefulset.Spec.Replicas == nil || *statefulset.Spec.Replicas < 2 {
+	// If replicas is not explicitly set, we'll still warn if the anti affinity is missing
+	// as that might indicate use of a Horizontal Pod Autoscaler
+	if statefulset.Spec.Replicas != nil && *statefulset.Spec.Replicas < 2 {
 		score.Grade = scorecard.GradeAllOK
 		score.AddComment("", "Skipped", "Skipped because the statefulset has less than 2 replicas")
 		return


Test patch:
diff --git a/score/apps_test.go b/score/apps_test.go
index c2e10c0..510e26c 100644
--- a/score/apps_test.go
+++ b/score/apps_test.go
@@ -43,3 +43,7 @@ func TestStatefulSetHasPodAntiAffinityNotSet(t *testing.T) {
 func TestStatefulSetHasPodAntiAffinityOneReplica(t *testing.T) {
 	testExpectedScore(t, "statefulset-host-antiaffinity-1-replica.yaml", "StatefulSet has host PodAntiAffinity", 10)
 }
+
+func TestStatefulSetHasPodAntiAffinityUndefinedReplicas(t *testing.T) {
+	testExpectedScore(t, "statefulset-host-antiaffinity-undefined-replicas.yaml", "StatefulSet has host PodAntiAffinity", 5)
+}
diff --git a/score/testdata/statefulset-host-antiaffinity-1-replica.yaml b/score/testdata/statefulset-host-antiaffinity-1-replica.yaml
index 4267dae..70b2daf 100644
--- a/score/testdata/statefulset-host-antiaffinity-1-replica.yaml
+++ b/score/testdata/statefulset-host-antiaffinity-1-replica.yaml
@@ -3,6 +3,7 @@ kind: StatefulSet
 metadata:
   name: statefulset-host-antiaffinity-1-replica
 spec:
+  replicas: 1
   template:
     metadata:
       labels:
diff --git a/score/testdata/statefulset-host-antiaffinity-undefined-replicas.yaml b/score/testdata/statefulset-host-antiaffinity-undefined-replicas.yaml
new file mode 100644
index 0000000..9679baa
--- /dev/null
+++ b/score/testdata/statefulset-host-antiaffinity-undefined-replicas.yaml
@@ -0,0 +1,13 @@
+apiVersion: apps/v1
+kind: StatefulSet
+metadata:
+  name: statefulset-host-antiaffinity-undefined-replicas
+spec:
+  template:
+    metadata:
+      labels:
+        app: foo
+    spec:
+      containers:
+      - name: foobar
+        image: foo:bar