Helm Chart Variables


Helm Chart Variables

This tutorial will discuss what helm chart variables/helm chart template variables are and how to use them.

Helm Chart Variables

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

In the same way, within the range looping, if I wanted to assign or process any specific element, I need to set it to variable and process it within the block. For that, I need to set a value to 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 an example and get a 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 have a with block, and outside the block, I’m reading the release name and assigning it 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 will 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 built-in 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 at the beginning and the end because it’s not going to print anything, and if I don’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.

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 }}
  {{- $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.

ashok@waytoeasylearn:~$ 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 newline character where we are reading or assigning the variable.

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

Example on looping

Now let us see another example where I will 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.

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 $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 read within the collection.

So 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.

ashok@waytoeasylearn:~$ 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 another way of reading the variable. And as a part of the variable, another essential thing to understand is the dollar.

dollar ($)

The 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 examples about the dollar variable as well. So here I am, adding the value by accessing the global variable dollar. So that will have the root context.

ashok@waytoeasylearn:~$ 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, it will get deployed into the Kubernetes cluster. Once we learn every 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.

ashok@waytoeasylearn:~$ 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 learned three major concepts.

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

Helm Chart Variables
Scroll to top