Skip to content

Commit

Permalink
fix(common): set correct default for restartPolicy for cronjobs (#28356)
Browse files Browse the repository at this point in the history
**Description**
<!--
Please include a summary of the change and which issue is fixed. Please
also include relevant motivation and context. List any dependencies that
are required for this change.
-->
⚒️ Fixes  #28118

**⚙️ Type of change**

- [ ] ⚙️ Feature/App addition
- [x] 🪛 Bugfix
- [ ] ⚠️ Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] 🔃 Refactor of current code

**🧪 How Has This Been Tested?**
<!--
Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration
-->

**📃 Notes:**
<!-- Please enter any other relevant information here -->

**✔️ Checklist:**

- [x] ⚖️ My code follows the style guidelines of this project
- [x] 👀 I have performed a self-review of my own code
- [ ] #️⃣ I have commented my code, particularly in hard-to-understand
areas
- [x] 📄 I have made corresponding changes to the documentation
- [x] ⚠️ My changes generate no new warnings
- [x] 🧪 I have added tests to this description that prove my fix is
effective or that my feature works
- [x] ⬆️ I increased versions for any altered app according to semantic
versioning
- [x] I made sure the title starts with `feat(chart-name):`,
`fix(chart-name):` or `chore(chart-name):`

**➕ App addition**

If this PR is an app addition please make sure you have done the
following.

- [ ] 🖼️ I have added an icon in the Chart's root directory called
`icon.png`

---

_Please don't blindly check all the boxes. Read them and only check
those that apply.
Those checkboxes are there for the reviewer to see what is this all
about and
the status of this PR with a quick glance._
  • Loading branch information
stavros-k authored Oct 26, 2024
1 parent b16bd48 commit 006cae1
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 6 deletions.
1 change: 0 additions & 1 deletion charts/library/common-test/ci/cron-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ workload:
type: CronJob
schedule: "*/1 * * * *"
podSpec:
restartPolicy: OnFailure
containers:
main:
enabled: true
Expand Down
5 changes: 5 additions & 0 deletions charts/library/common-test/tests/cronjob/spec_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ tests:
parallelism: 5
ttlSecondsAfterFinished: 100
activeDeadlineSeconds: 100
- documentIndex: *cronJobDoc
isSubset:
path: spec.jobTemplate.spec.template.spec
content:
restartPolicy: OnFailure

- it: should set suspend to true
set:
Expand Down
14 changes: 14 additions & 0 deletions charts/library/common-test/tests/cronjob/validation_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,17 @@ tests:
asserts:
- failedTemplate:
errorMessage: CronJob - Namespace [my-extra-super-duper-long-name-that-is-longer-than-63-characters] is not valid. Must start and end with an alphanumeric lowercase character. It can contain '-'. And must be at most 63 characters.

- it: should fail with restartPolicy "Always" for CronJob
set:
workload:
workload-name:
enabled: true
primary: true
type: CronJob
schedule: "* * * * *"
podSpec:
restartPolicy: Always
asserts:
- failedTemplate:
errorMessage: Expected [restartPolicy] to be one of [Never, OnFailure] for [CronJob] but got [Always]
6 changes: 3 additions & 3 deletions charts/library/common-test/tests/ingress/homepage_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ tests:
gethomepage.dev/description: Helper chart to test different use cases of the common library
gethomepage.dev/href: https://test-host/test-path
gethomepage.dev/widget.url: https://test-release-name-common-test.test-release-namespace.svc:9443
gethomepage.dev/icon: https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png
gethomepage.dev/icon: https://truecharts.org/img/hotlink-ok/chart-icons/common-test.webp
gethomepage.dev/widget.type: commontest
gethomepage.dev/pod-selector: app.kubernetes.io/instance=test-release-name,pod.lifecycle in (permanent)
- documentIndex: *ingressDoc
Expand Down Expand Up @@ -183,7 +183,7 @@ tests:
gethomepage.dev/description: Helper chart to test different use cases of the common library
gethomepage.dev/href: https://test-host/test-path
gethomepage.dev/widget.url: http://test-release-name-common-test-my-service2.test-release-namespace.svc:80
gethomepage.dev/icon: https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png
gethomepage.dev/icon: https://truecharts.org/img/hotlink-ok/chart-icons/common-test.webp
gethomepage.dev/widget.type: commontest
gethomepage.dev/pod-selector: app.kubernetes.io/instance=test-release-name,pod.lifecycle in (permanent)
- documentIndex: *thirdIngressDoc
Expand All @@ -205,7 +205,7 @@ tests:
gethomepage.dev/name: TestReleaseName
gethomepage.dev/description: Helper chart to test different use cases of the common library
gethomepage.dev/href: https://test-host/test-path
gethomepage.dev/icon: https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png
gethomepage.dev/icon: https://truecharts.org/img/hotlink-ok/chart-icons/common-test.webp
gethomepage.dev/pod-selector: app.kubernetes.io/instance=test-release-name,pod.lifecycle in (permanent)

# Failures
Expand Down
5 changes: 5 additions & 0 deletions charts/library/common-test/tests/job/spec_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ tests:
parallelism: 5
ttlSecondsAfterFinished: 100
activeDeadlineSeconds: 100
- documentIndex: *jobDoc
isSubset:
path: spec.template.spec
content:
restartPolicy: OnFailure

- it: should set parallelism to 0
set:
Expand Down
13 changes: 13 additions & 0 deletions charts/library/common-test/tests/job/validation_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,16 @@ tests:
asserts:
- failedTemplate:
errorMessage: Job - Namespace [my-extra-super-duper-long-name-that-is-longer-than-63-characters] is not valid. Must start and end with an alphanumeric lowercase character. It can contain '-'. And must be at most 63 characters.

- it: should fail with restartPolicy "Always" for Job
set:
workload:
workload-name:
enabled: true
primary: true
type: Job
podSpec:
restartPolicy: Always
asserts:
- failedTemplate:
errorMessage: Expected [restartPolicy] to be one of [Never, OnFailure] for [Job] but got [Always]
2 changes: 1 addition & 1 deletion charts/library/common/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ sources:
- https://hub.docker.com/r/mikefarah/yq
- https://hub.docker.com/r/traefik/whoami
type: library
version: 25.1.0
version: 25.1.1
10 changes: 10 additions & 0 deletions charts/library/common/templates/lib/pod/_restartPolicy.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ objectData: The object data to be used to render the Pod.

{{- $policy := "Always" -}}

{{- $jobTypes := (list "Job" "CronJob") -}}
{{- if mustHas $objectData.type $jobTypes -}}
{{- $policy = "OnFailure" -}}
{{- end -}}

{{/* Initialize from the "defaults" */}}
{{- with $rootCtx.Values.podOptions.restartPolicy -}}
{{- $policy = tpl . $rootCtx -}}
Expand All @@ -30,5 +35,10 @@ objectData: The object data to be used to render the Pod.
{{- fail (printf "Expected [restartPolicy] to be [Always] for [%s] but got [%s]" $objectData.type $policy) -}}
{{- end -}}

{{- if and (eq "Always" $policy) (mustHas $objectData.type $jobTypes) -}}
{{- $cronPolicies := mustWithout $policies "Always" -}}
{{- fail (printf "Expected [restartPolicy] to be one of [%s] for [%s] but got [%s]" (join ", " $cronPolicies) $objectData.type $policy) -}}
{{- end -}}

{{- $policy -}}
{{- end -}}
1 change: 0 additions & 1 deletion charts/library/common/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ podOptions:
# If this key exists, takes precedence over the automated calculation
# hostUsers: false
shareProcessNamespace: false
restartPolicy: Always
dnsPolicy: ClusterFirst
dnsConfig:
options:
Expand Down
27 changes: 27 additions & 0 deletions run_common_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
# https://github.com/helm-unittest/helm-unittest

# -- You need to install this helm plugin
# helm plugin install https://github.com/helm-unittest/helm-unittest

common_test_path="charts/library/common-test"

function cleanup {
if [ -d "$common_test_path/charts" ]; then
echo "🧹 Cleaning up charts..."
rm -r "$common_test_path/charts"
rm "$common_test_path/Chart.lock"
# Clean snapshots
rm -r "$common_test_path/**/__snapshot__" 2>/dev/null
fi
}

cleanup

echo "🔨 Building common..."
helm dependency update "$common_test_path"

echo "🧪 Running tests..."
helm unittest --update-snapshot -f "tests/*/*.yaml" "./$common_test_path" -v ./$common_test_path/unit-values.yaml

cleanup
4 changes: 4 additions & 0 deletions website/src/content/docs/common/workload/cronjob.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Replace references to `$name` with the actual name you want to use.

- `.Values.workload.$name`

## Notes

Value of `workload.$name.podSpec.restartPolicy` can **not** be `Always` for this type of workload

## `schedule`

Define the schedule
Expand Down
4 changes: 4 additions & 0 deletions website/src/content/docs/common/workload/job.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Replace references to `$name` with the actual name you want to use.

- `.Values.workload.$name`

## Notes

Value of `workload.$name.podSpec.restartPolicy` can **not** be `Always` for this type of workload

---

## `completionMode`
Expand Down

0 comments on commit 006cae1

Please sign in to comment.