Helm Chart Include Scope


Helm Chart include scope

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

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’m going to 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 important than how we are going to include within the main file. Let me go ahead and edit the main file.

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

[email protected]:~/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 }}

In case 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 name space structure and avoid duplicates of 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 within this particular file configmap.yaml file. As we discussed earlier, the template folders can have three type of files.

One is the yaml files, which are going to have manifest for Kubernetes and notes.txt file which will have information about this particular charts and the resources.

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

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

[email protected]:~$ 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 what is recommended where I can have the atomic content of different templates in the helper template file and include 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 builtin objects. So I’m including the builtin objects like release.name, chart.appversion, release.service.

[email protected]:~$ 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 this particular global builtin objects and print it in 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.

[email protected]:~$ 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 builtin objects. The reason, because the templates where I’m going to use I need to pass the root context. So that that can be used as a scope and that can be passed as a dot (.) or dollar ($).

Lets try out both the options. Let me edit the template, so when the template is getting invoked I need to pass the scope as well that can be passed as a dot.

[email protected]:~/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 templates scope will get passed to this particular template. And this particular template will have the builtin objects. So that it can pass and the values would get printed. Let me do the dry run.

[email protected]:~$ 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 dot I can use the dollar as well. Dollar is what more recommended. Let’s try out that particular option as well.

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

[email protected]:~$ 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 builtin 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 builtin objects.

Helm Chart Include Scope


Scroll to top