Sub topic 8
Ordering hooks and cleaning up hook resources
This chapter assumes you know Helm 3 basics (install, upgrade, release), can read Go templates in templates/, and understand Kubernetes Jobs and Pods. It also assumes you have seen the helm.sh/hook annotation. Here you control the order hooks run in and decide what happens to their resources afterward.
How Helm orders hooks
A release event can trigger several hooks. For example, pre-upgrade may fire a migration Job and a backup Job. Helm sorts the hooks for one event in this order:
- By
helm.sh/hook-weight, from lowest to highest. - By resource kind, if weights are equal.
- By resource name, if kind is also equal.
The weight is a string that holds an integer. It can be negative. If you omit it, the weight is 0.
Helm runs hooks one at a time and waits for each one to be ready before it starts the next. A Job is ready when it completes. A Pod is ready when it reaches Succeeded. Other kinds are ready once they are created.
A pre-upgrade hook that fails stops the upgrade before the main resources change. A failing post-upgrade hook marks the release as failed, even though the main resources are already applied.
Setting weights
Give the backup Job the lower weight so it runs first.
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "webapp.fullname" . }}-db-backup
annotations:
"helm.sh/hook": pre-upgrade
"helm.sh/hook-weight": "-10"
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
backoffLimit: 0
template:
spec:
restartPolicy: Never
containers:
- name: backup
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
command: ["./backup"]The migration Job from the earlier example uses "-5". Because -10 sorts before -5, the backup finishes first.
Quote the weight. Annotation values must be strings, so "-5" is correct and a bare -5 is rejected.
Delete policies
Helm does not manage hook resources the way it manages normal ones. It creates them, but helm uninstall does not remove them. Without a cleanup policy, old Jobs pile up, and a second run can fail because a Job with the same name already exists.
The helm.sh/hook-delete-policy annotation fixes this. You can list several values, separated by commas.
| Policy | When Helm deletes the resource |
|---|---|
| `before-hook-creation` | Just before a new hook of the same name is created. This is the default if you set no policy. |
| `hook-succeeded` | After the hook runs successfully. |
| `hook-failed` | After the hook fails. |
Choose based on what you need afterward:
before-hook-creationalone keeps the last run around, so you can read its logs withkubectl logs. The next run removes it.before-hook-creation,hook-succeededkeeps the namespace clean on success and keeps failed Jobs for debugging. This is a good default for migrations.- Adding
hook-failedremoves failures too. You then lose the logs, so use it only when something else collects them.
Hooks with the same name
Hook resources are plain Kubernetes objects, so their names must not collide. A fixed name like db-migrate works with before-hook-creation, because the old Job is deleted first. Without that policy, the second helm upgrade fails with an "already exists" error.
Prefixing with the release name, as in {{ include "webapp.fullname" . }}-db-migrate, avoids collisions between releases in the same namespace.
Seeing the order for yourself
helm template prints hooks along with normal manifests. Each hook carries its annotations, so you can check weights without installing.
helm template demo ./webapp --show-only templates/db-backup-job.yamlTo watch the real order, install with --debug and observe the Jobs:
helm upgrade demo ./webapp --debug
kubectl get jobs --watchThe backup Job should reach Completed before the migrate Job appears.
Summary of the decision
Use weights when one hook depends on another. Use before-hook-creation on every hook so reruns do not collide. Add hook-succeeded when you do not need a record of successful runs.
Exercise
A pre-upgrade migration Job has weight "5" and a pre-upgrade backup Job has weight "-3". Which runs first? Which delete policy lets you read the logs of a failed migration while keeping the namespace clean after successful runs?
Show answer
The backup Job runs first, because -3 is lower than 5. Use before-hook-creation,hook-succeeded. Successful Jobs are deleted. A failed Job stays until the next run, so you can read its logs.
Exercise
A second helm upgrade fails with "jobs.batch db-migrate already exists". The Job has a hook annotation but no hook-delete-policy. What do you add, and why?
Show answer
Add "helm.sh/hook-delete-policy": before-hook-creation. Helm then deletes the existing Job before it creates the new one, so the name no longer collides. If you set no policy at all, Helm uses this behavior by default. A collision like this therefore suggests the annotation has a typo or an invalid value, so check it first.