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.
Want to master this scenario in a live sandbox? KodeKloud's Enterprise GitOps with ArgoCD & Kubernetes Rollouts covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: 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.
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 }}
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.
- 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.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Promoting application releases to production without an automated, canary-validated rollback mechanism when configuring Helm Templates range Pipelines that lack automated rollback mechanisms force engineering teams to scramble manual hotfixes during severe production regressions.
- Running concurrent deployment pipeline jobs without stage mutex locking on shared target environments when configuring Helm Templates range Concurrent pipeline runs on the same environment race to apply state changes, causing out-of-order commits and unpredictable deployment state.
- Omitting dependency lockfiles (package-lock.json, go.sum, Cargo.lock) from continuous integration builds when configuring Helm Templates range Omitting lockfiles allows upstream package updates to break builds unpredictably without any changes to application codebase.
- Failing to define dynamic cache invalidation keys for CI package managers and layer caches when configuring Helm Templates range Stale build caches mask underlying dependency incompatibilities or pull outdated libraries, while un-cached runs slow feedback loops.