Working with Template Functions
1. Accessing Values ({{ .Values.key }})
| Expression | Result |
|---|---|
{{ .Values.replicaCount }} | Top-level scalar |
{{ .Values.image.repository }} | Nested field |
{{ index .Values "weird-key" }} | Key with dashes/dots |
{{ .Values.missing | default "x" }} | Safe access with fallback |
2. Using Pipeline Operations
Example: Chained transformations
name: {{ .Values.appName | lower | trunc 63 | trimSuffix "-" | quote }}
3. Quoting Strings
| Function | Output |
|---|---|
quote | Wraps in double quotes |
squote | Wraps in single quotes |
printf "%q" . | Go-style quoted string |
4. Converting to YAML
Example: Render arbitrary structure
nodeSelector:
{{- toYaml .Values.nodeSelector | nindent 2 }}
tolerations:
{{- toYaml .Values.tolerations | nindent 2 }}
5. Converting to JSON
| Function | Use |
|---|---|
toJson | Compact JSON |
toPrettyJson | Indented JSON |
fromJson | Parse JSON string into map |
6. Using Default Values
Example: Fallback chain
tag: {{ .Values.image.tag | default .Chart.AppVersion | quote }}
host: {{ .Values.host | default "localhost" }}
port: {{ .Values.port | default 8080 }}
7. String Manipulation
| Function | Effect |
|---|---|
upper / lower / title | Case conversion |
trim / trimAll / trimPrefix / trimSuffix | Strip whitespace/chars |
trunc N | Limit to N characters |
replace "a" "b" . | String replace |
contains "sub" . | Boolean substring check |
split "/" . | Returns dict _0, _1... |
splitList "/" . | Returns true list |
printf "%s-%d" .Name 1 | Formatted string |
8. Type Conversion
| Function | Converts to |
|---|---|
toString | String |
int / int64 / atoi | Integer |
float64 | Float |
toJson / fromJson | JSON |
toYaml / fromYaml | YAML |
typeOf . | Reflect type name |
kindOf . | Underlying kind (string, map, slice...) |
9. Encoding Functions
| Function | Use |
|---|---|
b64enc / b64dec | Base64 |
b32enc / b32dec | Base32 |
sha1sum / sha256sum | Hashing (for checksum annotations) |
adler32sum | Adler-32 checksum |
10. Date Functions
| Function | Result |
|---|---|
now | Current time.Time |
date "2006-01-02" now | Format using Go reference time |
dateInZone "2006-01-02T15:04:05Z" now "UTC" | Zone-aware format |
htmlDate / htmlDateInZone | HTML5 date format |
Warning:
now changes on every render — it forces rolling updates if used in annotations. Use .Release.Time if you want stability across re-renders.11. UUID Generation
Warning:
uuidv4 changes per render — triggers unnecessary pod restarts. Prefer hashing values for stability.12. Using fail Function
Example: Hard validation
{{- if not .Values.image.repository }}
{{- fail "image.repository is required" }}
{{- end }}
{{- if and .Values.persistence.enabled (not .Values.persistence.size) }}
{{- fail "persistence.size required when persistence.enabled=true" }}
{{- end }}
| Function | Behavior |
|---|---|
fail "msg" | Aborts render with error message |
required "msg" .Values.x | Aborts if value is nil/empty |