Skip to content

Commit

Permalink
cronjob: add a check for having startingDeadlineSeconds set
Browse files Browse the repository at this point in the history
  • Loading branch information
zegl committed Dec 5, 2018
1 parent cfdb5d9 commit fdcdfd4
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
21 changes: 21 additions & 0 deletions score/cronjob/cronjob.go
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions score/cronjob_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 15 additions & 0 deletions score/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -74,6 +75,7 @@ type score struct {
deployments []appsv1.Deployment
statefulsets []appsv1.StatefulSet
ingresses []extensionsv1beta1.Ingress
cronjobs []batchv1beta1.CronJob
}

type detectKind struct {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
18 changes: 18 additions & 0 deletions score/testdata/cronjob-deadline-not-set.yaml
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions score/testdata/cronjob-deadline-set.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fdcdfd4

Please sign in to comment.