Sub topic 15
Hooks and Chart Tests
This chapter assumes you can write a chart with templates and values, and that you know how Helm renders and installs a release. Here you will add a hook that runs at a point in the release lifecycle, and a test that checks a running release.
What a hook is
A hook is a normal Kubernetes manifest in templates/ that carries a helm.sh/hook annotation. Helm does not treat it as part of the release's regular resources. It creates the hook at the lifecycle event named in the annotation and, for Jobs and Pods, waits for it to finish.
Common hook events:
| Event | When it runs |
|---|---|
| `pre-install` | After templates render, before any resource is created |
| `post-install` | After all resources are created |
| `pre-upgrade` | Before an upgrade changes resources |
| `post-upgrade` | After an upgrade completes |
| `pre-delete` | Before a release is deleted |
| `test` | When you run `helm test` |
A pre-install migration Job
This Job runs before the Deployment exists. It reuses the helpers from earlier chapters.
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "webapp.fullname" . }}-migrate
labels:
{{- include "webapp.labels" . | nindent 4 }}
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
template:
spec:
restartPolicy: Never
containers:
- name: migrate
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
command: ["sh", "-c", "echo running migrations"]The annotations control the behavior:
helm.sh/hooklists one or more events, separated by commas.helm.sh/hook-weightorders hooks of the same event. Helm sorts weights in ascending order. Weights are strings.helm.sh/hook-delete-policysays when Helm removes the hook resource.before-hook-creationdeletes the previous copy before a new run, which avoids "already exists" errors on upgrade.hook-succeededcleans up after success.
Chart tests
A chart test is a hook with the event test. It is a Pod that exits with code 0 on success. Put it in templates/tests/.
apiVersion: v1
kind: Pod
metadata:
name: {{ include "webapp.fullname" . }}-test-connection
labels:
{{- include "webapp.labels" . | nindent 4 }}
annotations:
"helm.sh/hook": test
"helm.sh/hook-delete-policy": before-hook-creation
spec:
restartPolicy: Never
containers:
- name: wget
image: busybox:1.36
command: ["wget"]
args: ["-qO-", "http://{{ include "webapp.fullname" . }}:{{ .Values.service.port }}"]This assumes the chart's Service is named webapp-fullname through the same helper. Run it against an installed release:
helm install my-release ./webapp
helm test my-releaseCommon errors
- Hooks are not managed with the release.
helm uninstalldoes not delete a hook resource unless a delete policy removes it. Leftover Jobs cause name collisions on the next run, so setbefore-hook-creation. - A failed pre-install hook blocks the install. Helm marks the release as failed and creates nothing else. Check the Job logs with
kubectl logs. - Hook resources need the resources they use. A
pre-installJob runs before the chart's ConfigMaps and Secrets exist. Usepre-installhooks only for dependencies that already exist, or give the needed resources their own hook annotation and a lower weight. - Tests run only when you ask.
helm installdoes not runtesthooks.
Exercise
A pre-upgrade Job fails on its second upgrade with the error that the Job already exists. Which annotation fixes this, and with what value?
Show answer
Add "helm.sh/hook-delete-policy": before-hook-creation. Helm then deletes the old Job before it creates the new one.