Home > OS >  How to trigger some action when K8 namespace is deleted
How to trigger some action when K8 namespace is deleted

Time:05-31

Is there any default options in kubernetes, to trigger some actions when the resource gets deleted from the cluster?

CodePudding user response:

The default Kubernetes way of doing this is to use an operator.

In a nutshell, you have a software running that is watching resources (Namespaces in your case) and react when some namespace changes (deleted in your case).

You might want to add finalizers to Namespaces for proper cleanup.

Please refer to the documentation for more details.

CodePudding user response:

You can deploy your own service as admission webhook. You can create one that has so-called side effects, meaning it does not only interact with the resource in question but may do other things like creating a volume or similar. See the docs: https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/.

There are many ways you could implement such a service. For example, with go, you can use the same structs Kubernetes is using itself, which can reduce the development effort. See this package: https://pkg.go.dev/k8s.io/api/admission/v1. You need to create a small web server that handles a post request, decoding the body into an AdmissionReview struct.

There are also third party tools that can do it for you. I havent used it yet, but I think kyverno could fit here: https://kyverno.io/docs/introduction/.

  • Related