Template Functions


Template Functions

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

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

Now I’m going to add a type of collection within the yaml file where I’m going to have 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.

[email protected]:~/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 function called quote.

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 name, within 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 are n number of predefined functions available as a part of the go template that can be accessed from this particular location or helm supports the functions defined within sprig library as well.

Sprig library

So within sprig library there are 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 function within it. Say for example, within string function we do have upper function which is going to take an argument and convert that into uppercase.

The same way we do have quote function which is going to 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.

[email protected]:~$ 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, do all the computation and that will be replaced 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, it is read as a small case and we passed it to convert it into uppercase and that 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 useful feature called pipeline that we will be seeing it in the next 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.

Template Functions


Scroll to top