Skip to content

Commit

Permalink
score/apps: warn for missing anti affinity when replicas is not expli…
Browse files Browse the repository at this point in the history
…citly set
  • Loading branch information
zegl committed Jan 9, 2019
1 parent 17595cc commit 8ad94b1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions score/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions score/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ kind: StatefulSet
metadata:
name: statefulset-host-antiaffinity-1-replica
spec:
replicas: 1
template:
metadata:
labels:
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8ad94b1

Please sign in to comment.