Helm Chart Include Scope


Helm Chart include scope

In this tutorial, we will discuss the helm chart include scope that means how to have the template as a part of a separate file and include it in another template.

Helm Chart Include Scope

So let me get into the project. As a part of templates, I’m going to create a file. With prefix underscore, all the helper files will be having the prefix underscore (_). So I’m going to create a file with the name _helpers.tpl.

Example

As a part of this, I will include the template content that we had as a part of the main file. So it’s going to have a name, mychart.systemlabels. And the alignment within the template is more important than how we will include it within the main file. Let me go ahead and edit the main file.

ashok@waytoeasylearn:~/mychart/templates$ cat _helpers.tpl 
{{- define "mychart.systemlables" }}
  labels:
    name: AshokKumar
    place: Hyderabad
    website: www.waytoeasylearn.com
{{- end }}

And I’m going to include the template with the name mychart.systemlabels. It also proves the name of the template is universal.

ashok@waytoeasylearn:~/mychart/templates$ cat configmap.yaml 
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 }}

If I have any template with duplicate names, the one that is getting loaded latest will be used. So always, it is recommended to follow the namespace structure and avoid duplicating the template file names.

So what we have done, we created another file _helpers.tpl and included the template content within it.

And the same template is referred to within this particular file configmap.yaml file. As we discussed earlier, the template folders can have three types of files.

One is the YAML files, which will have manifest for Kubernetes, and the notes.txt file, which will have information about these particular charts and the resources.

We will have a detailed discussion about the notes files. And helpers file will have all the helpers templates, which will get included in the main templates.

Now, let me go ahead and do a dry run.

ashok@waytoeasylearn:~$ helm install --debug --dry-run templatedemo  ./mychart/
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: templatedemo
LAST DEPLOYED: Sun May 23 12:36:40 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: templatedemo-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"

So the labels were included as a part of the main template. This is another way of including the template.

This is recommended to have the atomic content of different templates in the helper template file and include it in the main template files.

Builtin Global objects

Now let us understand what if we include the built-In global objects as a part of the templates and how the scope will get transferred into the templates.

Now I’m going to edit the same template file. And as a part of the labels, I am going to include few built-in objects. So I include the built-in objects like release.namechart.appversionrelease.service.

ashok@waytoeasylearn:~$ cat mychart/templates/_helpers.tpl 
{{- define "mychart.systemlables" }}
  labels:
    name: AshokKumar
    place: Hyderabad
    website: www.waytoeasylearn.com
    app.kubernetes.io/instance: "{{ $.Release.Name }}"
    app.kubernetes.io/version: "{{ $.Chart.AppVersion }}"
    app.kubernetes.io/managed-by: "{{ $.Release.Service }}"
{{- end }}

So it should pick these particular global builtin objects and print them in the labels section. That’s going to be a surprise where the values will be blank.

Let’s go ahead and do a dry run of this particular chart.

ashok@waytoeasylearn:~$ helm install --debug --dry-run templatedemo  ./mychart/
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: templatedemo
LAST DEPLOYED: Sun May 23 12:36:40 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: templatedemo-configmap
  labels:
    name: AshokKumar
    place: Hyderabad
    website: www.waytoeasylearn.com
    app.kubernetes.io/instance: ""
    app.kubernetes.io/version: ""
    app.kubernetes.io/managed-by: ""
data:
  myvalue: "Sample Config Map"
  message: Welcome to waytoeasylearn
  website: WWW.WAYTOEASYLEARN.COM
  ownerName: "Ashok Kumar"
  ownerPlace: "Hyderabad"

I’m not having any value for the built-in objects. The reason, because of the templates where I’m going to use, I need to pass the root context. So that can be used as a scope, which can be given as a dot (.) or dollar ($).

Let’s try out both options. Let me edit the template, so when the template is invoked, I need to pass the scope that can be given as a dot.

ashok@waytoeasylearn:~/mychart/templates$ cat configmap.yaml 
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 }}

This means the current template’s scope will get passed to this particular template. And this specific template will have built-in objects. So that it can give and the values would get printed. Let me do the dry run.

ashok@waytoeasylearn:~$ helm install --debug --dry-run templatedemo  ./mychart/
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: templatedemo
LAST DEPLOYED: Sun May 23 12:36:40 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: templatedemo-configmap
  labels:
    name: AshokKumar
    place: Hyderabad
    website: www.waytoeasylearn.com
    app.kubernetes.io/instance: "templatedemo"
    app.kubernetes.io/version: "1.16.0"
    app.kubernetes.io/managed-by: "Helm"
data:
  myvalue: "Sample Config Map"
  message: Welcome to waytoeasylearn
  website: WWW.WAYTOEASYLEARN.COM
  ownerName: "Ashok Kumar"
  ownerPlace: "Hyderabad"

Yes, it’s able to get the values. Instead of the dot, I can use the dollar as well. The dollar is what is more recommended. Let’s try out that particular option as well.

ashok@waytoeasylearn:~/mychart/templates$ cat configmap.yaml 
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 go ahead and do the dry run.

ashok@waytoeasylearn:~$ helm install --debug --dry-run templatedemo  ./mychart/
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: templatedemo
LAST DEPLOYED: Sun May 23 12:36:40 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: templatedemo-configmap
  labels:
    name: AshokKumar
    place: Hyderabad
    website: www.waytoeasylearn.com
    app.kubernetes.io/instance: "templatedemo"
    app.kubernetes.io/version: "1.16.0"
    app.kubernetes.io/managed-by: "Helm"
data:
  myvalue: "Sample Config Map"
  message: Welcome to waytoeasylearn
  website: WWW.WAYTOEASYLEARN.COM
  ownerName: "Ashok Kumar"
  ownerPlace: "Hyderabad"

Now also I’m able to get the global built-in objects. So that means whenever I’m going to use the named templates, I need to pass the scope if I have to use any of the global built-in objects.

Helm Chart Include Scope
Scroll to top