Helm Chart Named Templates


Helm Chart Named Templates

In this tutorial, we are going to discuss  helm chart named templates. 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 will start with an underscore (_).

Helm Chart Named Templates

If you remember earlier, when we are creating the charts, by default, it was making 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; that’s what we will 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 examples.

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 an underscore as a prefix, or I can include it in the same file.

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

So to start with, I’m going to include it 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 supports the syntax of using the namespaces.

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.

Suppose 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 using the keyword template.

There is another option using include that we will be seeing 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 will include the template using that template keyword.

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 will discuss how to have this particular template in a separate file and include it within another template file.

Helm Chart Named Templates

Scroll to top