Sprig Functions
A comprehensive guide to Sprig functions in Helm covering string manipulation, math operations, date formatting, default, required, and advanced template functions.
String Functions
Math Functions
Date Functions
Default & Required
What is Sprig?
Sprig is a library of over 60 template functions for Go templates. Helm includes all Sprig functions, providing powerful capabilities for string manipulation, math operations, date formatting, and more.
Sprig functions are available in all Helm templates and can be chained using pipelines.
Key Concept: Sprig functions are the "Swiss Army knife" of Helm templating. They provide essential utilities that make templates more expressive and powerful.
String Manipulation Functions
upper / lower
Convert strings to uppercase/lowercase.
{{ "hello" | upper }} → HELLO
{{ "WORLD" | lower }} → world
trim
Remove leading and trailing whitespace.
{{ " hello " | trim }} → hello
trunc
Truncate a string to a specified length.
{{ "hello world" | trunc 5 }} → hello
replace
Replace all occurrences of a substring.
{{ "hello world" | replace "world" "there" }} → hello there
contains
Check if a string contains a substring.
{{ contains "hello" "hel" }} → true
hasPrefix / hasSuffix
Check if a string starts or ends with a prefix/suffix.
{{ hasPrefix "hello" "he" }} → true
trimPrefix / trimSuffix
Remove a prefix/suffix from a string.
{{ "hello" | trimSuffix "o" }} → hell
quote / squote
Wrap a string in double/single quotes.
{{ "hello" | quote }} → "hello"
# String function examples
# upper/lower
{{ "hello" | upper }} # HELLO
{{ "WORLD" | lower }} # world
# trim
{{ " hello " | trim }} # hello
# trunc
{{ "hello world" | trunc 5 }} # hello
# replace
{{ "hello world" | replace "world" "there" }} # hello there
# contains
{{ contains "hello" "hel" }} # true
# hasPrefix/hasSuffix
{{ hasPrefix "hello" "he" }} # true
{{ hasSuffix "hello" "lo" }} # true
# trimPrefix/trimSuffix
{{ "hello" | trimSuffix "o" }} # hell
{{ "hello" | trimPrefix "he" }} # llo
# quote/squote
{{ "hello" | quote }} # "hello"
{{ "hello" | squote }} # 'hello'
# split (split into list)
{{ split "a,b,c" "," }} # [a b c]
# join (join list with separator)
{{ list "a" "b" "c" | join "," }} # a,b,c
# regexReplaceAll
{{ "Hello World" | regexReplaceAll "World" "There" }} # Hello There
# toString
{{ 123 | toString }} # "123"
Math Functions
add / sub / mul / div
Basic arithmetic operations.
{{ add 5 3 }} → 8
{{ sub 10 4 }} → 6
floor / ceil
Round down/up to nearest integer.
{{ floor 5.7 }} → 5
{{ ceil 5.2 }} → 6
abs / max / min
Absolute value, maximum, minimum.
{{ abs -5 }} → 5
{{ max 1 5 3 }} → 5
mod
Modulo operation.
{{ mod 10 3 }} → 1
pow / sqrt
Power and square root.
{{ pow 2 3 }} → 8
{{ sqrt 16 }} → 4
# Math function examples
# add/sub/mul/div
{{ add 5 3 }} # 8
{{ sub 10 4 }} # 6
{{ mul 3 4 }} # 12
{{ div 10 2 }} # 5
# floor/ceil
{{ floor 5.7 }} # 5
{{ ceil 5.2 }} # 6
{{ round 5.55 }} # 6
# abs/max/min
{{ abs -5 }} # 5
{{ max 1 5 3 }} # 5
{{ min 1 5 3 }} # 1
# mod
{{ mod 10 3 }} # 1
# pow/sqrt
{{ pow 2 3 }} # 8
{{ sqrt 16 }} # 4
# inc/dec (increment/decrement)
{{ inc 5 }} # 6
{{ dec 5 }} # 4
# add1/sub1 (alternatives)
{{ add1 5 }} # 6
{{ sub1 5 }} # 4
Date and Time Functions
now
Get the current time.
{{ now }} → 2024-01-15 10:30:00 ...
date
Format a date using a layout string.
{{ now | date "2006-01-02" }} → 2024-01-15
dateModify
Add or subtract duration from a date.
{{ now | dateModify "+24h" }} → tomorrow
toDate
Parse a string into a date.
{{ "2024-01-15" | toDate "2006-01-02" }}
# Date function examples
# now (current time)
{{ now }}
# date (format)
{{ now | date "2006-01-02" }} # 2024-01-15
{{ now | date "2006-01-02 15:04:05" }} # 2024-01-15 10:30:00
{{ now | date "Mon Jan 2 15:04:05 MST 2006" }}
# dateModify (add/subtract duration)
{{ now | dateModify "+24h" }} # Tomorrow
{{ now | dateModify "-7d" }} # 7 days ago
{{ now | dateModify "+1h" }} # 1 hour from now
# toDate (parse string to date)
{{ "2024-01-15" | toDate "2006-01-02" }}
{{ "2024-01-15T10:30:00Z" | toDate "2006-01-02T15:04:05Z" }}
# date comparison
{{ $date := "2024-01-15" | toDate "2006-01-02" }}
{{ $today := now | date "2006-01-02" | toDate "2006-01-02" }}
{{ if $date.Before $today }}before{{ else }}after{{ end }}
# Unix timestamp
{{ now | unixEpoch }} # 1705330000
Date Formatting Note: Go uses a specific reference time for formatting:
Mon Jan 2 15:04:05 MST 2006. This is not a placeholder—it's the actual format used by Go's time package.
Default and Required Functions
default
Provide a fallback value if the input is empty.
{{ .Values.key | default "fallback" }}
required
Require a value; fail with an error message if missing.
{{ required "A valid key is required" .Values.key }}
empty
Check if a value is empty (nil, empty string, empty list, etc.).
{{ if empty .Values.optional }}empty{{ end }}
hasKey
Check if a map has a specific key.
{{ hasKey .Values "key" }}
# default - fallback values
{{ .Values.image.tag | default "latest" }}
{{ .Values.replicaCount | default 1 }}
{{ .Values.environment | default "dev" }}
# default with condition
{{ .Values.ingress.enabled | default false }}
# required - mandatory values
{{ required "A valid database URL is required" .Values.database.url }}
{{ required "A valid image repository is required" .Values.image.repository }}
# empty - check for empty values
{{ if empty .Values.optionalValue }}
# Value is empty
{{ end }}
# hasKey - check if key exists
{{ if hasKey .Values "database" }}
# Database key exists
{{ end }}
# coalesce - first non-empty value
{{ coalesce .Values.region .Values.zone .Values.area "default" }}
# ternary - conditional operator
{{ ternary "enabled" "disabled" .Values.feature.enabled }}
Best Practice: Use
default for optional values and required for mandatory values. This makes charts more robust and provides clear error messages for missing configuration.
Advanced Sprig Functions
# List/Array functions
{{ list "a" "b" "c" }} # Create list
{{ list 1 2 3 | first }} # First item (1)
{{ list 1 2 3 | last }} # Last item (3)
{{ list 1 2 3 | rest }} # All except first ([2 3])
{{ list 1 2 3 | append 4 }} # Add item ([1 2 3 4])
{{ list 1 2 3 | prepend 0 }} # Add at beginning ([0 1 2 3])
{{ list 1 2 3 | reverse }} # Reverse list ([3 2 1])
{{ len (list 1 2 3) }} # Length (3)
# Dictionary functions
{{ dict "key" "value" }} # Create dict
{{ get .Values "key" }} # Get value from dict
{{ set .Values "key" "value" }} # Set value in dict
{{ unset .Values "key" }} # Remove key from dict
{{ hasKey .Values "key" }} # Check if key exists
# Type conversion
{{ 123 | toString }} # "123"
{{ "123" | toInt }} # 123
{{ "123" | toFloat }} # 123.0
{{ "123" | toInt64 }} # 123
# UUID generation
{{ uuidv4 }} # Generate UUID
# Random functions
{{ randNumeric }} # Random number
{{ randNumeric 5 }} # 5-digit random number
{{ randAlphaNum 10 }} # 10-character alphanumeric string
# Environment variables
{{ env "HOME" }} # Get environment variable
# Base64 encoding
{{ "hello" | b64enc }} # Base64 encode
{{ "aGVsbG8=" | b64dec }} # Base64 decode
Advanced Tip: Sprig also provides functions for working with cryptographic hashes (sha256, sha1), JSON/YAML operations, and network-related utilities. Check the Sprig documentation for the complete list.
Frequently Asked Questions
What is Sprig and why is it important in Helm?
Sprig is a library of template functions for Go templates. Helm includes all Sprig functions, providing powerful utilities for string manipulation, math, date formatting, and more. It makes Helm templates more expressive and powerful.
What is the difference between default and required?
default provides a fallback value if the input is empty. required fails with an error message if the value is missing. Use default for optional values and required for mandatory values. How do I format dates in Helm?
Use
date with a layout string: {{ now | date "2006-01-02" }}. Go uses the reference time Mon Jan 2 15:04:05 MST 2006 for formatting. Can I do math in Helm templates?
Yes! Sprig provides math functions like
add, sub, mul, div, floor, ceil, max, min, and more. What string manipulation functions are available?
Sprig provides
upper, lower, trim, trunc, replace, contains, hasPrefix, hasSuffix, trimPrefix, trimSuffix, quote, and more. How do I check if a value is empty in Helm?
Use the
empty function: {{ if empty .Values.optional }}empty{{ end }}. This works for strings, lists, maps, and other types. Can I generate UUIDs in Helm?
Yes! Use
{{ uuidv4 }} to generate a UUID v4 string. This is useful for generating unique identifiers. How do I work with lists in Helm?
Use
list to create lists, and functions like first, last, rest, append, prepend, reverse, and len to manipulate them.
Related Topics
Sprig functions are essential tools for creating powerful Helm templates. Master these functions to write cleaner, more maintainable, and more expressive Helm charts.