Helm Chart Include Template


Helm Chart Include Template

In this tutorial, we will discuss the helm chart include template using the include keyword.

Helm Chart Include Template

In the earlier tutorial, we discussed how to include a template within another template. Also, I mentioned about two ways of including the template.

One is using the keyword template, another one using include. In the earlier example, we saw using the keyword template. While discussing that, I mentioned an important point: the alignment doesn’t matter while including the template. And within the template, whatever the alignment provided will be taken as it is.

Now, let me get into the template where I have defined the details.

[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 }}

Here I have provided the alignments. With the exact alignments, it will get included. And it isn’t easy to identify or assume where it will get included and at what indentations.

See, for example, I might use this particular label in different YAML files and might use indifferent YAML files, different types of indentation.

So having different files for different indentations is going to be a difficult task. That’s where another type of template includes methodology is introduced that is using the keyword include.

Example

Now let’s do the same demo using the template and overcome that using the include keyword. As a part of the helper, I’m going to introduce another template definition.

I’m providing the name mychart.version; assume I do not have any alignments.

[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 }}
{{- define "mychart.version" -}}
app_name: {{ .Chart.Name }}
app_version: "{{ .Chart.Version }}"
{{- end -}}

Now I’m going to include this particular template using the keyword template. So template mychart.version I’m including.

[email protected]:~/mychart/templates$ cat configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
{{- template "mychart.systemlables" $ }}
{{- template "mychart.version" $ }}
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 a dry run and check how it’s working.

[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
Error: YAML parse error on mychart/templates/configmap.yaml: error converting YAML to JSON: yaml: line 10: did not find expected key
helm.go:81: [debug] error converting YAML to JSON: yaml: line 10: did not find expected key
YAML parse error on mychart/templates/configmap.yaml
helm.sh/helm/v3/pkg/releaseutil.(manifestFile).sort         /home/circleci/helm.sh/helm/pkg/releaseutil/manifest_sorter.go:146 helm.sh/helm/v3/pkg/releaseutil.SortManifests         /home/circleci/helm.sh/helm/pkg/releaseutil/manifest_sorter.go:106 helm.sh/helm/v3/pkg/action.(Configuration).renderResources
        /home/circleci/helm.sh/helm/pkg/action/action.go:165
helm.sh/helm/v3/pkg/action.(Install).Run         /home/circleci/helm.sh/helm/pkg/action/install.go:240 main.runInstall         /home/circleci/helm.sh/helm/cmd/helm/install.go:242 main.newInstallCmd.func2         /home/circleci/helm.sh/helm/cmd/helm/install.go:120 github.com/spf13/cobra.(Command).execute
        /go/pkg/mod/github.com/spf13/[email protected]/command.go:850
github.com/spf13/cobra.(Command).ExecuteC         /go/pkg/mod/github.com/spf13/[email protected]/command.go:958 github.com/spf13/cobra.(Command).Execute
        /go/pkg/mod/github.com/spf13/[email protected]/command.go:895
main.main
        /home/circleci/helm.sh/helm/cmd/helm/helm.go:80
runtime.main
        /usr/local/go/src/runtime/proc.go:204
runtime.goexit
        /usr/local/go/src/runtime/asm_amd64.s:1374

It’s going to give the parsing error. The reason is that it doesn’t get appropriately indented. I can overcome this particular problem by using include keyword. Let me go ahead and edit the template file.

Using include keyword

So here I’m going to use include keyword. Include provides the additional option of mentioning how much indentation I wanted for this particular template using the indent keyword.

I included the labels’ lines in this particular template, which need to come after four spaces. So indent I will mention four that will be done using the pipeline and here. I’m going to say the keyword include, name of the template to include, and dot to pass the current scope. Then pipe and how much amount of indent I wanted.

For even better understanding, Let me include the same thing over here and the data section. If I wanted that to get listed along with the data, then the indent should be of 2 spaces.

Here I can mention the same thing with indent as 2.

[email protected]:~/mychart/templates$ cat configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
{{- template "mychart.systemlables" $ }}
{{ include "mychart.version" $ | indent 4 }}
data:
  myvalue: "Sample Config Map"
  message: {{ .Values.message }}
  website: {{ upper .Values.website }}
  ownerName: {{ quote .Values.owner.name }}
  ownerPlace: {{ quote .Values.owner.place }}
{{ include "mychart.version" $ | indent 2 }}

Let me go ahead and do a dry run.

a[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: Mon May 24 13:14: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: 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"
    app_name: mychart
    app_version: "0.1.0"
data:
  myvalue: "Sample Config Map"
  message: Welcome to waytoeasylearn
  website: WWW.WAYTOEASYLEARN.COM
  ownerName: "Ashok Kumar"
  ownerPlace: "Hyderabad"
  app_name: mychart
  app_version: "0.1.0"

Yes, It’s appropriately generated. Here the app_name and app_version got included with four spaces because the label demands four spaces, and along with the data, it got included with two spaces.

It is always recommended to use include because it will have more control and facilitate reusability. This is for the reusability when I mean that I may want that with four indents in some places. And in some places, I may wish to do that with two indents.

So as per the requirement, I can configure. But if I use the template, that’s going to be a difficult task.

Summary

In a quick summary, we have discussed the different types of keywords to use to include the templates and the template keyword, and all the differences. Which one is better and how to include keyword is much better than template keyword while including the templates.

Helm Chart Include Template

Scroll to top