Helm in Depth — From Charts to a Platform Library›05 · CRDs and hooks

Lesson 05 of 10 · Charts for a platform

CRDs and hooks

Two parts of Helm that cause real outages: CRDs, which Helm deliberately never upgrades or deletes from crds/, and hooks, which run outside the normal release. Learn the safe patterns for both.

Advanced
Key wordsCRDscrds/ directoryCRD upgradescustom resourcesHelm hookspre-installpre-upgradepost-installhook-weighthook-delete-policyhelm testdatabase migrationsArgo CD sync hooks

CRDs: Helm's deliberate gap

CRDs are cluster-wide and deleting one deletes every object of that type. So Helm treats the crds/ directory specially:

  • installed on the first helm install, before other resources;
  • never upgraded on helm upgrade;
  • never deleted on helm uninstall.

The school's rules for what a "club" is (the CRD) are kept in the head office safe. Changing one club's timetable never rewrites the rules, and closing one club never burns the rule book, because every other club depends on it.

That protects data, but leaves you to manage CRD upgrades. Patterns that work:

  1. A separate CRDs chart or Git folder, upgraded first (kubectl apply --server-side for large CRDs), then the operator chart with --skip-crds.
  2. The operator manages its own CRDs (some do, at start-up).
  3. GitOps ordering: CRDs in an earlier sync wave than the operator and the custom resources.

Never put CRDs in templates/ without thinking

They will be upgraded, which is what you wanted, but helm uninstall (or a pruning GitOps tool) will delete them and every custom resource with them. If you must, add helm.sh/resource-policy: keep to the CRDs.

Order of upgrades for an operator that needs a new CRD field: CRDs first, then the operator, then the custom resources that use the new field.

Hooks: running things at lifecycle points

A hook is a normal resource (usually a Job) annotated to run at a point in the release lifecycle:

Hook Runs
pre-install / post-install before / after the first install's resources
pre-upgrade / post-upgrade around each upgrade
pre-delete / post-delete around uninstall
pre-rollback / post-rollback around rollback
test only when you run helm test
apiVersion: batch/v1
kind: Job
metadata:
  name: orders-smoke-test
  annotations:
    helm.sh/hook: test
    helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded
spec:
  backoffLimit: 0
  template:
    spec:
      restartPolicy: Never
      containers:
      - name: curl
        image: curlimages/curl:8.10.1
        args: [ "-fsS", "http://orders/healthz" ]

Hook traps

  • Hook resources are not part of the release: they aren't upgraded or removed with it unless a delete policy says so.
  • A failing hook fails the upgrade; a slow one hits the timeout.
  • Migrations in pre-upgrade hooks don't roll back with the release. Prefer expand/contract migrations (add columns, deploy code that handles both, remove old columns later) run by the app or a separate pipeline step.
  • Argo CD doesn't run Helm itself; it maps Helm hook annotations to its own sync hooks, and some (such as pre-rollback) have no equivalent. Check behaviour when you deploy charts through GitOps.

Try it

Add the test Job above to a chart, install it, and run helm test. Make the URL wrong and run it again to see a failing test. Then create a tiny CRD in templates/ with a custom resource, uninstall the release, and confirm both are gone. Repeat with the CRD in crds/ and see it survive.

Recap

  • crds/ is installed once and never upgraded or deleted; manage CRD upgrades separately and in the right order.
  • CRDs in templates/ are deleted on uninstall, taking every custom resource with them.
  • Hooks run Jobs at lifecycle points; keep them small, idempotent and cleaned up; avoid irreversible migrations in hooks.

This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.