Lesson 03 Templating Fundamentals
Theme:
Lesson 03 · Slide 1

Templating Fundamentals

Go template syntax, built-in objects, control structures, functions, and whitespace control.

What you will learn

  • How Helm uses Go templates to generate Kubernetes YAML
  • The five built-in objects: .Values, .Release, .Chart, .Capabilities, .Template
  • Control structures: if/else, with, range
  • Essential template functions and pipelines
  • Whitespace control with trim markers
Lesson 03 · Slide 2

Go Template Syntax

Helm templates are YAML files with Go template actions enclosed in double curly braces.

# templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-config
  namespace: {{ .Release.Namespace }}
data:
  environment: {{ .Values.env | default "production" | quote }}
  version: {{ .Chart.AppVersion }}

Pipelines

Functions chain together with the | (pipe) operator, flowing left to right — just like Unix pipes.

# value flows: .Values.env -> default -> quote
environment: {{ .Values.env | default "production" | quote }}
Lesson 03 · Slide 3

Built-in Objects

Every Helm template has access to five top-level objects:

ObjectDescriptionCommon Fields
.ValuesUser-supplied values from values.yaml and --set.Values.replicaCount, .Values.image.tag
.ReleaseMetadata about the current release.Release.Name, .Release.Namespace, .Release.IsInstall, .Release.IsUpgrade
.ChartContents of Chart.yaml.Chart.Name, .Chart.Version, .Chart.AppVersion
.CapabilitiesCluster capabilities.Capabilities.KubeVersion, .Capabilities.APIVersions
.TemplateCurrent template info.Template.Name, .Template.BasePath
Lesson 03 · Slide 4

Control Structures

if / else

{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ .Release.Name }}
{{- else }}
# Ingress disabled
{{- end }}

with (scope change)

{{- with .Values.nodeSelector }}
nodeSelector:
  {{- toYaml . | nindent 2 }}
{{- end }}

with re-scopes . to the value, and skips the block if the value is empty.

range (iteration)

env:
{{- range .Values.env }}
  - name: {{ .name }}
    value: {{ .value | quote }}
{{- end }}
Lesson 03 · Slide 5

Essential Template Functions

FunctionPurposeExample
defaultFallback value{{ .Values.port | default 8080 }}
quoteWrap in double quotes{{ .Values.env | quote }}
toYamlMarshal to YAML string{{ toYaml .Values.labels | nindent 4 }}
nindentNewline + indent N spaces{{ toYaml . | nindent 6 }}
indentIndent N spaces (no newline){{ toYaml . | indent 4 }}
upper / lowerCase conversion{{ .Values.env | upper }}
trimStrip leading/trailing whitespace{{ .Values.name | trim }}
requiredFail if value is empty{{ required "image.tag is required" .Values.image.tag }}
Lesson 03 · Slide 6

Whitespace Control

Go templates preserve all whitespace outside actions. Use trim markers ({{- and -}}) to eat adjacent whitespace.

Without trim markers

metadata:
  labels:
    {{ if .Values.team }}
    team: {{ .Values.team }}
    {{ end }}
    app: myapp

Produces blank lines above and below the conditional.

With trim markers

metadata:
  labels:
    {{- if .Values.team }}
    team: {{ .Values.team }}
    {{- end }}
    app: myapp

Clean output — no extra blank lines.

Tip: {{- trims whitespace before the action. -}} trims whitespace after. Use helm template frequently to check your output.
Lesson 03 · Slide 7

Practical Example: Full Template

Putting it all together — a Deployment template using values, conditionals, range, and pipelines:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}-app
  labels:
    app.kubernetes.io/name: {{ .Chart.Name }}
    app.kubernetes.io/instance: {{ .Release.Name }}
spec:
  replicas: {{ .Values.replicaCount | default 1 }}
  selector:
    matchLabels:
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          ports:
          {{- range .Values.ports }}
            - containerPort: {{ .port }}
              protocol: {{ .protocol | default "TCP" }}
          {{- end }}
          {{- with .Values.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
Lesson 03 · Slide 8

Key Takeaways

Next Steps

Read the Theory for deeper coverage, then work through the Lab Guide exercises.

1 / ?