Skip to content

Commit

Permalink
Merge #474
Browse files Browse the repository at this point in the history
474: Bugfix/cronjob restartpolicy check r=zegl a=kmarteaux


```
RELNOTE: Implement New Rule: CronJob resource requires Pod restartPolicy set to Never or OnFailure #471
```


Co-authored-by: Kenneth Martau <kenneth.martau@gmail.com>
  • Loading branch information
bors[bot] and kmarteaux authored Jun 1, 2022
2 parents dbc739d + eefa518 commit 9becb93
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 0 deletions.
1 change: 1 addition & 0 deletions domain/kube-score.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ type CronJob interface {
GetTypeMeta() metav1.TypeMeta
GetObjectMeta() metav1.ObjectMeta
StartingDeadlineSeconds() *int64
GetPodTemplateSpec() corev1.PodTemplateSpec
FileLocationer
}

Expand Down
24 changes: 24 additions & 0 deletions score/cronjob/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

func Register(allChecks *checks.Checks) {
allChecks.RegisterCronJobCheck("CronJob has deadline", `Makes sure that all CronJobs has a configured deadline`, cronJobHasDeadline)
allChecks.RegisterCronJobCheck("CronJob RestartPolicy", `Makes sure CronJobs have a valid RestartPolicy`, cronJobHasRestartPolicy)
}

func cronJobHasDeadline(job ks.CronJob) (score scorecard.TestScore) {
Expand All @@ -21,3 +22,26 @@ func cronJobHasDeadline(job ks.CronJob) (score scorecard.TestScore) {
score.Grade = scorecard.GradeAllOK
return
}

// CronJob restartPolicy must be "OnFailure" or "Never". It cannot be empty (unspecified)
func cronJobHasRestartPolicy(job ks.CronJob) (score scorecard.TestScore) {

podTmpl := job.GetPodTemplateSpec()
restartPolicy := podTmpl.Spec.RestartPolicy

if len(restartPolicy) > 0 {
if restartPolicy == "Never" || restartPolicy == "OnFailure" {
score.Grade = scorecard.GradeAllOK
} else {
score.Grade = scorecard.GradeCritical
score.AddComment("", "The CronJob must have a valid RestartPolicy configured",
"Valid CronJob RestartPolicy settings are Never or OnFailure")
}
} else {
score.Grade = scorecard.GradeCritical
score.AddComment("", "The CronJob is missing a valid RestartPolicy",
"Valid CronJob RestartPolicy settings are Never or OnFailure")
}

return
}
30 changes: 30 additions & 0 deletions score/cronjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,33 @@ func TestProbesPodCronMissingReady(t *testing.T) {
})
}
}

func TestCronJobHasRestartPolicyMissing(t *testing.T) {
t.Parallel()

for _, v := range []string{"batchv1beta1", "batchv1"} {
t.Run(v, func(t *testing.T) {
testExpectedScore(t, "cronjob-"+v+"-restartpolicy-not-set.yaml", "CronJob RestartPolicy", scorecard.GradeCritical)
})
}
}

func TestCronJobHasRestartPolicyInvalid(t *testing.T) {
t.Parallel()

for _, v := range []string{"batchv1beta1", "batchv1"} {
t.Run(v, func(t *testing.T) {
testExpectedScore(t, "cronjob-"+v+"-restartpolicy-invalid.yaml", "CronJob RestartPolicy", scorecard.GradeCritical)
})
}
}

func TestCronJobHasRestartPolicyValid(t *testing.T) {
t.Parallel()

for _, v := range []string{"batchv1beta1", "batchv1"} {
t.Run(v, func(t *testing.T) {
testExpectedScore(t, "cronjob-"+v+"-restartpolicy-valid.yaml", "CronJob RestartPolicy", scorecard.GradeAllOK)
})
}
}
28 changes: 28 additions & 0 deletions score/testdata/cronjob-batchv1-restartpolicy-invalid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
kind: CronJob
apiVersion: batch/v1
metadata:
namespace: cronjobs
name: pwsh-test
spec:
jobTemplate:
spec:
template:
spec:
containers:
- name: pwsh
imagePullPolicy: Always
image: mcr.microsoft.com/powershell:7
command:
- pwsh
- -Command
- Start-Sleep -Seconds 5
securityContext:
readOnlyRootFilesystem: true
resources:
limits:
ephemeral-storage: 50Mi
requests:
ephemeral-storage: 50Mi
RestartPolicy: Once
schedule: '0/1 * * * *'
startingDeadlineSeconds: 5
18 changes: 18 additions & 0 deletions score/testdata/cronjob-batchv1-restartpolicy-not-set.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox:1.28
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
19 changes: 19 additions & 0 deletions score/testdata/cronjob-batchv1-restartpolicy-valid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox:1.28
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
28 changes: 28 additions & 0 deletions score/testdata/cronjob-batchv1beta1-restartpolicy-invalid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
kind: CronJob
apiVersion: batch/v1beta1
metadata:
namespace: cronjobs
name: pwsh-test
spec:
jobTemplate:
spec:
template:
spec:
containers:
- name: pwsh
imagePullPolicy: Always
image: mcr.microsoft.com/powershell:7
command:
- pwsh
- -Command
- Start-Sleep -Seconds 5
securityContext:
readOnlyRootFilesystem: true
resources:
limits:
ephemeral-storage: 50Mi
requests:
ephemeral-storage: 50Mi
RestartPolicy: OnFailure
schedule: '0/1 * * * *'
startingDeadlineSeconds: 5
18 changes: 18 additions & 0 deletions score/testdata/cronjob-batchv1beta1-restartpolicy-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: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox:1.28
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
19 changes: 19 additions & 0 deletions score/testdata/cronjob-batchv1beta1-restartpolicy-valid.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: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox:1.28
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure

0 comments on commit 9becb93

Please # to comment.