Helm Chart Flow Control Scoping and Looping with examples

Helm – Part-5: Helm Chart Flow Control, Scoping, and Looping.

Helm is a package manager for Kubernetes. Like yum and apt for the Linux. As the usage of the Kubernetes goes higher for seamless application, learning Helm seems important to fit in the future of Software Development. Hence we are going to see the series of tutorials on Helm in digitalvarys.com. In our previous article, we discussed what is Helm Chart Built-in Objects and Values For Template. Along with that, we have Helm Template Functions and Pipelines with examples. In this article, we will discuss what is Helm Chart Flow Control, Scoping, and Looping with examples.

Workspace Creation

Create a folder structure using CLI as the first step and it will create the folders as we discussed in our previous article.

  • Chart.yaml
  • values.yaml
  • template/
  • charts/

Then, we are going to create a sample deployment by deploying ConfigMaps into the Kubernetes cluster.

Template Flow Control

Flow Control is just like the If else statement in any other programming language. Here the condition will be evaluated by the Pipeline to see whether the condition is true or false. If the condition is true, the If part will be executed And false, the else if part will be executed. If all the conditions are false, then the default conditions will be executed

{{ if <PIPELINE> }}
    # If true will execute
{{ else if <next Pipeline> }}
    # If false and else true will execute
{{ else }}
    # default execution
{{ end }}

When we say Pipeline is evaluated, it should return, any one of the following to set the if condition true.

  • A Numeric Zero (0)
  • Empty String (“”)
  • Null
  • Boolean False
  • Empty Collections (array, map, tuple, etc..)

Let’s see some examples. For the example condition, we will take the Workspace created in the above step. So as per this, the value file will contain the following information.

projectNumber: 12345
systemuser: cicd-admin
infra:
  availability-zone: "1"
  regions: us-east

So, let’s create a control flow that will evaluate the values file and execute the template file accordingly. So a template file with Control Flow is:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-chart-configmap
data:
  my-key: "my-value"
    systemuser: {{ .Values.systemuser }}
  projectNumber: {{ .Values.projectNumber }}
  availability-zone: {{quote .Values.availability-zone }}
  {{ if eq .Values.infra.regions "ap-south"}}region: "Ap south region" 
  {{ else if eq .Values.infra.regions "us-west" }}region: "us west region"
  {{ else }}region: "us east region"
  {{ end }}

So, the first if condition will check if the region in the values file is “ap-south”. If that’s true, that will return “ap south region” string to the region-set. So as the else if conditions. And when no if conditions are true, the default value will be executed. Importantly, we need to be careful with the indentation that is going to be rendered after the dry run.

So, now let’s dry run this chart and see if the Manifest shows the region-set according to the conditions we applied. After the dry-run the manifest will show the below lines

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-chart-configmap
data:
  my-key: "my-value"
    systemuser: cicd-admin
  projectNumber: 12345
  availability-zone: "1"


  region: us-east

As you can see there are two empty lines rendered in the Manifest. This will not break but this is not advised to have. So, we are going to have a solution here by simply adding “-” at the beginning of the if condition. Like,

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-chart-configmap
data:
  my-key: "my-value"
    systemuser: {{ .Values.systemuser }}
  projectNumber: {{ .Values.projectNumber }}
  availability-zone: {{quote .Values.availability-zone }}
  {{- if eq .Values.infra.regions "ap-south"}}region: "Ap south region" 
  {{- else if eq .Values.infra.regions "us-west" }}region: "us west region"
  {{- else }}region: "us east region"
  {{- end }}

This will remove the empty line created and the new manifest after the dry run will look like this.

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-chart-configmap
data:
  my-key: "my-value"
    systemuser: cicd-admin
  projectNumber: 12345
  availability-zone: "1"
  region: us-east

Remember, do not add the “-” at the end of the if condition as it will remove starting and ending of the empty line unless you are creating a scenario.

Template Scoping

Just like we manipulate the content by Control flow, we can manipulate the scope by using “with”. So, Let’s consider the below values file and we are going to create a list of tags.

projectNumber: 12345
systemuser: cicd-admin
infra:
  availability-zone: "1"
  regions: us-east
tags:
  env: dev
  platform: java
  group: devs
  space: alpha

So, let’s modify the scope of the Template by using “with”. So I am going to add with condition with using tags. Then that’s going to be looking like.

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-chart-configmap
data:
  my-key: "my-value"
    systemuser: {{ .Values.systemuser }}
  projectNumber: {{ .Values.projectNumber }}
  availability-zone: {{quote .Values.availability-zone }}
  {{- with .Values.tags}}
  Environment name: {{ .env | default dev | quote }}
  OS Platorm: {{ .platform | default linux | quote }}
  Team Name: {{ .group | default devs | quote }}
  Name Space: {{ .space | default "" | quote }}
  {{- end }}

So, whatever we are going to have in the with block will be available only within the block. We don’t have to pass those to another area of the code.

If you run the dry run, you will see the below in the manifest.

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-chart-configmap
data:
  my-key: "my-value"
    systemuser: {{ .Values.systemuser }}
  projectNumber: {{ .Values.projectNumber }}
  availability-zone: {{quote .Values.availability-zone }}
  Environment name: dev
  OS Platorm: java
  Team Name: devs
  Name Space: ""

So, this is how scoping will work.

Template Looping

Just like any other programming language, Helm Templating will have Looping. But here the keyword for the loop is Range. Let’s see in action.

projectNumber: 12345
systemuser: cicd-admin
infra:
  availability-zone: "1"
  regions: us-east
tags:
  env: dev
  platform: java
  group: devs
  space: alpha
environment-list:
  - dev01
  - dev02
  - qat01
  - qat02
  - stg01
  - stg02
  - prod

In the above values file, we will have the list of environment names. And iterate it on the Range in the Template file.

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-chart-configmap
data:
  my-key: "my-value"
    systemuser: {{ .Values.systemuser }}
  projectNumber: {{ .Values.projectNumber }}
  availability-zone: {{quote .Values.availability-zone }}
  environment-names: | -
    {{- range .Values.environment-list}}
    - {{ . | title | quote }}
    {{- end }}

So, this will iterate through the environment-list and print one by one in the manifest file. You can also do the combinations with Range, with and if conditions and achieve the desired scenario.

Conclusion

In this article, we discussed discuss what is Helm Chart Flow Control, Scoping, and Looping with examples. In our upcoming article, we will discuss the dry run and debug mode, Subcharts, variables, and more. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOpsAgileDevSecOps, and App Development.

Leave a Reply