Template Functions

Helm ships 60+ functions from the Sprig library for strings, math, lists, dates, and encoding.

Syntax{{ funcName arg1 arg2 }}

Helm includes the Sprig function library plus a handful of Helm-specific functions. These do the real work inside templates.

Common categories

  • Stringsupper, lower, trim, trunc, replace, printf.
  • Defaults & logicdefault, empty, ternary, coalesce.
  • Lists & mapslist, first, dict, keys, hasKey.
  • Encodingb64enc, b64dec, toYaml, toJson.
  • Helm-specificinclude, required, lookup, tpl.

Calling functions

Functions take space-separated arguments: {{ trunc 63 .Values.name }}. Use parentheses to group: {{ upper (trim .Values.name) }}.

Example

Example · yaml
data:
  # Base64-encode a secret value
  password: {{ .Values.password | b64enc }}
  # Truncate to Kubernetes' 63-char name limit
  name: {{ .Values.name | trunc 63 | trimSuffix "-" }}
  # Render a whole map as YAML
  config: |
    {{- toYaml .Values.config | nindent 4 }}

When to use it

  • A chart author uses the Sprig trunc and trimSuffix functions to shorten a release name to 63 characters, satisfying the Kubernetes DNS label length limit.
  • A developer uses b64enc to base64-encode a password from values.yaml directly in a Secret template without requiring operators to pre-encode values themselves.
  • A template uses the Sprig list and join functions to build a comma-separated string from an array of allowed CORS origins stored in values.yaml.

More examples

Truncate name to DNS limit

Combines release and chart names then trims the result to 63 characters to stay within Kubernetes DNS label limits.

Example · yaml
{{- define "myapp.fullname" -}}
{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 63 | trimSuffix "-" }}
{{- end }}

Base64-encode a secret value

Uses the b64enc Sprig function to base64-encode sensitive values inline, so the Secret template accepts plaintext values input.

Example · yaml
apiVersion: v1
kind: Secret
metadata:
  name: {{ .Release.Name }}-creds
type: Opaque
data:
  password: {{ .Values.db.password | b64enc | quote }}
  apiKey: {{ .Values.apiKey | b64enc | quote }}

Date and random functions

Uses now and date for a deploy timestamp annotation, and randAlphaNum to generate a short random identifier.

Example · yaml
metadata:
  annotations:
    chart-deployed: {{ now | date "2006-01-02" | quote }}
    unique-id: {{ randAlphaNum 8 | lower | quote }}

Discussion

  • Be the first to comment on this lesson.