DEV Community

ujwal dhakal
ujwal dhakal

Posted on

11 2

Manage Multiple Cron with Helm Flow Control

If you want to set up a Cron on your application, using a cron in Kubernetes is straightforward. All you need to do is copy the CronJob template.

If you have one cronjob then it won't matter much but if you have many cronjobs then creating each file per cron can be not fun sometimes.

Helm has Flow Control which can be used to manipulate dynamic values in the template.

In this article, we will create a single cron and then we will loop through the collections of cronjob commands to create multiple cronjob resource.

Creating a single CronJob:

Create a cronjob by copying the following code.

apiVersion: batch/v1
kind: CronJob
metadata:
name: job1
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: job1
image: busybox:1.28
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
view raw Cronjob hosted with ❤ by GitHub

The file above creates a cron that runs every minute and prints some text.

Creating multiple Cronjob Commands:

Now let's create multiple cronjob commands which will hold all unique placeholders. We will now create a separate cronjob file that will hold the values i.e name, frequency, command etc of cron. Let's create a cronjobs.yaml inside .helm/values/cronjobs.yaml.

We will be creating four cronjobs which will have their own id, name, command, and schedule. But you can add more dynamic values if you want.

cronjobs:
- id: job-1
name: job-1
command: "date; echo Completed job 1"
schedule: "* * * * *"
- id: job-2
name: job-2
command: "date; echo Completed job 2"
schedule: "* * * * *"
- id: job-3
name: job-3
command: "date; echo Completed job 3"
schedule: "* * * * *"
- id: job-4
name: job-4
command: "date; echo Completed job 4"
schedule: "* * * * *"
view raw cronjobs.yaml hosted with ❤ by GitHub

Pass dynamic values to helm template:

So far we have worked single cronjob then we created multiple cronjob commands. Now we want to loop those cronjobs commands inside the single cronjob so that we do not have to create multiple files manually.

There are multiple commands we can use in Helm Flow Control. For this we will use *{{- range $cronjob := $.Values.cronjobs }}* to loop over the values and access the dynamic values like {{$cronjob.id}},{{$cronjob.name}},{{$cronjob.schedule}} . As shown in the code below:

{{- range $cronjob := $.Values.cronjobs }}
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: {{ $cronjob.id }}
labels:
app.kubernetes.io/name: {{ $cronjob.id }}
app.kubernetes.io/component: cronjob
app.kubernetes.io/instance: api
spec:
schedule: "{{ $cronjob.schedule }}"
jobTemplate:
spec:
template:
spec:
containers:
- name: cron-runner
image: busybox:1.28
command:
- /bin/sh
- -c
- {{ $cronjob.command }}
restartPolicy: OnFailure
{{- end }}
view raw crongist hosted with ❤ by GitHub

The final piece of the puzzle is to pass those dynamic values while installing/upgrading Helm. In order to do so, you can pass those values by using the — values keyword with the full path of that cronjobs command file i.e*.`*helm/values/cronjobs.yaml. `The final command will look something like this:

helm upgrade — install — values “.helm/values/cronjobs.yaml” multiple-cronjobs .helm
Enter fullscreen mode Exit fullscreen mode

Conclusion:

We set up multiple cronjobs using two files one for defining the specs of the whole Kubernetes cronjob and the next for defining the dynamic values i.e command, frequency, name, etc. You can build/manage complex configurations using the Helm Flow Control.

Github Link: https://github.com/ujwaldhakal/multiple-cronjob-helm

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more