TExcelTAD

Hooks

Most of a chart is declarative: "these objects should exist". Some steps are actions at a point in time: run database migrations before the new version starts, warm a cache after an install, back up data before an uninstall. Hooks are ordinary templates with an annotation that tells Helm to create them at a specific point in the release lifecycle, and to wait for them.

Hook points

Annotation value Runs
pre-install / post-install after templates render, before any object is created / after all are created and ready
pre-upgrade / post-upgrade before / after the upgrade's objects are updated
pre-rollback / post-rollback around a rollback
pre-delete / post-delete around helm uninstall
test only when you run helm test (next lesson)

One object can serve several points: helm.sh/hook: pre-install,pre-upgrade.

linkly's migration Job

linkly must migrate its PostgreSQL schema before a new version serves traffic. A Job annotated as a pre-install,pre-upgrade hook does exactly that: Helm creates it, waits until it completes, and only then applies the new Deployment. If the Job fails, the release fails and the old version keeps running.

bash · try it yourself
mkdir -p linkly/templates && cd linkly
printf 'apiVersion: v2\nname: linkly\nversion: 0.7.0\nappVersion: "1.4.0"\n' > Chart.yaml
printf 'image: ghcr.io/acme/linkly\n' > values.yaml
cat > templates/migrate-job.yaml <<'EOF'
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ .Release.Name }}-migrate
  annotations:
    helm.sh/hook: pre-install,pre-upgrade
    helm.sh/hook-weight: "0"
    helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded
spec:
  backoffLimit: 1
  activeDeadlineSeconds: 300
  template:
    spec:
      restartPolicy: Never
      containers:
        - name: migrate
          image: "{{ .Values.image }}:{{ .Chart.AppVersion }}"
          args: ["migrate", "up"]
          env:
            - name: DATABASE_URL
              valueFrom: {secretKeyRef: {name: {{ .Release.Name }}-db, key: url}}
EOF
cat > templates/configmap.yaml <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-config
data: {LOG_LEVEL: info}
EOF
helm template web . | grep -E '^# Source|^kind'
echo "--- with --no-hooks"
helm template web . --no-hooks | grep -E '^# Source|^kind'
helm template web . | kubeconform -strict -summary
# Source: linkly/templates/configmap.yaml
kind: ConfigMap
# Source: linkly/templates/migrate-job.yaml
kind: Job
--- with --no-hooks
# Source: linkly/templates/configmap.yaml
kind: ConfigMap
Summary: 2 resources found parsing stdin - Valid: 2, Invalid: 0, Errors: 0, Skipped: 0

helm template prints hooks along with the other manifests (--no-hooks drops them). That matters when something other than Helm applies the output, such as kubectl apply or a GitOps tool: the annotations then mean nothing to the API server, and the Job runs as an ordinary object, at the same time as everything else. Argo CD maps Helm hook annotations to its own sync phases; GitOps covers that.

Weights and delete policies

  • helm.sh/hook-weight: hooks of the same point run in ascending weight order (a string holding an integer, negative allowed). Helm waits for each weight group before starting the next; for Jobs and Pods it waits until they complete.
  • helm.sh/hook-delete-policy: hook objects are not part of the release, so Helm does not delete them on uninstall. The policy decides when Helm removes them:
Policy Deletes the hook object
before-hook-creation before creating it again on the next run (the default if no policy is set); avoids "already exists" errors
hook-succeeded right after it succeeds
hook-failed right after it fails (you lose the logs, so use it carefully)

before-hook-creation,hook-succeeded is the usual choice for migrations: clean up after success, keep a failed Job around to read its logs, and replace it on the next attempt.

What it looks like on a cluster (sample)

This needs a cluster, so it's a sample session. With linkly's chart, an upgrade waits for the migration first:

bash
helm upgrade web ./linkly -n linkly --wait --timeout 5m
kubectl get jobs -n linkly
text
Release "web" has been upgraded. Happy Helming!
NAME: web
LAST DEPLOYED: Tue Jan 13 10:42:07 2026
NAMESPACE: linkly
STATUS: deployed
REVISION: 4
DESCRIPTION: Upgrade complete
TEST SUITE: None
No resources found in linkly namespace.

No Job is left because hook-succeeded removed it. Had the migration failed, helm upgrade would print Error: UPGRADE FAILED: pre-upgrade hooks failed: …, the release revision would be marked failed, and the Deployment would still run the previous version.

Hook pitfalls

  • Hooks must be idempotent. A pre-upgrade migration runs on every upgrade, including one that only changes a replica count. Migration tools that track applied versions (golang-migrate, Flyway, Alembic) are fine.
  • Ordering against normal objects. A pre-install hook runs before the chart's ConfigMaps and Secrets exist. If the migration Job needs the database Secret, that Secret must already exist (an existing Secret, as in linkly's production setup) or itself be a hook with a lower weight.
  • Timeouts. Helm waits up to --timeout (default 5m0s) for each hook. A stuck migration blocks the deploy, so set activeDeadlineSeconds and backoffLimit on hook Jobs.
  • Expand and contract. During a rolling update old and new Pods run side by side, so migrations must be backwards compatible: add columns in one release, remove the old ones in a later one.

Test yourself

Why is pre-upgrade the right hook point for schema migrations?

  1. It runs after the new Pods are ready
  2. Helm waits for the hook to complete before updating the Deployment, and a failure stops the upgrade
  3. It runs only on the first install
  4. It runs in parallel with the rollout

Does helm uninstall delete hook objects?

  1. Yes, always
  2. No: hooks aren't part of the release; their delete policy decides when Helm removes them
  3. Only Jobs
  4. Only with --cascade

What happens to a Helm hook when the chart is applied with kubectl apply -f <(helm template …)?

  1. It runs first, as with Helm
  2. The annotations are ignored and the hook object is applied like any other object
  3. kubectl refuses it
  4. It is skipped