3 min read | by Jordi Prats
Sometimes when we are writing a helm chart we realize that we have some parts of the task that need to be performed before of after deploying certain objects. We can also need to execute some Jobs in order to, for example, update the database, but not continuously, just when we update the application.
To handle this kind of situations we can use helm hooks
A helm hook it's just a regular Kubernetes object with some extra annotations. For example:
apiVersion: batch/v1
kind: Job
metadata:
name: "sql-init"
labels:
app: "init-psql"
annotations:
"helm.sh/hook": "pre-install,pre-upgrade"
"helm.sh/hook-delete-policy": "before-hook-creation"
"helm.sh/hook-weight": "0"
spec:
template:
metadata:
labels:
component: init-sql
spec:
volumes:
- name: init-db-scripts
configMap:
name: "init-db-scripts"
containers:
- name: check-indexes
image: "postgres:10"
command:
- sh
- "-x"
- "/opt/scripts/init-db.sh"
volumeMounts:
- name: init-db-scripts
mountPath: /opt/scripts
Let's take a look at these annotations
The helm.sh/hook specifies the ordering we want for this objects:
So, on the example above this object would be created on before the resources are created (pre-install) or updated (pre-update)
The helm.sh/hook-delete-policy let us configure what to do with the resources that the hook creates:
Finally, if we have several hooks, with helm.sh/hook-weight we set the order we want them to be applied
Posted on 19/08/2021