Helm Chart Looping


Helm Chart Looping

In this tutorial, we are going to discuss helm chart looping. Like every other language, the go template language also provides looping using range keyword.

Helm Chart Looping

The syntax is very similar to the other languages where I will be providing the values of collection to the range. Within the block, I can iterate over every element, pick the required values, and do whatever pipeline operation I wanted.

I can do it within the block, and the syntax is very simple.

Lang Used: |-
  {{- range .Values.LangUsed }}
  - {{ . | title | quote }}
  {{- end }}

Let’s go ahead and do a small demo and get a better understanding.

Example

Let me go ahead and add a collection of values as a part of the values file. We get into the mychart within that, I’m going to edit the values file.

Here I’m going to add a key language used, and within that, I will provide a collection of values.

ashok@waytoeasylearn:~$ cat mychart/values.yaml
message: "Welcome to waytoeasylearn"
website: "www.waytoeasylearn.com"
owner:
  name: Ashok Kumar
  place: Hyderabad
  qualification: mca
tags:
  programming: java
  devops: kubernetes
  deployment: helm
  bigdata: hadoop
LangUsed:
  - Java
  - Python
  - Ruby
  - Scala 

Now I will read this specific key and a collection of values and iterate within the template. Let me get into the template and edit the template; within the template, I will get the values.

As a part of the data, I’m going to add the language used. So I will provide language used as a key, and within that, I wanted to have a collection of values, and here I have the range, and it will print every element.

Explanation

So what I’m going to do, I’m going to get the LangUsed and their values. Now I’ll be getting a collection of values, and it will iterate depending on the number of elements within it.

For every element, I will get the current element (Here we represented it as .), that is the scope within this particular block, and I’m going to convert that into a title format that is the first capital letter and put it within the quote.

ashok@waytoeasylearn:~$ cat mychart/templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
  labels:
  {{- with .Values.tags }}
    first: {{ .programming }}
    second: {{ .devops }}
    thrid: {{ .deployment }}
  {{- end }}
data:
  myvalue: "Sample Config Map"
  message: {{ .Values.message }}
  website: {{ upper .Values.website }}
  ownerName: {{ quote .Values.owner.name }}
  ownerPlace: {{ quote .Values.owner.place }}
  LanguagesUsed: |-
    {{- range .Values.LangUsed }}
    - {{ . | title | quote }}
    {{- end }}

So this is a very simple pipeline where I would convert that into a title and quote within it, and the block is getting over. We save the file and do a dry run.

ashok@waytoeasylearn:~$ helm install --debug --dry-run looping ./mychart/
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: looping
LAST DEPLOYED: Fri May 21 13:10:59 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{}
COMPUTED VALUES:
LangUsed:
  Python
  Ruby
  Java
  Scala
message: Welcome to waytoeasylearn
owner:
  name: Ashok Kumar
  place: Hyderabad
  qualification: mca
tags:
  bigdata: hadoop
  deployment: helm
  devops: kubernetes
  programming: java
  website: www.waytoeasylearn.com 
HOOKS:
MANIFEST:
Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: looping-configmap
  labels:
    first: java
    second: kubernetes
    thrid: helm
data:
  myvalue: "Sample Config Map"
  message: Welcome to waytoeasylearn
  website: WWW.WAYTOEASYLEARN.COM
  ownerName: "Ashok Kumar"
  ownerPlace: "Hyderabad"
  LanguagesUsed: |-
    - "Python"
    - "Ruby"
    - "Java"
    - "Scala"

So here it read the collection of values, and it converted the first letter into the capital letter and put it within the quote.

So the main thing that we need to worry about is it read the collection of values and iterated depending on the number of elements within it and printed them as a part of the template.

And this particular range, we can use it by other means as well, where I can read the individual element part of the variable section.

So in a quick summary, we learnt about how to iterate over the collection using the range.

Helm Chart Looping
Scroll to top