Sub topic 13
Practices for maintainable charts
This chapter assumes you can already write templates and named templates, run helm install, upgrade and rollback, and read a values.yaml. It collects habits that keep a chart easy to change after the first release.
Version the chart and the app separately
version in Chart.yaml describes the chart. appVersion describes the application it deploys. Bump version on every chart change, even a template fix, because a registry will not accept two different charts under the same version. Use semantic versioning: a patch for fixes, a minor for new optional values, a major for changes that break existing values files.
Leave image.tag empty in values.yaml and fall back to .Chart.AppVersion in the template. Upgrading the app then means changing one field.
Design values for the people who override them
Users override values with -f and --set, so the shape of values.yaml is your public interface.
- Keep nesting shallow.
--setpaths likea.b.c.d.eare easy to mistype. - Use maps instead of lists when users need to override one entry. Helm replaces a list as a whole, but it merges maps key by key.
- Give every key a safe default and a comment that explains it.
- Treat renaming a key as a breaking change.
# Number of pod replicas. Ignored when autoscaling.enabled is true.
replicaCount: 2
image:
# Image repository, without a tag.
repository: nginx
# Defaults to the chart's appVersion when empty.
tag: ""
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
memory: 256MiFail early on bad input
Use required for values that have no sensible default. The error then names the problem at render time instead of showing up later as a broken pod.
image: "{{ required "image.repository is required" .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"Pair this with values.schema.json for type and range checks. The schema catches replicaCount: "two", and required catches a missing key.
Keep templates predictable
- Define names and labels once in
_helpers.tpland call them withinclude. Do not copy them between manifests. - Build resource names from
.Release.Name, never from a fixed string. Two releases of the same chart in one namespace should not collide. - Quote string values with
quote, so"true"and"1.10"are not parsed as a boolean or a number. - Use
nindentwithincludeto get indentation right, and avoid hand-counted spaces. - Make optional resources conditional with
if, and expose a matchingenabledvalue.
Pin and review dependencies
Declare exact or tightly ranged versions in Chart.yaml, and commit Chart.lock. Run helm dependency build in CI so that it uses the lock file. Run helm dependency update only when you intend to upgrade a subchart. Add a condition to each subchart so users can supply their own database or cache instead.
Test the chart in CI
Run the same checks on every change. Each check catches a different kind of failure.
set -euo pipefail
helm dependency build ./webapp
helm lint ./webapp --strict
helm template my-release ./webapp -f ci/values-prod.yaml > /dev/null
helm package ./webapp --version "$CHART_VERSION"Keep one values file per scenario under ci/, such as minimal, production and autoscaling. Rendering each one exercises every if branch.
Operate releases safely
- Use
helm upgrade --installso the same command works for first and later deploys. - Add
--atomic(which implies--wait) so a failed upgrade rolls back on its own. - Set a
helm.sh/hook-delete-policyon hooks, or finished Jobs pile up and block the next run. - Run
helm diff(a plugin) in review to see what an upgrade would change. - Document breaking changes in a changelog and in
NOTES.txt.
Exercise
A teammate renames service.port to service.servicePort in values.yaml and bumps only the patch version. Name two problems and one fix.
Show answer
Existing values files that set service.port will be silently ignored, so services keep the default port. A rename is also a breaking change, so a patch bump misleads users. Fix: bump the major version (or keep both keys for a while, with the new one falling back to the old with default), and add a values.schema.json or required check so stale keys are noticed.
Exercise
Why does the CI script call helm dependency build instead of helm dependency update?
Show answer
build restores subcharts at the exact versions recorded in Chart.lock, so CI is reproducible. update re-resolves version ranges and rewrites the lock file, which can pull in an unreviewed subchart version.