Loops in Helm Charts

Mustafa AK
3 min readJan 21, 2023

Using loops in Helm charts is a powerful way to automate the deployment and management of your applications on Kubernetes. Loops allow you to iterate over a range of values, creating resources and making decisions based on those values. In this article, we’ll take a look at some common use cases for loops in Helm charts and provide detailed examples to help you get started with using loops in your own charts.

  1. Creating multiple replicas of a deployment: One of the most common use cases for loops in Helm charts is to create multiple replicas of a deployment. This is useful when you want to scale your application horizontally across multiple nodes. The .Range function can be used to loop over a range of values, creating a replica for each value.
{{- range $i := .Values.replicaCount }}
- name: replica-{{ $i }}
image: myorg/myapp:{{ .Values.image.tag }}
{{- end }}

2. Creating multiple resources of the same type: Another common use case for loops in Helm charts is to create multiple resources of the same type. For example, you might use a loop to create multiple ConfigMaps, each with different configuration values. The .Range function can be used to loop over a range of values, creating a resource for each value.

{{- range $i, $value := .Values.configmaps }}
apiVersion: v1
kind: ConfigMap
metadata:
name: configmap-{{ $i }}
data:
{{ $value.key }}: {{ $value.value }}
{{- end }}

3. Waiting for a specific condition: Sometimes, you may need to wait for a certain condition to be met before proceeding with the next step in your chart. For example, you may need to wait for a deployment to be ready before proceeding with creating additional resources. The .Until function can be used to loop over a range until a certain condition is met.Copy code

{{- $name := "mydeployment" -}}
{{- $ready := false -}}
{{- until $ready }}
{{- $status := kubectl get deploy $name -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}' -}}
{{- if eq $status "True" }}
{{- $ready = true -}}
{{- else }}
waiting for deployment to be ready...
{{- end }}
{{- end }}

4. Creating multiple resources based on a list: Sometimes, you may need to create multiple resources based on a list of values. For example, you may have a list of ports that need to be exposed on a service. The .Range function can be used to loop over the list of values, creating a resource for each value.

{{- range $i, $value := .Values.ports }}
- name: port-{{ $value }}
port: {{ $value }}
{{- end }}

Let’s create multiple ConfigMap objects using single manifest file that uses the .Range function:

{{- range $i, $value := .Values.configmaps }}
apiVersion: v1
kind: ConfigMap
metadata:
name: configmap-{{ $i }}
data:
{{- range $key, $val := $value }}
{{ $key }}: {{ $val }}
{{- end }}
{{- end }}

In this example, the outer .Range function is used to loop over a range of values defined in .Values.configmaps. Each iteration of the loop creates a new ConfigMap object and sets its name to "configmap-{{ $i }}", where $i is the index of the current iteration.

The inner .Range function is used to loop over the key-value pairs in the current configmap and creates the data section of the ConfigMap object.

The .Values.configmaps should be defined in the values.yaml file of your chart, it can be in the following format:

configmaps:
- configmap1:
key1: value1
key2: value2
- configmap2:
key1: value1
key2: value2

In conclusion, loops are a powerful tool for automating the deployment and management of your applications on Kubernetes. By understanding the different use cases for loops in Helm charts and how to use them, you can create more complex and dynamic charts that can be easily customized to suit your specific needs.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Mustafa AK
Mustafa AK

Written by Mustafa AK

DevOps Engineer @ relayr | 2 x AWS and 2 x Azure Certified Cloud Engineer | Terraform | CKA | CKAD

Responses (2)

Write a response

Getting below error when using "until" function to wait for some condition to be true as per your example.
" wrong type for value; expected int; got bool"
As per doc, "until" function takes integer param, how did it work for you ?
Please advise if possible.

This creates single configmap and merges it with other value