Helm Chart Named Templates


Helm Chart Named Templates

In this tutorial, we are going to discuss about named templates in helm chart. As we discussed earlier, we used to keep the templates as a part of the templates folder and most of the files within the templates folder are treated as Kubernetes manifest. And that is one exception where I can have notes.txt and I can have other files as well which is going to start with underscore (_).

If you remember earlier, when we are creating the charts, by default it was creating some sample files, it had underscore (_).

E.g: _helpers.tpl

[email protected]:~/mychart/templates$ ls
NOTES.txt  _helpers.tpl  deployment.yaml  hpa.yaml  ingress.yaml  service.yaml  serviceaccount.yaml  tests

I can have multiple templates in the same file and I can give a name to it and use it elsewhere as well that’s what we are going to learn in this particular tutorial.

So I can define a template and give a name to it and include this particular template in other locations as well. This will help us to leverage the reusability. Let us understand by doing some example.

Example

Let me open the template file. Here, I’m going to delete all the labels and include that as a predefined template and I can have the templates as a separate file with underscore as a prefix or I can include it in the same file as well.

In this tutorial we will discuss on templates include it in the same file.

So to start with I’m going to include in the same file. Here I’m going to have a template where I’m going to define the name of the template as mychart.systemlabels and it’s supports the syntax of using the namespaces as well.

This works very similar to the include or import that we use in the other programming languages.

[email protected]:~$ cat mychart/templates/configmap.yaml 
{{- define "mychart.systemlables" }}
  labels:
    name: Ashok Kumar
    place: Hyderabad
    website: www.waytoeasylearn.com
{{- end }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
  {{- template "mychart.systemlables" }}
data:
  myvalue: "Sample Config Map"
  message: {{ .Values.message }}
  website: {{ upper .Values.website }}
  ownerName: {{ quote .Values.owner.name }}
  ownerPlace: {{ quote .Values.owner.place }}

Now, let me do the dry run.

[email protected]:~$ helm install --debug --dry-run mytemplate ./mychart/
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: mytemplate
LAST DEPLOYED: Sat May 22 12:56:22 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: looping-configmap
  labels:
    name: Ashok Kumar
    place: Hyderabad
    website: www.waytoeasylearn.com
data:
  myvalue: "Sample Config Map"
  message: Welcome to waytoeasylearn
  website: WWW.WAYTOEASYLEARN.COM
  ownerName: "Ashok Kumar"
  ownerPlace: "Hyderabad"

So what happened? It automatically included the labels and it’s not listing the actual template that we had defined within the manifest file.

If you observe one thing very carefully. Let me get into the configuration file. This is having proper alignment. And wherever I’m going to include this particular alignment will not be considered, when I’m going to include using the keyword template.

There is another option using include that we will be seeing it later.

importance of indentation

And this particular include is not going to consider this particular space. I can go ahead and delete them as well. Let me delete them (spaces in front of {{- template “mychart.systemlables” }} ).

[email protected]:~$ cat mychart/templates/configmap.yaml 
{{- define "mychart.systemlables" }}
  labels:
    name: AshokKumar
    place: Hyderabad
    website: www.waytoeasylearn.com
{{- end }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
{{- template "mychart.systemlables" }}
data:
  myvalue: "Sample Config Map"
  message: {{ .Values.message }}
  website: {{ upper .Values.website }}
  ownerName: {{ quote .Values.owner.name }}
  ownerPlace: {{ quote .Values.owner.place }}

Now, let me do the dry run.

[email protected]:~$ helm install --debug --dry-run mytemplate ./mychart/
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: mytemplate
LAST DEPLOYED: Sat May 22 12:56:22 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: looping-configmap
  labels:
    name: AshokKumar
    place: Hyderabad
    website: www.waytoeasylearn.com
data:
  myvalue: "Sample Config Map"
  message: Welcome to waytoeasylearn
  website: WWW.WAYTOEASYLEARN.COM
  ownerName: "Ashok Kumar"
  ownerPlace: "Hyderabad"

This is also producing the same result. So within the template, the important thing to remember is the space or indentation. Within the inner template and not in the place where we are going to include the template using that template keyword.

The reason why I am concentrating on the indentation, while doing the template programming for the yaml file the indentation is where most of the mistakes used to happen.

Summary

So in a quick summary we have seen how to create a template and include that within another template.

In the next section we are going to discuss about how to have this particular template in a separate file and include within another template file.

Helm Chart Named Templates


Scroll to top