Helm Chart Template Include


Helm Chart Template Include

In this tutorial, we are going to discuss about include template using the include key word in helm chart.

In the earlier tutorial we discussed about 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 while including the template, the alignment doesn’t matter. And within the template, whatever the alignment provided that 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 same alignments, it will get included. And it’s quite difficult to identify or assume where it is going to get included and at what indentations.

See, for example, I might use this particular label in different yaml files and in different yaml files different type of indentation might be used.

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

Example

Now let’s do the same demo using the template and how to 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 because it doesn’t get indented properly. 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 additional option of mentioning how much indentation I wanted for this particular template using indent keyword.

In this particular template, I included the lines for the labels and that needs to come after 4 spaces. So indent I will mention four that will be done using the pipeline and here. I’m going to mention the keyword include and 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 go ahead and include the same thing over here along with the data section. In case if I wanted that to get listed along with the data, then the indent should be of 2 spaces.

Here I can mention 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.

[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 generated properly. here the app_name and app_version got included with four space because label demands 4 spaces and along with the data, it got included with 2 spaces.

Always it is recommended to use include because it will have more control as well as it facilitates the reusability. This is for the reusability when I mean, in some places I may wanted that with 4 indent. And in some places I may wanted that with 2 indent.

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 type of keywords to use to include the templates that is include and template keyword and what are all the differences And which one is better and how include keyword is much better than template keyword while including the templates.

Helm Chart Template Include


Scroll to top