Manage Multiple Cron with Helm Flow Control
Tags: cronjobs, kubernetes, helm
Published: 2022-04-04 12:39:00

---
title: Manage Multiple Cron with Helm Flow Control
published: true
description: Manage Multiple Cron with Helm Flow Control
tags: cronjobs,kubernetes, helm 
//cover_image: https://direct_url_to_image.jpg
---

If you want to set up a [Cron](https://www.hivelocity.net/kb/what-is-cron-job/) on your application, using a cron in Kubernetes is straightforward. All you need to do is copy the [CronJob template](https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/).

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](https://helm.sh/) has [Flow Control ](https://helm.sh/docs/chart_template_guide/control_structures/)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.
{% gist https://gist.github.com/ujwaldhakal/4005f5193fc0ca9f15fc50b6357eff53 %}


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.

{% gist https://gist.github.com/ujwaldhakal/099ace9401ed99054199f670db9d857b %}

## 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](https://helm.sh/docs/chart_template_guide/control_structures/). 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:

{% gist https://gist.github.com/ujwaldhakal/ff97414c73fb52d8d72d96b97282c2bf %}

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:

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