javakubernetesfabric8fabric8-kubernetes-client

How to filter events from fabric8 sharedIndexInformer by labels?


I am on fabric8 kubernetes client v6.8.0 and initializing a sharedIndexInformer for a CRD like this:

SharedIndexInformer<MyResource> informer =
                sharedInformerFactory.sharedIndexInformerFor(MyResource.class, 30 * 1000L);
informer.addEventHandler(myEventHandler);

I only want to receive events from resources with certain labels.

This seemed straightforward in earlier version of the client, with label selection being one of the arguments in sharedIndexInformerFor.

SharedIndexInformer<MyResource> informer =
                    sharedInformerFactory.sharedIndexInformerFor(
MyResource.class, 
myLabelSelector,
30 * 1000L);

How to achieve this in v6.8.0+?


Solution

  • For more fine grained control over informers filtering, you can use the inform() method available on Resource DSL:

    SharedIndexInformer<MyResource> informer = client.resources(MyResource.class)
      .inNamespace("default")
      .withLabel("app", "test-operator")
      .inform(myEventHandler, 30 * 1000L);