Helm Chart Variables


Helm Chart Variables

In this tutorial, we are going to discuss about what are helm chart variables and how to use them.

As we discussed in the earlier tutorial, whenever I wanted to inject a value within a with block, which is out of its scope I need to use the variables.

The same way within the range looping if I wanted to assign or process any specific element then I need to assign it to variable and process it within the block. For that, I need to assign a value into a variable.

The syntax for that is the name of the variable with a dollar sign ($) and the special assignment operator that we will be using is := And then the value that we are reading from the template or a literal value.

Example

Let’s go ahead and do a example and get better understanding. Let me modify the template. Within the template, I’m going to add few lines.

So what I’m doing over here, I do have a with block and outside the block I’m reading the release name and assigning in to a variable called relName.

And that is starting with the dollar sign ($) that is the syntax and the operated to assign the value is := and this particular variable can be accessed across the template and within the with block.

So I am going to read that specific variable that is $relName and print it with the key Release. Earlier when we are accessing this particular value Release.Name that is the builtin object I was not able to read.

Now what I am doing, I am assigning it to a variable and that variable will have the scope across the template.

And over here I have added a dash in the beginning as well as in the end, because it’s not going to print anything and if I doesn’t provide this particular dash, it’s going to add a new line character. And there will be a new line or empty line within that YAML file. Even though that is valid, that’s not recommended.

[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 }}
  {{- $relName := .Release.Name }}
  {{- with .Values.tags }}
  Programming: {{ .programming | default "NA" | quote }}
  DevOps: {{ .devops | quote }}
  Deployment: {{ .deployment | upper | quote }}
  BigData: {{ .bigdata | quote }}
  Release: {{ $relName }}
  {{- end }}

Now let me go ahead and execute the dry run.

[email protected]:~$ helm install --debug --dry-run vartest ./mychart/
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: vartest
LAST DEPLOYED: Fri May 21 15:15:57 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: vartest-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"
  Programming: "java"
  DevOps: "kubernetes"
  Deployment: "HELM"
  BigData: "hadoop"
  Release: vartest

It’s able to print the release name. That’s the name that I provided as a part of the dry run statement. And there is no new line character where we are reading or assigning the variable.

All other lines within the with block is same as we had earlier. And this is a proof where we can assign a builtin object into a variable and read it within the with block.

Example on looping

Now let us see another example where I’m going to assign an element within the range into a variable and read it within the range block.

So within the template let me go ahead and edit the template.

[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 $index, $topping := .Values.LangUsed }}
     - {{ $index }} : {{ $topping }}
    {{- end }}

So within the range, what I’m doing I’m assigning that specific element that is Values.LangUsed as a part of topping and I’m going to have the index.

After that I am printing both the index and the individual element that is being read within the collection.

So I I’ll be having key as the index and value as the actual element within the collection. Now I’m going to do a dry run.

[email protected]:~$ helm install --debug --dry-run vartest ./mychart/
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: vartest
LAST DEPLOYED: Fri May 21 15:15:57 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: vartest-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: |-
     - 0 : Python
     - 1 : Ruby
     - 2 : Java
     - 3 : Scala

So I’m getting the index and the actual value. This is one another way of reading the variable. And as a part of the variable, another important thing to understand is the dollar.

dollar ($)

Dollar is a global variable and this particular variable will always point to the root context. So I can access the global objects using the variable dollar.

Let us see some example about dollar variable as well. So here I am adding the value by accessing the global variable dollar. So that will have the root context.

[email protected]:~$ cat mychart/templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
  labels:
    helm.sh/chart: "{{ $.Chart.Name }}-{{ $.Chart.Version }}"
    app.kubernetes.io/instance: "{{ $.Release.Name }}"
    app.kubernetes.io/version: "{{ $.Chart.AppVersion }}"
    app.kubernetes.io/managed-by: "{{ $.Release.Service }}"
data:
  myvalue: "Sample Config Map"
  message: {{ .Values.message }}
  website: {{ upper .Values.website }}
  ownerName: {{ quote .Values.owner.name }}
  ownerPlace: {{ quote .Values.owner.place }}

Now I can access the global object. Here I am accessing the global object, Chart.Name, Chart.Version, Release details as well as the Chart.AppVersion.

Let me go ahead and execute this particular chart and for almost all the examples I’m using the dry run because there is no need to go ahead and deploy.

If a dry run is good definitely it will get deployed into the Kubernetes cluster. Once we learn each and every individual concept, we can consolidate everything together and release them into the Kubernetes cluster.

I’m going to do a dry run of the chart mychart.

[email protected]:~$ helm install --debug --dry-run vartest ./mychart/
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: vartest
LAST DEPLOYED: Fri May 21 15:15:57 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: vartest-configmap
  labels:
    helm.sh/chart: "mychart-0.1.0"
    app.kubernetes.io/instance: "vartest"
    app.kubernetes.io/version: "1.16.0"
    app.kubernetes.io/managed-by: "Helm"
data:
  myvalue: "Sample Config Map"
  message: Welcome to waytoeasylearn
  website: WWW.WAYTOEASYLEARN.COM
  ownerName: "Ashok Kumar"
  ownerPlace: "Hyderabad"

So I’m able to get the chart releases and release Name, the version of the helm that we’re using and what it is managed by.

Summary

So in a quick summary we learnt three major concepts.

That is, how to inject variables into a with block and how to assign a individual element of a range and access the individual element and its index within the range looping and how to access the global variable dollar and use the global objects in the helm chart.

Helm Chart Variables


Scroll to top