Go templates and values
Templates produce text before Kubernetes sees it
A Helm chart uses Go text templates plus Helm functions. The dot (.) is the current input context. At the top level, .Values contains merged values and .Release contains release metadata. Rendering is not a Kubernetes operation by default. In this example a ConfigMap records a replica count as text; it does not create replicas. The chart is under lab-fixtures/helm-engineering/catalog.
apiVersion: v1kind: ConfigMapmetadata: name: {{ printf "%s-catalog" .Release.Name | quote }} labels: {{- include "catalog.v1.labels" . | nindent 4 }} {{- with .Values.extraLabels }} {{- toYaml . | nindent 4 }} {{- end }}data: message: {{ required "message must not be empty" .Values.message | quote }} replicas: {{ .Values.replicaCount | quote }} {{- if .Values.showBanner }} banner: {{ .Values.banner | quote }} {{- end }} {{- range $index, $item := .Values.items }} item-{{ $index }}: {{ $item | quote }} {{- end }}Reuse a helper and preserve indentation
The helper emits two labels. define registers a named template; include returns its output as a string, so the pipeline passes that string into nindent 4, which adds a newline and four spaces before each line. The trim marker on the opening action removes preceding whitespace. Naming helpers with a chart-specific prefix and version reduces collisions across dependencies. A helper receives the scope explicitly passed to it; $ inside that helper refers to that passed root, not necessarily the original caller.
{{- define "catalog.v1.labels" -}}app.kubernetes.io/name: catalogapp.kubernetes.io/instance: {{ .Release.Name | quote }}{{- end -}}Control flow and scope
if includes the banner only when showBanner is true. with changes dot to extraLabels while inside its block; toYaml serializes that map. range walks items with index and value; an empty list adds no item entries. Save the root in a variable or use $.Release.Name inside a range in the main template if needed. Avoid trimming both sides of adjacent actions indiscriminately: two YAML keys can end up on one line. The extraLabels schema prevents collisions with the fixed label keys.
{{- range $index, $item := .Values.items }}item-{{ $index }}: {{ $item | quote }}{{- end }}Preserve types and meaningful zero values
quote is appropriate for ConfigMap data, whose values must be strings. A Deployment spec.replicas instead expects an integer. Helm default considers false and zero empty, so default 2 applied to replicaCount can unexpectedly replace an explicit zero; rely on values.yaml and a schema instead. required is useful for a nonempty message, not as a substitute for precise numeric or boolean validation.
# Partial Deployment example (not a complete object)replicas: {{ .Values.replicaCount }}# Avoid: replicas: {{ default 2 .Values.replicaCount }}Predict and review the render
With release demo and the default values, expect demo-catalog, two fixed labels plus team=learning, message Catalog ready, replicas as string "2", two item keys and no banner. The alternate values deliberately set zero replicas and an empty list while enabling the banner. Use only a reviewed local chart, no plugins, no dependency update and no server dry-run. lookup can read live resources through Helm’s cluster credentials when server-backed rendering is used; local output may differ. Never expose real secrets in values or logs. Helm is unavailable here; these are predictions, not captured output.
helm template demo ./catalog -n helm-study -f alternate.yamlCheck Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Go templates and values — knowledge check
Apply concepts to the original offline chart fixtures.
What does include return?
What is dot inside with .Values.extraLabels?
What happens to item entries with an empty list?
Why avoid default 2 for replicaCount?
Why quote replicas in this ConfigMap?