Sub topic 2
Your first template: Go template actions
This chapter assumes you can read Kubernetes Deployment and Service YAML, have Helm 3 installed, and have seen the chart layout of Chart.yaml, values.yaml and templates/. It covers how Helm turns a template file into a manifest.
What a template is
Every file in templates/ is a Go template. Helm reads the file and copies plain text to the output unchanged. Anything inside {{ ... }} is an action. Helm evaluates the action and replaces it with the result. The result must be valid YAML for Kubernetes.
The word after {{ is usually an object path that starts with a dot. The dot (.) is the current context. At the top of a template, the context is the root object, which holds .Values, .Release and .Chart. The next chapter covers these objects in detail. Here you only need to know that .Values.replicaCount reads the key replicaCount from values.yaml.
The actions you will use
| Action | Purpose |
|---|---|
| `{{ .Values.x }}` | Insert a value |
| `{{/* text */}}` | Comment, removed from the output |
| `{{ if ... }}` ... `{{ end }}` | Output a block only when a condition holds |
| `{{ with ... }}` ... `{{ end }}` | Run a block with the context changed to a value |
| `{{ range ... }}` ... `{{ end }}` | Repeat a block for each item |
| `{{ define ... }}` | Declare a named template |
This chapter uses value insertion, comments and a simple if. Later chapters cover the rest.
A worked example
Start with a values.yaml:
replicaCount: 2
image:
repository: nginx
tag: "1.27.0"
service:
type: ClusterIP
port: 80Now create templates/service.yaml:
{{/* A Service for the web application */}}
apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name }}-{{ .Chart.Name }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: 80
selector:
app.kubernetes.io/instance: {{ .Release.Name }}Each action reads one value. .Release.Name is the name you give to helm install. .Chart.Name comes from Chart.yaml. A dotted path such as .Values.service.port walks down through the nested keys in values.yaml.
Render it without a cluster
The helm template command renders templates locally and prints the manifests. It needs no cluster.
helm template my-release ./webappWith the chart name webapp, the Service renders as:
apiVersion: v1
kind: Service
metadata:
name: my-release-webapp
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
selector:
app.kubernetes.io/instance: my-releaseThe comment line disappears, but it leaves a blank line at the top. Whitespace control, covered in a later chapter, removes such lines.
Make part of the output conditional
An if action outputs its block only when the condition is true. An empty string, 0, false and a missing key all count as false.
spec:
type: {{ .Values.service.type }}
{{- if eq .Values.service.type "NodePort" }}
ports:
- port: {{ .Values.service.port }}
nodePort: 30080
{{- else }}
ports:
- port: {{ .Values.service.port }}
{{- end }}The function eq compares two values. Function calls in Go templates put the function name first, then its arguments, with no parentheses.
Common errors
- A missing key such as
.Values.servce.port(a typo) does not fail by default. Helm renders an empty value, or fails withnil pointer evaluating interface {}.portwhen the parent key is missing. - An unclosed action produces a
unexpected EOForunclosed actionerror. Everyif,withandrangeneeds its ownend. - A number inserted into a string field can break YAML. Quote values that must stay strings.
Exercise
In templates/service.yaml, what does {{ .Values.service.port }} output if values.yaml sets service.port to 8080, and what command shows the result without installing anything?
Show answer
It outputs 8080. Run helm template my-release ./webapp to render the manifest locally without a cluster.