Helm Sprig toString Function: Type Conversion
Convert values to strings in Helm templates using the Sprig toString function. Handle integers, booleans, lists, and nil values safely in Kubernetes manifests.
π‘ Quick Answer: The Sprig
toStringfunction converts any value to its string representation:{{ .Values.port | toString }}converts the integer8080to the string"8080". Essential when Helm values are numbers but YAML fields require strings β like container ports in annotations or ConfigMap data.
The Problem
Helm values in values.yaml can be integers, booleans, or other types, but many Kubernetes fields (annotations, ConfigMap data, environment variables) require strings. Without explicit conversion, Helm renders numbers as 8080 instead of "8080", causing YAML parsing errors or unexpected behavior.
The Solution
Basic Syntax
# Convert integer to string
{{ .Values.port | toString }}
# Input: 8080 β Output: "8080"
# Convert boolean to string
{{ .Values.debug | toString }}
# Input: true β Output: "true"
# Convert float to string
{{ .Values.ratio | toString }}
# Input: 0.75 β Output: "0.75"Common Use Cases
ConfigMap Data (Must Be Strings)
# values.yaml
config:
port: 8080
workers: 4
debug: true
# templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-config
data:
PORT: {{ .Values.config.port | toString | quote }}
WORKERS: {{ .Values.config.workers | toString | quote }}
DEBUG: {{ .Values.config.debug | toString | quote }}Annotations (Always Strings)
metadata:
annotations:
prometheus.io/port: {{ .Values.metrics.port | toString | quote }}
app.kubernetes.io/version: {{ .Chart.AppVersion | toString | quote }}
replicas: {{ .Values.replicaCount | toString | quote }}Environment Variables
env:
- name: SERVER_PORT
value: {{ .Values.port | toString | quote }}
- name: MAX_CONNECTIONS
value: {{ .Values.maxConn | toString | quote }}
- name: ENABLE_TLS
value: {{ .Values.tls.enabled | toString | quote }}toString vs Other Conversion Functions
| Function | Input | Output | Use Case |
|---|---|---|---|
toString | 8080 | "8080" | Any type β string |
int | "8080" | 8080 | String β integer |
float64 | "3.14" | 3.14 | String β float |
toJson | {a: 1} | {"a":1} | Object β JSON string |
quote | hello | "hello" | Wrap in YAML quotes |
Handling Nil Values
# If .Values.optional is nil, toString returns ""
{{ .Values.optional | toString }}
# Output: ""
# Use default to provide fallback BEFORE toString
{{ .Values.optional | default 3000 | toString }}
# Output: "3000"Real-World Example: Ingress Annotations
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/proxy-body-size: {{ .Values.ingress.maxBodySize | toString | quote }}
nginx.ingress.kubernetes.io/proxy-read-timeout: {{ .Values.ingress.readTimeout | toString | quote }}
nginx.ingress.kubernetes.io/limit-rps: {{ .Values.ingress.rateLimit | toString | quote }}# values.yaml
ingress:
maxBodySize: 50 # Will become "50"
readTimeout: 120 # Will become "120"
rateLimit: 10 # Will become "10"Common Issues
βcannot convert nil to stringβ
# Problem: value doesn't exist
{{ .Values.missing | toString }}
# Fix: always provide a default
{{ .Values.missing | default "" | toString }}Double Quoting
# Wrong: toString + quote on a value that's already a string
# Produces: '"already-a-string"' (double quoted)
# Right: use quote OR toString, check the type
{{ if kindIs "string" .Values.port }}
{{ .Values.port | quote }}
{{ else }}
{{ .Values.port | toString | quote }}
{{ end }}List to String
# toString on a list produces Go syntax: [a b c]
{{ .Values.tags | toString }}
# Output: "[tag1 tag2 tag3]"
# Better: use join for lists
{{ .Values.tags | join "," }}
# Output: "tag1,tag2,tag3"Best Practices
- Always
toStringbeforequotefor numeric values in annotations - Use
defaultbeforetoStringβ handle nil values gracefully - Prefer
joinfor lists βtoStringon lists produces ugly Go syntax - Check types with
kindIsβ avoid double conversion - Use
toJsonfor complex objects β maps and nested structures
Key Takeaways
toStringconverts integers, booleans, and floats to string representation- Essential for ConfigMap data, annotations, and environment variables
- Always pair with
quotefor proper YAML output:| toString | quote - Use
defaultbeforetoStringto handle nil values - For lists, prefer
joinovertoString

Recommended
Kubernetes Recipes β The Complete Book100+ production-ready patterns with detailed explanations, best practices, and copy-paste YAML. Everything in one place.
Get the Book βLearn by Doing
CopyPasteLearn β Hands-on Cloud & DevOps CoursesMaster Kubernetes, Ansible, Terraform, and MLOps with interactive, copy-paste-run lessons. Start free.
Browse Courses βπ Deepen Your Skills β Hands-on Courses
Courses by CopyPasteLearn.com β Learn IT by Doing
