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:

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

Creating multiple Cronjob Commands:

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.

Pass dynamic values to helm template:

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:

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

Conclusion:

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

--

--

Software Development Enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store