Should Controller do cleanup for the Resource it's deleting?

2/17/2022

I am new to Kubernetes Operators. I have a general question about how to conceive of cleanup at the point of deletion.

Let's say the Controller is managing a resource which consists of a Deployment among other things. This Deployment writes to some external database. I'd like the items from the Database to be deleted when the resource is deleted (but not when its Pod is simply restarted - thus it can't happen as part of the application's shut down logic).

It seems like the database purging would have to happen in the Controller then? But this makes me a bit uneasy since it seems like this knowledge of how values are stored is the knowledge of the resource being managed, not the Controller. Is the only other good option to have the Controller send a message to the underlying application to perform its own cleanup?

What is the general way to handle this type of thing?

-- vmayer
kubernetes
kubernetes-operator

1 Answer

2/18/2022

Have you heard about Finalizers and Owner References in Kubernetes? It's the Owner references describe how groups of objects are related. They are properties on resources that specify the relationship to one another, so entire trees of resources can be deleted.

To avoid futher copy-pasting, I will just leave the links here: Understanding Finalizers

-- Bazhikov
Source: StackOverflow