Helm include templates

12/20/2019

Please! Is it possible to squash multiple helm templates into one and then refer to it as a one-liner in a deployment file?

EG:

 {{- define "foo.deploy" -}} 
 value:
   {{- include "foo.1" . | nindent 6 }}
   {{- include "foo.2" . | nindent 6 }}
   {{- include "foo.3" . | nindent 6 }}

And then do an {{- include "foo.deploy" . }} in a separate deployment file.

Which should then contain foo.1, foo.2 and foo.3, and their respective definitions.

As opposed to literally writing out all three different 'includes' especially if you've got loads.

Much appreciated,

Thanks,

-- Tee Hammed
kubernetes
kubernetes-helm

1 Answer

12/20/2019

A named template (sometimes called a partial or a subtemplate) is simply a template defined inside of a file, and given a name. We’ll see two ways to create them, and a few different ways to use them. Template names are global. As a result of this, if two templates are declared with the same name the last occurrence will be the one that is used. Since templates in subcharts are compiled together with top-level templates, it is best to name your templates with chart specific names. A popular naming convention is to prefix each defined template with the name of the chart: {{ define "mychart.labels" }}.

More information about named templates you can find here: named-template.

Proper configuration file should look like:

{{/* Generate basic labels */}}
{{- define "mychart.labels" }}
  labels:
    generator: helm
    date: {{ now | htmlDate }}
{{- end }}

In your case part of file should looks like:

{{- define "foo.deploy" -}} 
{{- include "foo.1" . | nindent 6 }}
{{- include "foo.2" . | nindent 6 }}
{{- include "foo.3" . | nindent 6 }}
{{ end }}
-- MaggieO
Source: StackOverflow