Skip to content

Commit 689d6b1

Browse files
reixdRainer 'rei' Schuth
andauthored
[cronjobs] add jitter initcontainer (#135)
Co-authored-by: Rainer 'rei' Schuth <rainer.schuth@digital-results-international.com>
1 parent 400cfe3 commit 689d6b1

5 files changed

Lines changed: 81 additions & 2 deletions

File tree

charts/cronjobs/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: v2
33
name: cronjobs
44
description: A generic helm cronjob chart for kubernetes
55
type: application
6-
version: 1.6.6
6+
version: 1.7.0
77
appVersion: latest
88
home: https://github.com/klicktipp/helm-charts
99
keywords:

charts/cronjobs/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# cronjobs
22

3-
![Version: 1.6.6](https://img.shields.io/badge/Version-1.6.6-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: latest](https://img.shields.io/badge/AppVersion-latest-informational?style=flat-square)
3+
![Version: 1.7.0](https://img.shields.io/badge/Version-1.7.0-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: latest](https://img.shields.io/badge/AppVersion-latest-informational?style=flat-square)
44

55
A generic helm cronjob chart for kubernetes
66

@@ -24,6 +24,15 @@ A generic helm cronjob chart for kubernetes
2424
| secretEnvFrom | list | `[]` | Set secretEnvFrom. |
2525
| env | object | `{}` | Environment variable entries. |
2626
| timezone | string | `""` | Default time zone for all CronJobs. Individual jobs can override this value. |
27+
| startupJitter | object | `{"enabled":false,"image":{"pullPolicy":"IfNotPresent","registry":"docker.io","repository":"bash","tag":"5.3"},"maxSeconds":60,"seed":"cronjobs-jitter"}` | Startup jitter settings for all jobs. Individual jobs can override these values. |
28+
| startupJitter.enabled | bool | `false` | Enable startup jitter initContainer. |
29+
| startupJitter.maxSeconds | int | `60` | Maximum startup delay in seconds. The effective delay is between 0 and this value. |
30+
| startupJitter.seed | string | `"cronjobs-jitter"` | Seed prefix combined with namespace for pseudo-random delay generation. |
31+
| startupJitter.image | object | `{"pullPolicy":"IfNotPresent","registry":"docker.io","repository":"bash","tag":"5.3"}` | Jitter initContainer image settings. |
32+
| startupJitter.image.registry | string | `"docker.io"` | Container image registry. |
33+
| startupJitter.image.repository | string | `"bash"` | Container image repository. |
34+
| startupJitter.image.tag | string | `"5.3"` | Container image tag. |
35+
| startupJitter.image.pullPolicy | string | `"IfNotPresent"` | Container image pull policy. |
2736
| jobs | object | `{}` | Configure jobs. |
2837
| serviceAccount | object | `{"annotations":{},"automount":true,"create":true,"name":""}` | ServiceAccount configuration. |
2938
| serviceAccount.create | bool | `true` | Set serviceAccount.create. |

charts/cronjobs/templates/_helpers.tpl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ Create the name of the service account to use
6060
{{- end }}
6161
{{- end }}
6262

63+
{{/*
64+
Return the fully qualified startup jitter image reference.
65+
*/}}
66+
{{- define "cronjobs.startupJitterImage" -}}
67+
{{- $repository := .repository | default "bash" -}}
68+
{{- $tag := .tag | default "5.3" -}}
69+
{{- if .registry -}}
70+
{{- printf "%s/%s:%s" .registry $repository $tag -}}
71+
{{- else -}}
72+
{{- printf "%s:%s" $repository $tag -}}
73+
{{- end -}}
74+
{{- end -}}
75+
6376
{{/*
6477
Given a list of strings, concatenate all of them with a dash ("-")
6578
and slugify the string to be DNS name compatible
@@ -68,3 +81,15 @@ and slugify the string to be DNS name compatible
6881
{{- $r := (join "-" .) | lower | replace "." "-" | replace "/" "-" | replace "_" "-" | replace "--" "-" | replace " " "-" | trimPrefix "-" | trunc 63 | trimSuffix "-" }}
6982
{{- $r }}
7083
{{- end -}}
84+
85+
{{/*
86+
Generate deterministic startup delay seconds from seed parts.
87+
*/}}
88+
{{- define "com.klicktipp.generateStartupDelaySeconds" -}}
89+
{{- $seed := join "-" (index . "seed") -}}
90+
{{- $exceptionList := list "prod" "staging" -}}
91+
{{- $hash := sha256sum $seed -}}
92+
{{- $intFromHash := int64 (printf "%x" (substr 0 8 $hash)) -}}
93+
{{- $maxSeconds := int (default 60 (index . "maxSeconds")) -}}
94+
{{- mod $intFromHash (add $maxSeconds 1) -}}
95+
{{- end -}}

charts/cronjobs/templates/cronjob.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
{{- $env := mergeOverwrite dict $.Values.env ($job.env | default dict) }}
77
{{- $image := mergeOverwrite dict ($.Values.image | default dict) ($job.image | default dict) }}
88
{{- $timezone := $job.timezone | default $.Values.timezone }}
9+
{{- $startupJitter := mergeOverwrite dict ($.Values.startupJitter | default dict) ($job.startupJitter | default dict) }}
10+
{{- $jitterImage := $startupJitter.image | default dict }}
11+
{{- $startupJitterSeed := $startupJitter.seed | default "cronjobs-jitter" }}
12+
{{- $startupJitterMaxSeconds := $startupJitter.maxSeconds | default 60 }}
13+
{{- $startupDelaySeconds := include "com.klicktipp.generateStartupDelaySeconds" (dict "seed" (list $startupJitterSeed $.Release.Namespace $JOB_NAME_SLUG) "maxSeconds" $startupJitterMaxSeconds) }}
914
---
1015
apiVersion: batch/v1
1116
kind: CronJob
@@ -83,6 +88,18 @@ spec:
8388
{{- end }}
8489
securityContext:
8590
{{- toYaml ($job.podSecurityContext | default $.Values.podSecurityContext) | nindent 12 }}
91+
{{- if $startupJitter.enabled }}
92+
initContainers:
93+
- name: jitter
94+
image: {{ include "cronjobs.startupJitterImage" $jitterImage | quote }}
95+
imagePullPolicy: {{ $jitterImage.pullPolicy | default "IfNotPresent" }}
96+
command:
97+
- /usr/local/bin/bash
98+
- -ec
99+
- |
100+
echo "Startup jitter delay: {{ $startupDelaySeconds }}s"
101+
sleep {{ $startupDelaySeconds }}
102+
{{- end }}
86103
containers:
87104
- name: {{ .containerName | default ($.Values.commonContainerName | default $JOB_NAME_SLUG) }}
88105
securityContext:

charts/cronjobs/values.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,25 @@ env: {}
6969
# -- Default time zone for all CronJobs. Individual jobs can override this value.
7070
timezone: ""
7171

72+
# -- Startup jitter settings for all jobs. Individual jobs can override these values.
73+
startupJitter:
74+
# -- Enable startup jitter initContainer.
75+
enabled: false
76+
# -- Maximum startup delay in seconds. The effective delay is between 0 and this value.
77+
maxSeconds: 60
78+
# -- Seed prefix combined with namespace for pseudo-random delay generation.
79+
seed: "cronjobs-jitter"
80+
# -- Jitter initContainer image settings.
81+
image:
82+
# -- Container image registry.
83+
registry: "docker.io"
84+
# -- Container image repository.
85+
repository: bash
86+
# -- Container image tag.
87+
tag: "5.3"
88+
# -- Container image pull policy.
89+
pullPolicy: IfNotPresent
90+
7291
# -- Configure jobs.
7392
jobs: {}
7493
# test:
@@ -84,6 +103,15 @@ jobs: {}
84103
# repository: alpine
85104
# tag: latest
86105
# pullPolicy: IfNotPresent
106+
# startupJitter:
107+
# enabled: true
108+
# maxSeconds: 60
109+
# seed: "my-team"
110+
# image:
111+
# registry: "docker.io"
112+
# repository: bash
113+
# tag: "5.3"
114+
# pullPolicy: IfNotPresent
87115
# # see https://github.com/kubernetes/kubernetes/issues/74848#issuecomment-475178355
88116
# restartPolicy: Never
89117
# startingDeadlineSeconds: 60

0 commit comments

Comments
 (0)