Sub topic 4
Writing templates with built-in objects
This chapter assumes you can read YAML and know the basic Kubernetes objects. It also assumes you have a chart like the webapp chart from the earlier chapters, with its Chart.yaml and values.yaml.
How a template becomes a manifest
Files in a chart's templates/ directory are Go templates. Helm replaces each action, which is text inside {{ }}, with a computed value. The result must be valid YAML. Helm then sends that YAML to Kubernetes.
Helm gives every template a set of built-in objects. You reach them through the dot (.), which is the root scope.
The main built-in objects
| Object | What it holds | Example |
|---|---|---|
| `.Values` | Merged values from `values.yaml`, `-f` files, and `--set` | `.Values.service.port` |
| `.Release` | Facts about this release | `.Release.Name`, `.Release.Namespace`, `.Release.Revision`, `.Release.IsInstall`, `.Release.IsUpgrade` |
| `.Chart` | Fields from `Chart.yaml`, with the first letter capitalized | `.Chart.Name`, `.Chart.Version`, `.Chart.AppVersion` |
| `.Files` | Non-template files inside the chart | `.Files.Get "config.ini"` |
| `.Capabilities` | Cluster details such as the Kubernetes version | `.Capabilities.KubeVersion.Version` |
| `.Template` | The template being rendered | `.Template.Name` |
.Values is the object you will use most. Whatever a user sets with -f or --set appears there.
A complete example
Create templates/service.yaml in the webapp chart. It uses the service block from values.yaml.
apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name }}-{{ .Chart.Name }}
namespace: {{ .Release.Namespace }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: 80
selector:
app.kubernetes.io/name: {{ .Chart.Name }}
app.kubernetes.io/instance: {{ .Release.Name }}Render it locally without touching a cluster:
helm template my-release ./webapp --set service.port=8080 -s templates/service.yamlThe output has the actions replaced:
---
# Source: webapp/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-release-webapp
namespace: default
spec:
type: ClusterIP
ports:
- port: 8080
targetPort: 80
selector:
app.kubernetes.io/name: webapp
app.kubernetes.io/instance: my-releaseThe --set flag overrode the default port of 80. The release name came from the first argument. helm template reports default as the namespace unless you pass -n.
Scope and whitespace
Inside blocks such as with and range, the dot changes to mean the current item. To reach the root again, use $, for example $.Release.Name. Later chapters cover those blocks.
A plain {{ }} leaves its surrounding spaces and newlines in the output. Add a hyphen to trim them:
{{-removes whitespace before the action, including the preceding newline.-}}removes whitespace after the action.
Use {{- for actions that sit on their own line, so they do not leave blank lines in the YAML.
Common errors
nil pointer evaluating interface {}.port: You referenced a key under a parent that does not exist, such as.Values.service.portwhenserviceis missing. Check the spelling and add the default tovalues.yaml.- A value that parses as the wrong type: An image tag of
1.27becomes a number. Quote strings you insert:{{ .Values.image.tag | quote }}. error converting YAML to JSON: The rendered text is not valid YAML, usually because of wrong indentation or stray blank lines. Runhelm templateand read the output.- Empty output for a field: A missing key renders as empty text, not as an error. Test with
helm templatebefore you install.
Exercise
A chart named shop is installed as helm install blue ./shop -n prod. What does {{ .Release.Name }}-{{ .Chart.Name }} render to, and which object would you use to get prod?
Show answer
It renders to blue-shop. The namespace prod comes from .Release.Namespace.
Exercise
Your template contains tag: {{ .Values.image.tag }} and the value is 1.20. Why might Kubernetes reject or misread the result, and how do you fix it?
Show answer
YAML reads the unquoted 1.20 as the number 1.2, so the trailing zero is lost and the field may have the wrong type. Quote it with {{ .Values.image.tag | quote }}, or write "{{ .Values.image.tag }}".