Template Pipeline


Template Pipeline

In this tutorial, we are going to discuss about template pipeline and default values. One of the powerful features of the template language is its concept of pipelines (|).

Along with functions the pipeline will be used more frequently to add more value to the calculations.

Say for example, I may want to convert a particular string into an uppercase and then do further processing by adding double quotes, then I might do some other operation.

So this particular pipeline got derived from the Linux concepts. Let’s do some example and get some better understanding.

Let me go into the values file and add qualification key and its corresponding value.

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

Now let me get into the templates file, within templates I’m going to add a key qualification and I’m going to have a derivative where it’s going to pick the value from qualification and that will be piped to a function called upper.

After that i added another key date and here also I’m going to have a declarative and that will be piped to a function called quote.

[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 }}
  qualification: {{ .Values.owner.qualification | upper | quote }}
  date: {{ now | date "2006-01-02" | quote }}
Explanation

So what will happen, the qualification will be read from the values file and it will be converted into uppercase. Then the output of it will be piped to another function called quote. So it’s going to quote the output and put that as the final calculated value.

The same way I do have another derivative where I do have a function called now, that’s going to get the current timestamp and it’s going to pipe it to another function called date, and that’s going to format the date in this particular order (YEAR-MONTH-DAY).

The way Go template works for the date formatting is quite different. So you can verify the same page of sprig functions. Here a lot more details provided as a part of the date functions.

So here the current timestamp will be formatted in this particular structure and output will be enclosed within double quotes.

Let me go ahead and do a dry run.

[email protected]:~$ helm install --dry-run --debug templatepipeline ./mychart/
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: templatepipeline
LAST DEPLOYED: Thu May 20 10:15:02 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
  qualification: mca
website: www.waytoeasylearn.com
HOOKS:
MANIFEST:
Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: templatepipeline-configmap
data:
  myvalue: "Sample Config Map"
  message: Welcome to waytoeasylearn
  website: WWW.WAYTOEASYLEARN.COM
  ownerName: "Ashok Kumar"
  ownerPlace: "Hyderabad"
  qualification: "MCA"
  date: "2021-05-20"

So it took the qualification from values file and converted that into uppercase and then enclosed within the double quotes.

The same way the next one. It took the current timestamp converted into the structure that I had defined and enclosed within the double quotes.

So this is one of the very useful functions when we are moving on to the condition, verification’s or looping, lot more places this will be very, very useful.

Also, to apply the logical operators this will be very useful. Along with this, we need to have understanding on the default value as well.

default value

Whenever we do some computation, we may not get the value or that may not be available from the place where we are calculating the value.

So we need to provide some default value so that it can be replaced and the value is getting calculated to provide it to the template.

Let me go ahead and add some additional details to the templates. Here I’m going to provide another line called email and where I’m going to have a derivative.

[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 }}
  qualification: {{ .Values.owner.qualification | upper | quote }}
  date: {{ now | date "2006-01-02" | quote }}
  email: {{ .Values.email | default "[email protected]" | quote }}

So it’s going to pick the values contact from the values file. But I have not added the email within the values file. Anyway we can go ahead and verify.

[email protected]:~$ helm install --dry-run --debug templatepipeline ./mychart/
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: templatepipeline
LAST DEPLOYED: Thu May 20 10:15:02 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
  qualification: mca
website: www.waytoeasylearn.com
HOOKS:
MANIFEST:
Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: templatepipeline-configmap
data:
  myvalue: "Sample Config Map"
  message: Welcome to waytoeasylearn
  website: WWW.WAYTOEASYLEARN.COM
  ownerName: "Ashok Kumar"
  ownerPlace: "Hyderabad"
  qualification: "MCA"
  date: "2021-05-20"
  email: "[email protected]"

So in case, if the value is not calculated or it is not derived from the values file, it is going to use this value as the default one.

Let me go ahead and check the values yaml file as well.

[email protected]:~$ cat mychart/values.yaml 
message: Welcome to waytoeasylearn
website: www.waytoeasylearn.com
owner:
  name: Ashok Kumar
  place: Hyderabad
  qualification:mca

Here I do not have value for email. So it replaced the default value that we had provided.

Now, I’m going to run the dry run on the same chart resources but here I’m passing the contact value using the argument –set.

[email protected]:~$ helm install --dry-run --debug templatepipeline ./mychart/ --set email="[email protected]"
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: templatepipeline
LAST DEPLOYED: Thu May 20 10:15:02 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
  qualification: mca
website: www.waytoeasylearn.com
HOOKS:
MANIFEST:
Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: templatepipeline-configmap
data:
  myvalue: "Sample Config Map"
  message: Welcome to waytoeasylearn
  website: WWW.WAYTOEASYLEARN.COM
  ownerName: "Ashok Kumar"
  ownerPlace: "Hyderabad"
  qualification: "MCA"
  date: "2021-05-20"
  email: "[email protected]"

So within the calculated value, it replaced the value that we provided as part of the command prompt using the argument –set.

So in a quick summary we have seen how to use the template pipeline and the default value within the templates.

Template Pipeline


Scroll to top