Sub topic 12
Failing early with required and rendering strings with tpl
This chapter assumes you can read a Helm template, pass values with -f and --set, and use pipelines such as default and nindent. It covers two functions that make charts safer and more flexible: required and tpl.
Why fail at render time
A missing value often produces valid-looking YAML that only breaks in the cluster. For example, an empty image.repository renders image: ":1.27.0". Kubernetes rejects it later with a confusing error. It is better to stop during helm template, helm lint or helm install, with a message that names the missing value.
required
required takes an error message and a value. If the value is empty, rendering stops and Helm prints your message. If the value is set, it passes through unchanged, so you can keep piping it.
image: {{ required "image.repository is required" .Values.image.repository | quote }}If you render without the value, you see an error like this:
Error: execution error at (webapp/templates/deployment.yaml:20:16): image.repository is required"Empty" means the zero value: "", 0, false, nil, or an empty list or map. A replicaCount of 0 therefore fails required, even though it is a legitimate setting. Use required for values that have no sensible default, such as a repository, a hostname or a secret name.
Where to put the check
Place required at the point of use, so the message appears next to the failing template. If several templates use the same value, put the check in a helper in _helpers.tpl and call it with include. The check then lives in one place.
tpl
Normally, a value read from values.yaml is plain text. If a user writes {{ .Release.Name }} in a value, Helm prints it literally. The tpl function takes a string and a context, and renders the string as a template. Use it when you want users to write values that refer to the release or to other values.
{{- if .Values.extraConfig }}
config: |
{{- tpl .Values.extraConfig . | nindent 2 }}
{{- end }}With this values file:
extraConfig: |
server_name {{ .Release.Name }}.example.com;
worker_processes {{ .Values.replicaCount }};and a release named my-release with replicaCount: 2, the output is:
config: |
server_name my-release.example.com;
worker_processes 2;The second argument, ., is the context the string sees. Passing the root context gives the string access to .Values, .Release and .Chart. Inside a range or with block, . has changed, so pass $ to keep the root context.
Using both together
You can combine the functions. Here the chart requires a value and also renders it:
host: {{ required "ingress.host is required" .Values.ingress.host | tpl $ | quote }}This lets a user set ingress.host: "{{ .Release.Name }}.example.com" and still get an error if they set nothing.
Checking your work
helm template my-release ./webapp --set image.repository=""
helm template my-release ./webapp --set image.repository=nginxThe first command should fail with your message. The second should render normally.
Exercise
A chart uses {{ required "replicaCount is required" .Values.replicaCount }}. A user sets replicaCount: 0 to pause the app, and rendering fails. Why, and what would you do instead?
Show answer
required treats 0 as empty, so it raises the error. Remove required for this value and give it a default in values.yaml (or use default). If you must check that it is set, use hasKey .Values "replicaCount", which tests for presence, not truthiness.
Exercise
A value message: "Hello {{ .Release.Name }}" renders literally as Hello {{ .Release.Name }} in a ConfigMap. What change to the template fixes it?
Show answer
Pass the value through tpl with the root context: {{ tpl .Values.message . }}. Inside a range or with block, use $ instead of ..