Helm Functions

Template Functions and Pipelines

๐Ÿงญ Helm Template Functions Cheat Sheet with Examples

This document provides a comprehensive list of Helm template functions used in Helm charts, with real-world examples using .Values, .Chart, and .Release.


๐Ÿ”ง Commonly Used Functions

๐Ÿ—๏ธ default โ€“ Provide a fallback value

replicaCount: {{ .Values.replicaCount | default 3 }}

If .Values.replicaCount is not set in values.yaml, it defaults to 3.


โ— required โ€“ Make a value mandatory

apiVersion: v1
kind: Secret
metadata:
  name: my-secret
data:
  password: {{ required "A password is required!" .Values.password | b64enc }}
apiVersion: v1
kind: Secret
metadata:
  name: my-secret
data:
{{- if .Values.password }}
  password: {{ .Values.password | b64enc | quote }}
{{- else }}
  password: ""
{{- end }}

Will throw an error at install/upgrade if password is not provided.


How to render yaml files from new charts

helm template my-nginx01 ./nginx-demo -n helm-test --debug

๐Ÿ”ค String Functions

upper, lower, title, trim, replace

metadata:
  name: {{ .Chart.Name | upper }}-{{ .Release.Name | lower }}
  annotations:
    summary: {{ .Values.summary | title | quote }}

Transforms strings:

  • helm โ†’ HELM
  • release-name โ†’ release-name
  • some text โ†’ Some Text
annotations:
  clean-name: {{ replace .Chart.Name "-" "_" }}

Replaces hyphens (-) with underscores (_) in the chart name.
Useful for making label-safe names.


๐Ÿ”ข Number Functions

add, sub, mul, div, mod

resources:
  limits:
    cpu: {{ mul .Values.cpuBase .Values.cpuFactor }}
replicaCount: {{ add 2 3 }}  # Output: 5

Perform arithmetic using Helm values.


๐Ÿ“ƒ List Functions

list, join, first, last, uniq, range

โœ… Example: Looping with range and list

env:
{{- range $env := list "STAGING" "PROD" "DEV" }}
  - name: ENV
    value: {{ $env }}
{{- end }}

Loops over the list and renders each value as an environment variable.

Rendered output:

env:
  - name: ENV
    value: STAGING
  - name: ENV
    value: PROD
  - name: ENV
    value: DEV

โœ… Example: join โ€“ Convert a list to comma-separated string

labels:
  environments: {{ join "," (list "dev" "staging" "prod") }}

Converts list to string: dev,staging,prod.


โœ… Example: uniq โ€“ Remove duplicates from list

uniq-values: {{ uniq (list 1 2 2 3 1) }}  # Output: [1 2 3]

Removes duplicate values.
Use join if you need to convert it to string for use in annotations/labels.


๐Ÿ—‚๏ธ Dictionary/Object Functions

dict, hasKey, pluck, keys

{{- $config := dict "env" "prod" "region" "us-west" }}
region: {{ $config.region }}
{{- if hasKey .Values "replicaCount" }}
replicas: {{ .Values.replicaCount }}
{{- end }}

These functions help dynamically access and iterate through key-value pairs.


๐Ÿ” Flow Control

if, else, range, with

{{- if .Values.enabled }}
metadata:
  labels:
    enabled: "true"
{{- else }}
metadata:
  labels:
    enabled: "false"
{{- end }}
{{- range $key, $val := .Values.config }}
  {{ $key }}: {{ $val }}
{{- end }}

in Values.yaml

config:
  timeout: 30s
  retries: 5
  logLevel: debug
{{- with .Values.database }}
host: {{ .host }}
port: {{ .port }}
{{- end }}

in Values.yaml

database:
  host: db.example.com
  port: 5432

Conditional logic and scoping for clean, DRY templates.


๐Ÿ” Crypto & Encoding

b64enc, b64dec, sha256sum

data:
  password: {{ .Values.dbPassword | b64enc }}
hashed: {{ "sensitive" | sha256sum }}

Useful for secrets, hashing, and safe encoding.


๐Ÿ•’ Date & Time (Limited)

annotations:
  generatedAt: {{ now | date "2006-01-02T15:04:05Z07:00" }}

now returns current timestamp.
Format it using Go time layout (2006-01-02 is the reference format).


๐Ÿงช Example values.yaml

replicaCount: 2
enabled: true
summary: "simple chart"
cpuBase: 100
cpuFactor: 2
password: "secret123"
dbPassword: "admin"
envList:
  - name: ENV
    value: PROD
config:
  LOG_LEVEL: debug
  TIMEOUT: 30
database:
  host: db.example.com
  port: 5432
services:
  service1:
    port: 80
  service2:
    port: 443