diff --git a/go.mod b/go.mod index 9cc47b89..a22ca525 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/score/disruptionbudget/disruptionbudget.go b/score/disruptionbudget/disruptionbudget.go index 4b29c20c..b0101274 100644 --- a/score/disruptionbudget/disruptionbudget.go +++ b/score/disruptionbudget/disruptionbudget.go @@ -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 { @@ -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 { diff --git a/score/disruptionbudget/disruptionbudget_test.go b/score/disruptionbudget/disruptionbudget_test.go new file mode 100644 index 00000000..7fe7bb09 --- /dev/null +++ b/score/disruptionbudget/disruptionbudget_test.go @@ -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 +}