
Helm is a powerful package manager for Kubernetes, and templating is at its core. This cheat sheet provides a quick reference for Helm templating, helping you master Go templates, conditional logic, loops, and helper functions.
📌 Basic Helm Template Syntax
Helm templates use Go templating with sprig functions to dynamically generate Kubernetes manifests.
Basic Syntax
{{ .Values.key }} # Access values from values.yaml
{{ .Release.Name }} # Get the release name
{{ .Chart.Name }} # Get the chart name
{{ .Release.Namespace }} # Get the namespace
Conditions & Loops
{{ if .Values.enabled }}
# YAML if enabled
{{ end }}
{{ range .Values.list }}
- {{ . }}
{{ end }}
{{ with .Values.config }}
# Use . inside this scope
{{ end }}
📌 Default & Ternary Operators
{{ .Values.image | default "nginx:latest" }} # Default value
{{ .Values.env | default "production" }} # Default env
Ternary alternative:
{{ if .Values.debug }} debug: true {{ else }} debug: false {{ end }}
📌 String Manipulation
{{ .Values.name | upper }} # Uppercase
{{ .Values.name | lower }} # Lowercase
{{ .Values.name | quote }} # Wrap in quotes
{{ printf "%s-%s" .Values.variable1 .Values.variable2 }} # concat or custom test formatting
📌 List & Map Functions
{{ .Values.list | join "," }} # Join list items
{{ .Values.dict | keys }} # Get dict keys
📌 File & Secret Encoding
{{ .Files.Get "config.yaml" }} # Load file content
{{ .Files.Get "config.yaml" | b64enc }} # Encode in base64
📌 Template Functions
Define & Include
{{ define "myTemplate" }}
Hello {{ .Values.name }}
{{ end }}
{{ include "myTemplate" . }}
Template Helpers (_helpers.tpl
)
{{- define "app.fullname" -}}
{{ .Release.Name }}-{{ .Chart.Name }}
{{- end -}}
name: {{ include "app.fullname" . }}
📌 Conditionally Creating Resources
{{ if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
{{ end }}
📌 Loops Example
Looping over a list:
{{ range .Values.hosts }}
- host: {{ . }}
{{ end }}
Looping over a dictionary:
{{ range $key, $value := .Values.configs }}
{{ $key }}: {{ $value }}
{{ end }}
📌 Custom Variables in Templates
Define & Use Custom Variables in Templates
{{- $appName := "my-app" }} # Define a variable
metadata:
name: {{ $appName }} # Use the variable
📌 Example values.yaml
name: my-app
enabled: true
list:
- item1
- item2
image: "my-image:latest"
configs:
key1: value1
key2: value2
Conclusion
Mastering Helm templating is essential for deploying scalable and configurable Kubernetes applications. This cheat sheet provides a quick reference for common Helm templating patterns and functions. Happy Helm charting! 🚀
Do you have any favorite Helm tricks? Share them in the comments below! 🔥