Skip to content

Commit

Permalink
score/disruptionbudget: skip test if replicas is explicitly less than 2
Browse files Browse the repository at this point in the history
  • Loading branch information
zegl committed Jan 9, 2019
1 parent 8ad94b1 commit a360ac9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.3
github.com/stretchr/testify v1.3.0
golang.org/x/net v0.0.0-20181220203305-927f97764cc3 // indirect
Expand Down
12 changes: 12 additions & 0 deletions score/disruptionbudget/disruptionbudget.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func hasMatching(budgets []policyv1beta1.PodDisruptionBudget, namespace string,

func statefulSetHas(budgets []policyv1beta1.PodDisruptionBudget) func(appsv1.StatefulSet) scorecard.TestScore {
return func(statefulset appsv1.StatefulSet) (score scorecard.TestScore) {
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
}

if hasMatching(budgets, statefulset.Namespace, statefulset.Spec.Template.Labels) {
score.Grade = scorecard.GradeAllOK
} else {
Expand All @@ -50,6 +56,12 @@ func statefulSetHas(budgets []policyv1beta1.PodDisruptionBudget) func(appsv1.Sta

func deploymentHas(budgets []policyv1beta1.PodDisruptionBudget) func(appsv1.Deployment) scorecard.TestScore {
return func(deployment appsv1.Deployment) (score scorecard.TestScore) {
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
}

if hasMatching(budgets, deployment.Namespace, deployment.Spec.Template.Labels) {
score.Grade = scorecard.GradeAllOK
} else {
Expand Down
52 changes: 52 additions & 0 deletions score/disruptionbudget/disruptionbudget_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package disruptionbudget

import (
"github.com/stretchr/testify/assert"
"github.com/zegl/kube-score/scorecard"
appsv1 "k8s.io/api/apps/v1"
"testing"
)

func TestStatefulSetReplicas(t *testing.T) {
cases := map[*int32]scorecard.Grade{
nil: scorecard.GradeCritical, // failed
intptr(1): scorecard.GradeAllOK, // skipped
intptr(10): scorecard.GradeCritical, // failed
}

fn := statefulSetHas(nil)

for replicas, expected := range cases {
res := fn(appsv1.StatefulSet{Spec: appsv1.StatefulSetSpec{Replicas: replicas}})

if replicas == nil {
assert.Equal(t, expected, res.Grade, "replicas=nil")
} else {
assert.Equal(t, expected, res.Grade, "replicas=%+v", *replicas)
}
}
}

func TestDeploymentReplicas(t *testing.T) {
cases := map[*int32]scorecard.Grade{
nil: scorecard.GradeCritical, // failed
intptr(1): scorecard.GradeAllOK, // skipped
intptr(10): scorecard.GradeCritical, // failed
}

fn := deploymentHas(nil)

for replicas, expected := range cases {
res := fn(appsv1.Deployment{Spec: appsv1.DeploymentSpec{Replicas: replicas}})

if replicas == nil {
assert.Equal(t, expected, res.Grade, "replicas=nil")
} else {
assert.Equal(t, expected, res.Grade, "replicas=%+v", *replicas)
}
}
}

func intptr(a int32) *int32 {
return &a
}

0 comments on commit a360ac9

Please sign in to comment.