Helm Chart Looping


Helm Chart Looping

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

The syntax is very similar to the other languages where I will be providing the values of collection to the range and within the block I can iterate over each and every element and pick the required values and do what ever the 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 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’m going to provide a collection of values.

[email protected]:~$ 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’m going to read this specific key and a collection of values and iterate within the template. Let me get into the template, edit the template, within the template I’m going to 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 do have the range and each and every element will be printed.

Explanation

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

For each and every element I am going to 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 within the quote.

[email protected]:~$ 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.

[email protected]:~$ 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 in to capital letter and put 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