Skip to content

Commit 5109863

Browse files
reixdRainer 'rei' Schuth
andauthored
[crojobs] rewrite templating and volume mounting. Bump to v1.0.0 (#7)
Co-authored-by: Rainer 'rei' Schuth <rainer.schuth@digital-results-international.com>
1 parent cf2302f commit 5109863

7 files changed

Lines changed: 146 additions & 75 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ To uninstall the chart:
2828
* Branch out with the name `${chartname}-chart`
2929
* Put all your changes on this branch and push
3030
* Create a merge request and let it approve and be merged to the `main` branch
31-
* After sucessfull merging, create a tag on the `main` branch with the name of the chart and the latest chart version. Example: `redisinsight-0.2.0`.
31+
* After successful merging, create a tag on the `main` branch with the name of the chart and the latest chart version. Example: `redisinsight-0.2.0`.
3232
* Push the tag
3333
* Now the chart index (in the `gh-pages`) should be updated with the latest chart changes

charts/cronjobs/Chart.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
---
12
apiVersion: v2
23
name: cronjobs
34
description: A generic helm cronjob chart for kubernetes
45
type: application
5-
version: 0.2.5
6+
version: 1.0.0
67
appVersion: latest
78
home: https://github.com/klicktipp/helm-charts
89
keywords:

charts/cronjobs/templates/_helpers.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Create the name of the service account to use
6565
Given a list of strings, concatenate all of them with a dash ("-")
6666
and slugify the string to be DNS name compatible
6767
*/}}
68-
{{- define "kt.slugify-volume-name" -}}
68+
{{- define "com.klicktipp.slugify-volume-name" -}}
6969
{{- $r := (join "-" .) | lower | replace "." "-" | replace "/" "-" | replace "_" "-" | replace "--" "-" | replace " " "-" | trimPrefix "-" | trunc 63 | trimSuffix "-" }}
7070
{{- $r }}
7171
{{- end -}}

charts/cronjobs/templates/configmap.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,25 @@ data:
2424
{{- end }}
2525
{{- end }}
2626
{{- end }}
27+
28+
{{- range $job_name, $job := .Values.jobs }}
29+
{{- if and (hasKey $job "extraFiles") $job.extraFiles }}
30+
{{- $checksum := toYaml $job.extraFiles | sha256sum | trunc 20 | quote }}
31+
---
32+
apiVersion: v1
33+
kind: ConfigMap
34+
metadata:
35+
name: {{ include "com.klicktipp.slugify-volume-name" (list $chart_name "files" $job_name) }}
36+
labels:
37+
{{- $chart_labels | nindent 4 }}
38+
annotations:
39+
values/checksum: {{ $checksum }}
40+
data:
41+
{{- range $filename, $file := $job.extraFiles }}
42+
{{- if $file.enabled }}
43+
{{ $filename }}: |
44+
{{ $file.content | indent 4 }}
45+
{{- end }}
46+
{{- end }} {{/* # end range extraFiles */}}
47+
{{- end }} {{/* # end exists extraFiles */}}
48+
{{- end }} {{/* # end range jobs */}}

charts/cronjobs/templates/cronjob.yaml

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,15 @@
33
{{- $chart_labels := include "cronjobs.labels" . }}
44
{{- $chart_selector_labels := include "cronjobs.selectorLabels" . }}
55
{{- $service_account_name := include "cronjobs.serviceAccountName" . }}
6-
{{- $registry := .Values.image.registry }}
7-
{{- $tag := .Values.image.tag | default .Chart.AppVersion }}
8-
{{- $image_pull_secrets := .Values.imagePullSecrets }}
9-
106
{{- range $job_name, $job := .Values.jobs }}
11-
{{- $image := print $registry "/" $.Values.image.repository ":" $tag }}
12-
{{- if .image }}
13-
{{- $image = print (default $registry .image.registry) "/" .image.repository ":" (default $tag .image.tag) }}
14-
{{- end }}
15-
{{- $env := $.Values.env }}
16-
{{- if .env }}
17-
{{- $env = concat $env .env }}
18-
{{- end }}
7+
{{- $JOB_NAME_SLUG := include "com.klicktipp.slugify-volume-name" $job_name }}
8+
{{- $env := mergeOverwrite dict $.Values.env ($job.env | default dict) }}
199
{{- $checksum := toYaml . | sha256sum | trunc 20 | quote }}
2010
---
2111
apiVersion: {{ $api_version }}
2212
kind: CronJob
2313
metadata:
24-
name: {{ $chart_name }}-{{ $job_name }}
14+
name: {{ $chart_name }}-{{ $JOB_NAME_SLUG }}
2515
labels:
2616
{{- $chart_labels | nindent 4 }}
2717
annotations:
@@ -31,9 +21,7 @@ spec:
3121
failedJobsHistoryLimit: {{ default 1 .failedJobsHistoryLimit }}
3222
successfulJobsHistoryLimit: {{ default 1 .successfulJobsHistoryLimit }}
3323
schedule: {{ .schedule | quote }}
34-
{{- if .suspend }}
35-
suspend: {{ .suspend }}
36-
{{- end }}
24+
suspend: {{ .suspend | default false }}
3725
{{- if .startingDeadlineSeconds }}
3826
startingDeadlineSeconds: {{ .startingDeadlineSeconds }}
3927
{{- end }}
@@ -66,23 +54,23 @@ spec:
6654
spec:
6755
restartPolicy: {{ default "Never" .restartPolicy }}
6856
imagePullSecrets:
69-
{{- with default $image_pull_secrets .imagePullSecrets }}
57+
{{- with ($job.imagePullSecrets | default $.Values.imagePullSecrets) }}
7058
{{- toYaml . | nindent 12 }}
7159
{{- end }}
72-
{{- if $.Values.dockerConfigJson }}
60+
{{- if $job.dockerConfigJson | default $.Values.dockerConfigJson }}
7361
- name: {{ $chart_name }}-registry
7462
{{- end }}
7563
{{- if or $.Values.serviceAccount.create $.Values.serviceAccount.name }}
7664
serviceAccountName: {{ $service_account_name }}
7765
{{- end }}
7866
securityContext:
79-
{{- toYaml $.Values.podSecurityContext | nindent 12 }}
67+
{{- toYaml ($job.podSecurityContext | default $.Values.podSecurityContext) | nindent 12 }}
8068
containers:
81-
- name: {{ $job_name }}
69+
- name: job
8270
securityContext:
83-
{{- toYaml $.Values.securityContext | nindent 16 }}
84-
image: {{ $image }}
85-
imagePullPolicy: {{ $.Values.image.pullPolicy }}
71+
{{- toYaml ($job.securityContext | default $.Values.securityContext) | nindent 16 }}
72+
image: "{{ $job.image.repository }}:{{ $job.image.tag | default $.Chart.AppVersion }}"
73+
imagePullPolicy: {{ $job.image.pullPolicy | default "IfNotPresent" }}
8674
{{- if .command }}
8775
command:
8876
{{- toYaml .command | nindent 16 }}
@@ -92,15 +80,33 @@ spec:
9280
{{- toYaml . | nindent 16 }}
9381
{{- end }}
9482
env:
95-
{{- toYaml $env | nindent 16 }}
83+
{{- range $key, $value := $env}}
84+
- name: {{ $key }}
85+
value: {{ $value | quote }}
86+
{{- end }}
9687
resources:
97-
{{- toYaml $.Values.resources | nindent 16 }}
88+
{{- toYaml $job.resources | nindent 16 }}
9889
volumeMounts:
99-
{{- range .volumeMounts }}
100-
{{- $PVC_NAME := include "kt.slugify-volume-name" (list $.Release.Name (coalesce .claimName .name)) }}
90+
{{- if and $job.storage (hasKey $job.storage "efs") (hasKey $job.storage.efs "mounts") }}
91+
{{- range $efs_mount := $job.storage.efs.mounts }}
92+
{{- range $efs_ap := $efs_mount.access_points }}
93+
{{- $EFS_AP_ID_TRIMMED := trimPrefix "fsap-" $efs_ap.id }}
94+
{{- $PVC_NAME := include "com.klicktipp.slugify-volume-name" (list $.Release.Name $JOB_NAME_SLUG (coalesce $efs_ap.name $efs_ap.path) $EFS_AP_ID_TRIMMED) }}
10195
- name: {{ $PVC_NAME }}
102-
mountPath: {{ .mountPath }}
96+
mountPath: {{ required "ERROR: the EFS mount point requires a mount_dest." $efs_ap.mount_dest }}
97+
{{- end }} {{/* # end range $efs_ap */}}
98+
{{- end }} {{/* # end range $efs_mount */}}
99+
{{- end }} {{/* # end if storage.efs */}}
100+
{{- if and (hasKey $job "extraFiles") $job.extraFiles }}
101+
{{- range $filename, $file := $job.extraFiles }}
102+
{{- if $file.enabled }}
103+
- name: cronjob-files
104+
mountPath: '{{ $file.mountPath }}'
105+
subPath: '{{ $filename }}'
106+
readOnly: true
103107
{{- end }}
108+
{{- end }} {{/* # end range extraFiles */}}
109+
{{- end }} {{/* # end exists extraFiles */}}
104110
{{- if $.Values.customConfigMap }}
105111
- mountPath: /configMaps/{{ $.Values.customConfigMap }}
106112
name: {{ $.Values.customConfigMap }}
@@ -113,25 +119,43 @@ spec:
113119
- mountPath: /secrets/{{ $job_name }}
114120
name: {{ $job_name }}
115121
{{- end }}
116-
{{- with $.Values.nodeSelector }}
122+
{{- with $job.nodeSelector | default $.Values.nodeSelector }}
117123
nodeSelector:
118124
{{- toYaml . | nindent 12 }}
119125
{{- end }}
120-
{{- with $.Values.affinity }}
126+
{{- with $job.affinity | default $.Values.affinity }}
121127
affinity:
122128
{{- toYaml . | nindent 12 }}
123129
{{- end }}
124-
{{- with $.Values.tolerations }}
130+
{{- with $job.tolerations | default $.Values.tolerations }}
125131
tolerations:
126132
{{- toYaml . | nindent 12 }}
127133
{{- end }}
128134
volumes:
129-
{{- range .volumes }}
130-
{{- $PVC_NAME := include "kt.slugify-volume-name" (list $.Release.Name (coalesce .claimName .name)) }}
135+
{{- if and $job.storage (hasKey $job.storage "efs") (hasKey $job.storage.efs "mounts") }}
136+
{{- range $efs_mount := $job.storage.efs.mounts }}
137+
{{- range $efs_ap := $efs_mount.access_points }}
138+
{{- $EFS_AP_ID_TRIMMED := trimPrefix "fsap-" $efs_ap.id }}
139+
{{- $PVC_NAME := include "com.klicktipp.slugify-volume-name" (list $.Release.Name $JOB_NAME_SLUG (coalesce $efs_ap.name $efs_ap.path) $EFS_AP_ID_TRIMMED) }}
131140
- name: {{ $PVC_NAME }}
132141
persistentVolumeClaim:
133142
claimName: {{ $PVC_NAME }}
134-
{{- end }}
143+
{{- end }} {{/* # end range $efs_ap */}}
144+
{{- end }} {{/* # end range $efs_mount */}}
145+
{{- end }} {{/* # end if storage.efs */}}
146+
{{- if and (hasKey $job "extraFiles") $job.extraFiles }}
147+
- name: cronjob-files
148+
configMap:
149+
name: {{ include "com.klicktipp.slugify-volume-name" (list $chart_name "files" $JOB_NAME_SLUG) }}
150+
items:
151+
{{- range $filename, $file := $job.extraFiles }}
152+
{{- if $file.enabled }}
153+
- key: '{{ $filename }}'
154+
path: '{{ $filename }}'
155+
mode: {{ ($file.mode | default "0644") | toDecimal }}
156+
{{- end }}
157+
{{- end }} {{/* # end range extraFiles */}}
158+
{{- end }} {{/* # end exists extraFiles */}}
135159
{{- if $.Values.customConfigMap }}
136160
- name: {{ $.Values.customConfigMap }}
137161
configMap:
@@ -142,7 +166,7 @@ spec:
142166
configMap:
143167
name: {{ print $chart_name "-" $job_name }}
144168
{{- end }}
145-
{{- range $job_name, $secret := $.Values.secrets }}
169+
{{- range $job_name, $secret := $job.secrets | default $.Values.secrets }}
146170
- name: {{ $job_name }}
147171
secret:
148172
secretName: {{ print $chart_name "-" $job_name }}

charts/cronjobs/templates/volumes.yaml

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,60 @@
1-
{{- $chart_name := include "cronjobs.fullname" . }}
1+
---
22
{{- range $job_name, $job := .Values.jobs }}
3-
{{- range $volume := .volumes }}
4-
{{- $PV_NAME := include "kt.slugify-volume-name" (list $.Release.Namespace $.Release.Name $job_name .name) }}
5-
{{- $PVC_NAME := include "kt.slugify-volume-name" (list $.Release.Name (coalesce .claimName .name)) }}
3+
{{- $JOB_NAME_SLUG := include "com.klicktipp.slugify-volume-name" $job_name }}
4+
{{- if and $job.storage (hasKey $job.storage "efs") (hasKey $job.storage.efs "mounts") }}
5+
{{- range $efs_mount := $job.storage.efs.mounts }}
6+
{{- range $efs_ap := $efs_mount.access_points }}
7+
{{- $EFS_AP_ID_TRIMMED := trimPrefix "fsap-" $efs_ap.id }}
8+
{{- $PV_NAME := include "com.klicktipp.slugify-volume-name" (list $.Release.Namespace $.Release.Name $JOB_NAME_SLUG $EFS_AP_ID_TRIMMED) }}
9+
{{- $PVC_NAME := include "com.klicktipp.slugify-volume-name" (list $.Release.Name $JOB_NAME_SLUG (coalesce $efs_ap.name $efs_ap.path) $EFS_AP_ID_TRIMMED) }}
610
---
711
apiVersion: v1
812
kind: PersistentVolume
913
metadata:
1014
name: {{ $PV_NAME }}
15+
annotations:
16+
storage.ktsys.cloud/efs_fs_id: {{ $efs_mount.fs_id | quote }}
17+
storage.ktsys.cloud/efs_name: {{ $efs_mount.fs_name | quote }}
18+
storage.ktsys.cloud/efs_ap_id: {{ $efs_ap.id | quote }}
19+
storage.ktsys.cloud/efs_path: {{ $efs_ap.path | quote }}
20+
{{- with $efs_mount.annotations }}
21+
{{- toYaml . | nindent 4 }}
22+
{{- end }}
1123
spec:
1224
capacity:
1325
storage: 5Gi # Any number will do, we are just required to specify a storage capacity
1426
volumeMode: Filesystem
1527
accessModes:
1628
- ReadWriteMany
1729
persistentVolumeReclaimPolicy: Delete
18-
storageClassName: {{ .storageClass }}
30+
storageClassName: {{ $job.storage.efs.storageClass }}
1931
csi:
20-
{{- with .csi }}
21-
{{- toYaml . | nindent 4 }}
22-
{{- end }}
32+
driver: efs.csi.aws.com
33+
volumeHandle: {{ $efs_mount.fs_id }}::{{ $efs_ap.id }}
2334

2435
---
2536
apiVersion: v1
2637
kind: PersistentVolumeClaim
2738
metadata:
2839
name: {{ $PVC_NAME }}
40+
annotations:
41+
storage.ktsys.cloud/efs_fs_id: {{ $efs_mount.fs_id | quote }}
42+
storage.ktsys.cloud/efs_name: {{ $efs_mount.fs_name | quote }}
43+
storage.ktsys.cloud/efs_ap_id: {{ $efs_ap.id | quote }}
44+
storage.ktsys.cloud/efs_path: {{ $efs_ap.path | quote }}
45+
{{- with $efs_mount.annotations }}
46+
{{- toYaml . | nindent 4 }}
47+
{{- end }}
2948
spec:
3049
accessModes:
3150
- ReadWriteMany
32-
storageClassName: {{ .storageClass }}
51+
storageClassName: {{ $job.storage.efs.storageClass }}
3352
volumeName: {{ $PV_NAME }}
3453
resources:
3554
requests:
3655
storage: 5Gi # Any number will do, we are just required to specify a storage capacity
3756

57+
{{- end }} {{/* # end range $efs_ap */}}
58+
{{- end }} {{/* # end range $efs_mount */}}
59+
{{- end }} {{/* # end if storage.efs */}}
3860
{{- end }} {{/* # end range volumes */}}
39-
{{- end }} {{/* # end range jobs */}}

charts/cronjobs/values.yaml

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
nameOverride: ""
22
fullnameOverride: ""
33

4-
image:
5-
registry: docker.io
6-
repository: busybox
7-
pullPolicy: IfNotPresent
8-
# Overrides the image tag whose default is the chart appVersion.
9-
tag: latest
10-
114
imagePullSecrets: []
125

136
# will create a kubernetes.io/dockerconfigjson secret
@@ -51,7 +44,7 @@ configMaps: {}
5144
# example.sh: ci/files/example.sh
5245

5346
# environment variables for all jobs
54-
env: []
47+
env: {}
5548

5649
jobs: {}
5750
# test:
@@ -63,26 +56,36 @@ jobs: {}
6356
# - echo "foo"; ps fauxwww
6457
# image:
6558
# repository: alpine
59+
# tag: latest
60+
# pullPolicy: IfNotPresent
6661
# # see https://github.com/kubernetes/kubernetes/issues/74848#issuecomment-475178355
67-
# # restartPolicy: Never
68-
# # startingDeadlineSeconds: 60
69-
# # activeDeadlineSeconds: 60
70-
# # backoffLimit: 3
71-
# # completions: 3
72-
# # parallelism: 1
73-
# # ttlSecondsAfterFinished: 60
74-
# # suspend: false
75-
# # volumes:
76-
# # - name: docker1
77-
# # hostPath:
78-
# # path: /var/
79-
# # storageClass: efs
80-
# # csi:
81-
# # driver: efs.csi.aws.com
82-
# # volumeHandle: {{ $efs_fs_id }}::{{ $efs_ap.id }}
83-
# # volumeMounts:
84-
# # - name: docker1
85-
# # mountPath: /var/
62+
# restartPolicy: Never
63+
# startingDeadlineSeconds: 60
64+
# activeDeadlineSeconds: 60
65+
# backoffLimit: 3
66+
# completions: 3
67+
# parallelism: 1
68+
# ttlSecondsAfterFinished: 60
69+
# suspend: false
70+
# storage:
71+
# efs:
72+
# storageClass: kt-efs-sc
73+
# mounts: []
74+
# # - fs_name: <aws-efs-filesystem-name>
75+
# # fs_id: fs-1111111111
76+
# # access_points:
77+
# # - path: /foo/bar
78+
# # mount_dest: /var/bar
79+
# # id: fsap-1232345678
80+
# # #name: foo # [OPTIONAL]
81+
# extraFiles: {}
82+
# # myscript.sh:
83+
# # mountPath: "/usr/local/bin/myscript.sh"
84+
# # enabled: true
85+
# # mode: "0755"
86+
# # content: |
87+
# # #!/bin/bash
88+
# # echo "Hello from script"
8689

8790
serviceAccount:
8891
# Specifies whether a service account should be created

0 commit comments

Comments
 (0)