Q: What is the range function in Helm, and what is the difference between values and parameters during deployment?
Mastering dynamic list and map iteration in Helm templates using the range function, managing variable scope shifts, and understanding value precedence across values.yaml, file cascades, and --set flags.
#Helm #Kubernetes #Templates #GitOps #CI/CD
🎙️ Candidate Opening & Architectural Context
"The range function in Helm is a Go template control structure that iterates over arrays, slices, or dictionaries. Inside a range block, the root context (.) shifts to the current item in the iteration, which requires using the dollar sign ($) to access top-level values."
Advertisement
🛠️ Production Runbook & Step-by-Step Resolution
1
Using the range Function in Helm Templates
Demonstrate iterating over an array of environment variables or ingress hosts with scope preservation.
env:
{{- range $key, $val := .Values.extraEnv }}
- name: {{ $key }}
value: {{ $val | quote }}
{{- end }}
# Accessing root scope inside range using $
{{- range .Values.ingress.hosts }}
- host: {{ .host }}
secretName: {{ $.Values.global.tlsSecretName }}
{{- end }}
2
Difference: Values vs. Parameters (Precedence Order)
Values are declared static defaults in values.yaml. Parameters are dynamic runtime overrides passed during CI/CD execution. Precedence order (lowest to highest): 1) Chart values.yaml, 2) Parent chart values, 3) -f values.env.yaml, 4) --set parameter overrides.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"range iterates over maps and lists while shifting context (.) to the current element. Access root values via $. Command-line --set parameters override all file-based values."
⚡ 60-Second Elevator Pitch Talking Points
- range iterates over collections in Helm; remember that (.) shifts to the element scope, requiring ($) for root context.
- values.yaml provides base defaults committed to Git.
- -f values.env.yaml supplies environment-specific overrides (dev, stage, prod).
- --set CLI parameters take absolute precedence for dynamic CI build hashes and image tags.
Advertisement