Sub topic 11
Hooks and helm test
This chapter assumes you can install and upgrade a release, read Helm templates, and know what a Kubernetes Job and Pod are. Here you will run work at set points in a release's life and check a running release with helm test.
What a hook is
A hook is a normal manifest in templates/ with a helm.sh/hook annotation. Helm does not create it with the rest of the chart. It creates it at the lifecycle event the annotation names, then waits for it to finish (a Job must complete, a Pod must succeed) before it continues. If a hook fails, the whole operation fails.
Common events:
| Annotation value | When it runs |
|---|---|
| `pre-install` | After templates render, before any resource is created |
| `post-install` | After all resources are created |
| `pre-upgrade`, `post-upgrade` | Before or after an upgrade |
| `pre-delete`, `post-delete` | Before or after an uninstall |
| `pre-rollback`, `post-rollback` | Before or after a rollback |
| `test` | Only when you run `helm test` |
A migration hook
This Job runs before every install and upgrade. It is deleted after it succeeds.
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "webapp.fullname" . }}-migrate
annotations:
"helm.sh/hook": pre-install,pre-upgrade
"helm.sh/hook-weight": "-5"
"helm.sh/hook-delete-policy": hook-succeeded
spec:
template:
spec:
restartPolicy: Never
containers:
- name: migrate
image: busybox
command: ["echo", "running migrations"]Two more annotations control behavior:
helm.sh/hook-weightis a string holding an integer. Hooks for the same event run in ascending order, lowest first. Ties are broken by resource name.helm.sh/hook-delete-policyacceptsbefore-hook-creation(the default),hook-succeeded, andhook-failed.
The default policy removes the previous hook object before a new one is created. Without it, a second install fails because a Job with that name already exists.
Hook gotchas
Helm does not track hook resources as part of the release. helm uninstall leaves them in the cluster unless a delete policy removes them. Choose a policy deliberately. hook-succeeded keeps the namespace clean but deletes the logs you would read after a failure. before-hook-creation keeps the last run for inspection.
A pre-upgrade hook runs before the new version of the chart's other resources exist. If the Job needs a ConfigMap or Secret that this same release creates or changes, it sees the old version or none. On a first install it sees nothing. Either make the hook self-contained, or mark that ConfigMap as a hook with a lower weight.
Chart tests
A test is a hook with the value test. It is usually a Pod that exits 0 on success. helm create generates one in templates/tests/. Here is a version for the webapp chart:
apiVersion: v1
kind: Pod
metadata:
name: {{ include "webapp.fullname" . }}-test
annotations:
"helm.sh/hook": test
"helm.sh/hook-delete-policy": before-hook-creation
spec:
restartPolicy: Never
containers:
- name: wget
image: busybox
command: ["wget"]
args: ['-qO-', 'http://{{ include "webapp.fullname" . }}:{{ .Values.service.port }}']Tests never run during install or upgrade. You run them against a live release:
helm install my-release ./webapp -n demo --create-namespace --wait
helm test my-release -n demo --logs--wait makes the install return only after the Pods are ready, so the test does not race the rollout. --logs prints the test Pod's output. The command exits non-zero if any test Pod fails, so CI can use it as a gate.
Keep tests small and read-only. A good test checks that the Service answers, not that the application is correct. Put full integration testing elsewhere.
Exercise
A Job annotated "helm.sh/hook": pre-upgrade needs a Secret created by the same chart. After the first install, the upgrade works. On a brand-new install the Job is not run at all. Why is this described correctly or incorrectly, and what should you annotate to make the Job run on first install?
Show answer
The description is wrong in one part. A pre-upgrade hook only runs on upgrades, so it never runs during the first install. To run on both, annotate it "helm.sh/hook": pre-install,pre-upgrade. On a first install the Secret does not exist yet when a pre-install hook runs, so the Secret must also be a hook with a lower weight, or the Job must not depend on it.