Helm Template Functions


Helm Template Functions

In this tutorial, we are going to discuss more helm template functions. As a part of the values YAML file or the templates YAML file, we can put in functions to compute the values.

Template Functions

Let us see some example that’s going to give us more clarity. Let me get into the values file and add some values. I’m going to edit the value file. Here we were adding key-value pairs.

Now I’m going to add a type of collection within the YAML file where I will have a key as owner, and it’s going to have a couple of key-value pairs, name, and place.

message: Welcome to waytoeasylearn
website: www.waytoeasylearn.com
owner:
  name: Ashok Kumar
  place: Hyderabad

So this is another example to demonstrate how to access the collection as well. Let me get into the templates. Let me edit the template file. Here I’m going to add and access the values.

ashok@waytoeasylearn:~/mychart$ cat templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Sample Config Map"
  message: {{ .Values.message }}
  website: {{ upper .Values.website }}
  ownerName: {{ quote .Values.owner.name }}
  ownerPlace: {{ quote .Values.owner.place }}

Here I’m adding ownerName as key, and I’m adding template derivative within that; I am adding a quote function.

So the function quote will be getting one argument, and it’s going to quote that specific value that’s being read from the value file.

So within the value file, it’s going to read the name, within the owner, and it’s going to surround it with double quotes, the same way it’s going to read the website from the values file, and it is going to convert it into uppercase.

There is n number of predefined functions available as part of the go template that can access from this particular location or helm supports the functions defined within the sprig library.

Sprig library

So within the sprig library, there is n number of useful template functions for Go templates. Here we can use any of those functions.

We do have lots of simple to complex functions within it. Say, for example, within string function; we have an upper function that will take an argument and convert that into uppercase.

In the same way, we have a quote function that will convert or enclose the string using a double quote.

So I’m going to call the function within the template directive and pass the value required for that particular function as an argument and pass the required argument to those functions.

Let me save this particular template, and I’m going to do a dry run. Let me get into the parent directory, where I do have the chart resources. My chart resources are available as a part of mychart.

ashok@waytoeasylearn:~$ helm install --dry-run --debug valuesetfunction ./mychart/
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: valuesetfunction
LAST DEPLOYED: Wed May 19 21:44:32 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{}
COMPUTED VALUES:
message: Welcome to waytoeasylearn
owner:
  name: Ashok Kumar
  place: Hyderabad
website: www.waytoeasylearn.com
HOOKS:
MANIFEST:
Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: valuesetfunction-configmap
data:
  myvalue: "Sample Config Map"
  message: Welcome to waytoeasylearn
  website: WWW.WAYTOEASYLEARN.COM
  ownerName: "Ashok Kumar"
  ownerPlace: "Hyderabad"

So it’s going to read the values file and do all the computation, which will replace within the config map YAML file.

Here we can see it read the ownerName and ownerPlace, and those are enclosed within the double-quotes. And the website, is read as a small case, and we passed it to convert it into uppercase, which is getting passed to this particular YAML file.

Functions play a very significant role where I can convert whatever the data that I wanted and dynamically, it’ll help me to compute the required values.

And along with functions, we will be using another helpful feature called pipeline that we will see in the following tutorial.

Summary

So in a quick summary, We have seen how to use functions as a part of the template directive and the introduction to another useful library called Sprig.

Helm Template Functions
Scroll to top