DEV Community

ujwal dhakal
ujwal dhakal

Posted on

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.

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.

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:

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

Discussion (0)