Repository: zegl/kube-score (go)

Issue:
Warn if startingDeadlineSeconds is not set on CronJobs

Pull request:
cronjob: add a check for having startingDeadlineSeconds set
This fixes #67

Patch:
diff --git a/score/cronjob/cronjob.go b/score/cronjob/cronjob.go
new file mode 100644
index 0000000..6e7bcac
--- /dev/null
+++ b/score/cronjob/cronjob.go
@@ -0,0 +1,21 @@
+package cronjob
+
+import (
+	"github.com/zegl/kube-score/scorecard"
+	"k8s.io/api/batch/v1beta1"
+)
+
+func ScoreCronJobHasDeadline(job v1beta1.CronJob) (score scorecard.TestScore) {
+	score.Name = "CronJob has deadline"
+	score.ID = "cronjob-has-deadline"
+
+	if job.Spec.StartingDeadlineSeconds == nil {
+		score.Grade = scorecard.GradeCritical
+		score.AddComment("", "The CronJob should have startingDeadlineSeconds configured",
+			"This makes sure that jobs are automatically cancelled if they can not be scheduler")
+		return
+	}
+
+	score.Grade = scorecard.GradeAllOK
+	return
+}
diff --git a/score/score.go b/score/score.go
index 3268006..38e90e6 100644
--- a/score/score.go
+++ b/score/score.go
@@ -4,6 +4,7 @@ import (
 	"bytes"
 	ks "github.com/zegl/kube-score"
 	"github.com/zegl/kube-score/score/container"
+	"github.com/zegl/kube-score/score/cronjob"
 	"github.com/zegl/kube-score/score/disruptionbudget"
 	"github.com/zegl/kube-score/score/ingress"
 	"github.com/zegl/kube-score/score/internal"
@@ -74,6 +75,7 @@ type score struct {
 	deployments          []appsv1.Deployment
 	statefulsets         []appsv1.StatefulSet
 	ingresses            []extensionsv1beta1.Ingress
+	cronjobs             []batchv1beta1.CronJob
 }
 
 type detectKind struct {
@@ -142,6 +144,7 @@ func Score(config Configuration) (*scorecard.Scorecard, error) {
 				var cronjob batchv1beta1.CronJob
 				decode(fileContents, &cronjob)
 				addPodSpeccer(internal.Batchv1beta1CronJob{cronjob})
+				s.cronjobs = append(s.cronjobs, cronjob)
 
 			case appsv1.SchemeGroupVersion.WithKind("Deployment"):
 				var deployment appsv1.Deployment
@@ -262,6 +265,10 @@ func (s *score) runTests() (*scorecard.Scorecard, error) {
 		ingress.ScoreIngressTargetsService(s.services),
 	}
 
+	cronjobTests := []func(batchv1beta1.CronJob) scorecard.TestScore{
+		cronjob.ScoreCronJobHasDeadline,
+	}
+
 	scoreCard := scorecard.New()
 
 	for _, meta := range s.typeMetas {
@@ -331,5 +338,13 @@ func (s *score) runTests() (*scorecard.Scorecard, error) {
 		}
 	}
 
+	for _, cjob := range s.cronjobs {
+		for _, cronjobTest := range cronjobTests {
+			score := cronjobTest(cjob)
+			score.AddMeta(cjob.TypeMeta, cjob.ObjectMeta)
+			scoreCard.Add(score)
+		}
+	}
+
 	return scoreCard, nil
 }


Test patch:
diff --git a/score/cronjob_test.go b/score/cronjob_test.go
new file mode 100644
index 0000000..0acedd8
--- /dev/null
+++ b/score/cronjob_test.go
@@ -0,0 +1,13 @@
+package score
+
+import (
+	"testing"
+)
+
+func TestCronJobHasDeadline(t *testing.T) {
+	testExpectedScore(t, "cronjob-deadline-set.yaml", "CronJob has deadline", 10)
+}
+
+func TestCronJobNotHasDeadline(t *testing.T) {
+	testExpectedScore(t, "cronjob-deadline-not-set.yaml", "CronJob has deadline", 1)
+}
diff --git a/score/testdata/cronjob-deadline-not-set.yaml b/score/testdata/cronjob-deadline-not-set.yaml
new file mode 100644
index 0000000..64259ad
--- /dev/null
+++ b/score/testdata/cronjob-deadline-not-set.yaml
@@ -0,0 +1,18 @@
+apiVersion: batch/v1beta1
+kind: CronJob
+metadata:
+  name: hello
+spec:
+  schedule: "*/1 * * * *"
+  jobTemplate:
+    spec:
+      template:
+        spec:
+          containers:
+            - name: hello
+              image: busybox
+              args:
+                - /bin/sh
+                - -c
+                - date; echo Hello from the Kubernetes cluster
+          restartPolicy: OnFailure
\ No newline at end of file
diff --git a/score/testdata/cronjob-deadline-set.yaml b/score/testdata/cronjob-deadline-set.yaml
new file mode 100644
index 0000000..9f0da61
--- /dev/null
+++ b/score/testdata/cronjob-deadline-set.yaml
@@ -0,0 +1,19 @@
+apiVersion: batch/v1beta1
+kind: CronJob
+metadata:
+  name: hello
+spec:
+  schedule: "*/1 * * * *"
+  startingDeadlineSeconds: 100
+  jobTemplate:
+    spec:
+      template:
+        spec:
+          containers:
+            - name: hello
+              image: busybox
+              args:
+                - /bin/sh
+                - -c
+                - date; echo Hello from the Kubernetes cluster
+          restartPolicy: OnFailure
\ No newline at end of file