Magicron, small K8s template to make cronjob

Sometimes you need to create a lot of CronJobs in k8s. In particular, in my last project I need to create a lot of stupid "web hooks" to fire complex job execution. K8s is well suited for this task because it take care of launching a single job instance, and relaunch them in case of error.

I ended up creating a small helm template you can use to create standard jobs In your values html define a thing like

1magicron:
2  boringTask1:
3    schedule: "10 5 * * *"
4    url: /boring1
5  boringTask2:
6    schedule: "10 6 * * *"
7    url: /boring2    

Then use the following tiny template to generate all the K8s boilerplate:

 1{{- /*
 2  Magicron Core
 3  Author: Giovanni Giorgi - 2024
 4  Purpose:
 5    Dinamically create a set of batch jobs based on very simple structure under values called magicron
 6Usage example:
 7magicron:
 8  ctb:
 9    schedule: "10 5 * * *"
10    url: /cluster/private/cronJob
11
12
13*/}}
14{{/* Generate basic labels */}}
15{{- define "magicron.labels" -}}
16labels:
17  helm.sh/chart: {{ .top.Chart.Name }}-{{ .top.Chart.Version }}
18  app.kubernetes.io/name: {{ .top.Release.Name }}
19  app.kubernetes.io/instance: {{ .top.Release.Name }}
20  application_code: yourapp
21  app.kubernetes.io/managed-by: Helm  
22  magicron/job: cron-{{ .jobName }}
23{{- end -}}
24{{- $top := . -}}
25{{- range $k, $v := $top.Values.magicron -}}
26{{ $labelData := dict "top" $top "jobName" $k }}
27---
28# Crontab for {{ $k }}
29apiVersion: batch/v1
30kind: CronJob
31metadata:
32  name: cron-{{ $k }}-{{ $.Values.app.name }}
33  namespace: {{ $.Values.app.namespace }}
34  {{- include "magicron.labels" $labelData | nindent 2 }}
35spec:
36  schedule: {{ $v.schedule | quote }}
37  jobTemplate:
38    spec:
39      template:
40        metadata:
41          {{- include "magicron.labels" $labelData | nindent 10 }}
42        spec:
43          {{- with $.Values.imagePullSecrets }}
44          imagePullSecrets:
45            {{- toYaml . | nindent 12 }}
46          {{- end }}
47          containers:
48            - name: {{ $k }}-batch-run              
49              image: alpine:latest
50              imagePullPolicy: IfNotPresent
51              # Alpine should consume less than 6Mb RAM and it is a good solution for tiny webhooks jobs
52              resources:
53                requests:
54                  memory: "9Mi"
55                  cpu: "25m"
56                limits:
57                  memory: "9Mi"
58                  cpu: "50m"
59              # The following command emit on standard output the log of the webhook
60              # Also provide a magic 'User-Agent' string with option U to detect the source of the event
61              command:
62                - /bin/sh
63                - -c
64                - wget -O - --no-check-certificate -S -U MagiCron   http://svc-{{ $.Values.app.name }}.{{ $.Values.app.namespace }}.svc.cluster.local:8080{{ $v.url }}          
65          restartPolicy: OnFailure
66  # Replace running job if running (needed to be sure new version upgrades)
67  concurrencyPolicy: Replace
68{{ end }}