如何通过标签从fabric8 sharedIndexInformer中过滤事件?

huangapple go评论63阅读模式
英文:

How to filter events from fabric8 sharedIndexInformer by labels?

问题

我使用 fabric8 kubernetes client v6.8.0,并初始化一个用于 CRD 的 sharedIndexInformer,如下所示:

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

我只想接收具有特定标签的资源的事件。

在客户端的早期版本中,这似乎很简单,因为标签选择是sharedIndexInformerFor的一个参数之一。

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

如何在 v6.8.0+ 中实现这一点?

英文:

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

SharedIndexInformer&lt;MyResource&gt; 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&lt;MyResource&gt; informer =
                    sharedInformerFactory.sharedIndexInformerFor(
MyResource.class, 
myLabelSelector,
30 * 1000L);

How to achieve this in v6.8.0+?

答案1

得分: 1

要更精细地控制信息筛选,您可以使用Resource DSL上可用的inform()方法:

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

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

SharedIndexInformer&lt;MyResource&gt; informer = client.resources(MyResource.class)
  .inNamespace(&quot;default&quot;)
  .withLabel(&quot;app&quot;, &quot;test-operator&quot;)
  .inform(myEventHandler, 30 * 1000L);

huangapple
  • 本文由 发表于 2023年7月31日 21:12:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定