DEV Community

Maxime Guilbert
Maxime Guilbert

Posted on • Updated on

Execute temporary treatments with Helm

When we deploy an helm chart, it's possible that we want to do some actions before and/or after. We think about using Jobs, but they are not the best because we can't configure easily when to execute them or about their auto deletion (cf https://dev.to/mxglt/automatically-delete-a-job-in-kubernetes-21oc).

So, if you are using Helm, we will see how to do it with Hooks.

What are hooks?

Hooks are mecanism which allows to execute some treatments at particular moments of the helm life cycle.

To define these treatments, we will add some annotations to a Kubernetes Job to define 3 things:

  • Moment where the job should be executed
  • its priority (if multiple jobs are started with the same hook)
  • How to delete it

Moment where the job should be executed

With the annotation helm.sh/hook, we can define moments where the job should be executed.

Currently, available values are :

  • pre-install : Executes after templates are rendered, but before any resources are created in Kubernetes (Not executed in an upgrade)
  • post-install : Executes after all resources are loaded into Kubernetes (Not executed in an upgrade)
  • pre-delete : Executes on a deletion request before any resources are deleted from Kubernetes
  • post-delete : Executes on a deletion request after all of the release's resources have been deleted
  • pre-upgrade : Executes on an upgrade request after templates are rendered, but before any resources are updated (Not executed in an install)
  • post-upgrade : Executes on an upgrade request after all resources have been upgraded (Not executed in an install)
  • pre-rollback : Executes on a rollback request after templates are rendered, but before any resources are rolled back
  • post-rollback : Executes on a rollback request after all resources have been modified
  • test : Executes when the Helm test subcommand is invoked

With all these possibilities, it becomes easier to :

  • install CRDs before to install an operator
  • execute script to clean something after a deletion
  • have some script to move between two versions

Example

annotations:
    "helm.sh/hook": post-install,post-upgrade
Enter fullscreen mode Exit fullscreen mode

Its priority

To define the priority of a job, we must use the annotation helm.sh/hook-weight.

Its value must be a number declared as a string. This number can be positive or negative.

Example

annotations:
    "helm.sh/hook-weight": "-5"
Enter fullscreen mode Exit fullscreen mode

How to delete it

After the end of a job, we want to be able to manage how it should be deleted. Helm gives us the possibility to configure it with the annotation helm.sh/hook-ddelete-policy.

3 values are available :

  • before-hook-creation : Delete the previous instance of the job before anotherhook started (default)
  • hook-succeeded : Delete the job if the hook was successful
  • hook-failed : Delete the job if the hook fails

Example

annotations:
    "helm.sh/hook-delete-policy": hook-succeeded
Enter fullscreen mode Exit fullscreen mode

In conclusion, we saw that Helm gives us a little bit more dynamism which is really helpful to have a complete automated flow for an application.


Links


I hope it will help you! 🍺


You want to support me?

Buy Me A Coffee

Top comments (0)