TExcelTAD
Home › Kubernetes Packaging: Helm & Kustomize › Field Notes: Charts & Overlays in Production

Field Notes: Charts & Overlays in Production

Practical advice collected from packaging that went wrong, grouped by the mistake it prevents. Each note is short; the linked lessons have the details.

A patch that matches nothing does nothing

Kustomize patches with a target selector apply to whatever matches, and zero matches is not an error. Rename an object in the base and the overlay's patch silently stops applying; production quietly loses its resource limits. A strategic merge patch without a target fails loudly instead:

bash · try it yourself
mkdir -p base prod
printf 'apiVersion: v1\nkind: ConfigMap\nmetadata:\n  name: linkly-settings\ndata: {LOG_LEVEL: info}\n' > base/cm.yaml
printf 'resources: [cm.yaml]\n' > base/kustomization.yaml
cat > prod/kustomization.yaml <<'EOF'
resources: [../base]
patches:
  - target: {kind: ConfigMap, name: linkly-config}     # the base renamed it to linkly-settings
    patch: |-
      - op: replace
        path: /data/LOG_LEVEL
        value: warn
EOF
echo "--- target selector, no match:"
kustomize build prod; echo "exit code: $?"
cat > prod/kustomization.yaml <<'EOF'
resources: [../base]
patches:
  - patch: |-
      apiVersion: v1
      kind: ConfigMap
      metadata: {name: linkly-config}
      data: {LOG_LEVEL: warn}
EOF
echo "--- strategic merge patch, no match:"
kustomize build prod 2>&1 | cut -c1-90; echo "exit code: ${PIPESTATUS[0]}"
--- target selector, no match:
apiVersion: v1
data:
  LOG_LEVEL: info
kind: ConfigMap
metadata:
  name: linkly-settings
exit code: 0
--- strategic merge patch, no match:
Error: no resource matches strategic merge patch "ConfigMap.v1.[noGrp]/linkly-config.[noNs
exit code: 1

Defences: prefer SMPs (they identify their target by kind and name) for single objects; for targeted patches, assert the result in CI (yq on the built output: "production has LOG_LEVEL: warn"); and keep the rendered-manifest diff in every review, where a missing change is visible.

Never let a selector change

A Deployment's or StatefulSet's spec.selector is immutable. Anything that changes it breaks the next upgrade with field is immutable, and the only fix is deleting and recreating the workload (downtime, and for StatefulSets, care with volumes). Common causes: adding the chart version or app.kubernetes.io/version to selector labels; renaming the chart (app.kubernetes.io/name changes); a new fullnameOverride; labels with includeSelectors: true added later in Kustomize. Compare selectors between the old and new render in CI:

bash · try it yourself
mkdir -p linkly/templates
printf 'apiVersion: v2\nname: linkly\nversion: 1.0.0\nappVersion: "1.4.0"\n' > linkly/Chart.yaml
cat > linkly/templates/deployment.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}
spec:
  selector:
    matchLabels:
      app.kubernetes.io/instance: {{ .Release.Name }}
      app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}   # the bug
  template:
    metadata:
      labels:
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
    spec:
      containers: [{name: linkly, image: "ghcr.io/acme/linkly:{{ .Chart.AppVersion }}"}]
EOF
selector() { helm template web linkly "$@" | yq -o json -I0 'select(.kind == "Deployment") | .spec.selector'; }
old=$(selector)
echo "re-render, same chart: $([ "$old" = "$(selector)" ] && echo unchanged)"
sed -i 's/appVersion: "1.4.0"/appVersion: "1.4.1"/' linkly/Chart.yaml
new=$(selector)
if [ "$old" != "$new" ]; then echo "FAIL: selector changed"; echo "  old: $old"; echo "  new: $new"; fi
re-render, same chart: unchanged
FAIL: selector changed
  old: {"matchLabels":{"app.kubernetes.io/instance":"web","app.kubernetes.io/version":"1.4.0"}}
  new: {"matchLabels":{"app.kubernetes.io/instance":"web","app.kubernetes.io/version":"1.4.1"}}

CRDs: Helm installs them once and never again

Files in a chart's crds/ folder are applied on the first helm install only: Helm never upgrades or deletes them (deleting a CRD deletes every custom resource of that kind in the cluster). They don't appear in helm template unless you ask:

bash · try it yourself
mkdir -p op/templates op/crds
printf 'apiVersion: v2\nname: op\nversion: 0.1.0\n' > op/Chart.yaml
printf 'apiVersion: apiextensions.k8s.io/v1\nkind: CustomResourceDefinition\nmetadata:\n  name: shortlinks.acme.example.com\n' > op/crds/shortlinks.yaml
printf 'apiVersion: v1\nkind: ConfigMap\nmetadata:\n  name: op-config\n' > op/templates/cm.yaml
echo "default:        $(helm template d op | grep '^kind:' | paste -sd' ')"
echo "--include-crds: $(helm template d op --include-crds | grep '^kind:' | paste -sd' ')"
default:        kind: ConfigMap
--include-crds: kind: CustomResourceDefinition kind: ConfigMap

For CRDs that evolve, ship them separately (a dedicated CRD chart, or plain manifests applied by GitOps before the operator), and upgrade them deliberately. GitOps tools that render with --include-crds do apply CRD changes, which is convenient and risky in equal measure.

More notes, without sessions

  • Pin everything. Chart versions (--version 1.0.3, or a digest), image tags (or digests), remote Kustomize bases (?ref=), and the Helm/Kustomize binaries in CI. "Latest" anywhere makes a deploy depend on the day it ran.
  • One owner per field. If Helm manages an object, don't kubectl edit it; the next upgrade reverts the edit (Helm 3) or hits a server-side apply conflict (Helm 4). Let HPAs own replicas by omitting the field when autoscaling is on.
  • Names are identity. Renaming an object in a chart or overlay means delete and create. For a PVC that means a new, empty volume. Add helm.sh/resource-policy: keep to objects that must survive helm uninstall or a rename (PVCs, Secrets with generated passwords), and clean them up by hand.
  • helm uninstall leaves things behind: hook objects, CRDs, PVCs created from StatefulSet templates, and anything with resource-policy: keep. Know the list before you rely on uninstall to clean a namespace.
  • Don't hard-code namespaces in templates. Use {{ .Release.Namespace }} so -n works; in Kustomize, let the overlay's namespace: set it.
  • Keep values small. Expose what users really change; every values key is API you must support across chart majors. A chart that templates every field is just YAML with extra steps, and a Kustomize patch serves rare needs better.
  • Secrets never live in values files or generators in Git. Reference existing Secrets; let an operator (External Secrets, Sealed Secrets) or SOPS handle the values (Files, ConfigMaps & Secrets).
  • Review the rendered diff, not the template diff. A one-character template change can touch every object in every environment.

Test yourself

An overlay's JSON patch targets name: linkly-config, but the base renamed the ConfigMap. What does kustomize build do?

  1. Fails with an error
  2. Applies the patch to the closest name
  3. Builds successfully; the patch silently applies to nothing
  4. Creates a new ConfigMap

Why must app.kubernetes.io/version stay out of a Deployment's selector?

  1. Selectors can't contain dots
  2. The selector is immutable, so the next version would fail to upgrade
  3. Kubernetes forbids version labels
  4. It slows scheduling

What does Helm do with a chart's crds/ folder on helm upgrade?

  1. Upgrades the CRDs
  2. Deletes and recreates them
  3. Nothing: CRDs there are only installed on first install
  4. Fails