Helm: "Template" keyword

6/15/2021

Can someone explain to me what the role of the keyword "template" is in this code :

apiVersion: v1
kind: Secret
metadata:
  name: {{ template "identity-openidconnect" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ template "microService.name" . }}
    release: "{{ .Release.Name }}"
    xxxx
xxxxxxxxxxxx
-- AllaouaA
kubernetes
kubernetes-helm

1 Answer

6/15/2021

The keyword "template" means, that Helm will find the previously created template and complete the yaml file according to the template in the template. It has to be created in advance. This type of construction allows you to refer to the same scheme many times.

For example, we can define a template to encapsulate a Kubernetes block of labels:

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

Now we can embed this template inside of our existing ConfigMap, and then include it with the template action:

{{- define "mychart.labels" }}
  labels:
    generator: helm
    date: {{ now | htmlDate }}
{{- end }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
  {{- template "mychart.labels" }}
data:
  myvalue: "Hello World"
  {{- range $key, $val := .Values.favorite }}
  {{ $key }}: {{ $val | quote }}
  {{- end }}

When the template engine reads this file, it will store away the reference to mychart.labels until template "mychart.labels" is called. Then it will render that template inline. So the result will look like this:

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: running-panda-configmap
  labels:
    generator: helm
    date: 2016-11-02
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "pizza"

Note: a define does not produce output unless it is called with a template, as in this example.

For more info about templates you can read this page.

-- Mikołaj Głodziak
Source: StackOverflow