Java Operator SDK to listen for custom resources

99 Views Asked by At

I am implementing a Java service that uses the Java Operator SDK to listen for specific custom resources. I am implementing both Reconciler and Cleaner because I want to get notified even when a CRs is deleted.

The issue is that implementing the Cleaner means that my application changes the CRs to add finalizers. I don't have permission to change the CRs though.

In general, my application simply needs to:

  • List the CRs at startup
  • Get an event when one of these CRs gets deleted
  • No need to do any update to those CRs

Is the Reconciler a suitable pattern for this use case? Should I look at something else?

2

There are 2 best solutions below

1
Fabrizio Fortino On

For this scenario, the operator SDK is not the best choice. Cleaners attempt to add finalizers to the CRs, so it's not an option in my case.

Shared informers, provided by K8s Fabric8, are more than enough and quite easy to implement.

1
Christophe Laprun On

While you certainly don't need a Java Operator SDK (JOSDK) Reconciler for this use case, not being able to modify the CRs has a significant drawback, which is why JOSDK automatically activates finalizers for resources that are handled by a Reconciler implementing the Cleaner interface.

Using a simple SharedInformer can certainly be enough if your use case doesn't require that you process all deletion events, without exception.

In the absence of finalizers, though, if your app / operator gets re-scheduled or killed by Kubernetes, you might lose those deletion events as they are not blocking: Kubernetes will mark a resource as deleted but the deletion happens asynchronuously (which can happen quickly or not depending on the target resource). If resources are deleted while your app is down, and there are no finalizers to indicate that your app is interested in being notified about the resources deletion, then these resources might get deleted without your application being none the wiser. This might be fine for your use case.

If you need to account for all deletion events, though, even the ones that might occur while your app is not running, then you need finalizers to prevent Kubernetes from deleting these resources before your app/operator got a chance to process them. See the relevant section in the JOSDK documentation for more details.