Search code examples
kubernetesapache-zookeeperkubernetes-custom-resourceskubernetes-operator

Can I execute a prestop hook only before deletion?


My Go-based custom resource operator needs some cleanup operations before it is deleted. It has to delete a specific znode from the ZooKeeper.

These operations must not be executed before regenerating resource. They have to be executed with the user's deletion command only. Thus, I can't use an ordinary prestop-hook.

Can I execute a prestop hook only before deletion? Or is there any other way for the operator to execute cleanup logic before the resource is deleted?


Solution

  • Can I execute a prestop hook only before deletion?

    This is the whole purpose of the preStop hook. A pre-stop hook is executed immediately before the container is terminated. Once there as terminatino signal from API, Kubelet runs the pre-stop hook and afterwards sends the SIGTERM signal to the process.

    Its design was to perform arbitrary operations before shutdown without having to implement those operations in the application itself. This is especially useful if you run some 3rd party app which code you can't modify.

    Now the call the to terminate pod and invoke the hook can be due to API request, probes failed, resource contention and others.

    For more reading please visit: Container Lifecycle Hooks