SharedInformerFactoryWithOptions – 无法根据标签进行过滤。

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

SharedInformerFactoryWithOptions - Not able to filter based on labels

问题

我想要监视特定应用程序的Kubernetes Pod事件。

我使用了NewSharedInformerFactoryWithOptions。我已经添加了适当的标签选择器,但是它没有被过滤掉。因为我想要使用标签'app=nats-box'进行过滤。

以下是代码示例 -

req, err := labels.NewRequirement("app", selection.Equals, []string{"nats-box"})
	if err != nil {
		log.Fatalf("error %s, bad requirement", err.Error())
	}
selector := labels.NewSelector()
selector.Add(*req)

filteredFactory := informers.NewSharedInformerFactoryWithOptions(clientset, 0, informers.WithNamespace("default"), informers.WithTweakListOptions(func(options *metav1.ListOptions) {
	options.LabelSelector = selector.String()
}))

podInformer := filteredFactory.Core().V1().Pods().Informer()

podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
	AddFunc: func(obj interface{}) {
		fmt.Println("pod add")
		name := obj.(*corev1.Pod).Name
		labels := obj.(*corev1.Pod).Labels
		fmt.Printf("pod Name: %s\nLabels - %v\n\n", name, labels)
	})

我得到了所有Pod的输出,命名空间被过滤了,但是标签没有被过滤。我无法找出我犯了什么错误。

英文:

I want to watch Kubernetes pod events for a certain application.

I went with NewSharedInformerFactoryWithOptions. I have added appropriate labels selector, But it is not getting filtered out. As I want to filter it using the label: 'app=nats-box'

Here is the code -

req, err := labels.NewRequirement("app", selection.Equals, []string{"nats-box"})
	if err != nil {
		log.Fatalf("error %s, bad requirement", err.Error())
	}
selector := labels.NewSelector()
selector.Add(*req)

filteredFactory := informers.NewSharedInformerFactoryWithOptions(clientset, 0, informers.WithNamespace("default"), informers.WithTweakListOptions(func(options *metav1.ListOptions) {
	options.LabelSelector = selector.String()
}))

podInformer := filteredFactory.Core().V1().Pods().Informer()

podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
	AddFunc: func(obj interface{}) {
		fmt.Println("pod add")
		name := obj.(*corev1.Pod).Name
		labels := obj.(*corev1.Pod).Labels
		fmt.Printf("pod Name: %s\nLabels - %v\n\n", name, labels)
	})

I get the output for all the pods, the namespace is getting filtered but not the labels. Not able to figure out where I am making the mistake.

答案1

得分: 4

你可以直接在opts.LabelSelector中提供标签字符串:

labelOptions := informers.WithTweakListOptions(func(opts *metav1.ListOptions) {
	opts.LabelSelector = "app=nats-box"
})
factory := informers.NewSharedInformerFactoryWithOptions(clientset, 0, informers.WithNamespace("default"), labelOptions)
informer := factory.Core().V1().Pods().Informer()
stopper := make(chan struct{})
defer close(stopper)
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
	AddFunc: func(obj interface{}) {
		fmt.Println("pod add")
		name := obj.(*corev1.Pod).Name
		labels := obj.(*corev1.Pod).Labels
		fmt.Printf("pod Name: %s\nLabels - %v\n\n", name, labels)
	},
})
informer.Run(stopper)
英文:

You can directly provide the label string in the opts.LabelSelector:

labelOptions := informers.WithTweakListOptions(func(opts *metav1.ListOptions) {
	opts.LabelSelector = "app=nats-box"
})
factory := informers.NewSharedInformerFactoryWithOptions(clientset, 0, informers.WithNamespace("default") ,labelOptions)
informer := factory.Core().V1().Pods().Informer()
stopper := make(chan struct{})
defer close(stopper)
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
	AddFunc: func(obj interface{}) {
		fmt.Println("pod add")
		name := obj.(*corev1.Pod).Name
		labels := obj.(*corev1.Pod).Labels
		fmt.Printf("pod Name: %s\nLabels - %v\n\n", name, labels)
	},
})
informer.Run(stopper)

答案2

得分: 1

如果你想继续使用k8s.io/apimachinery/pkg/labels和selection包,当调用selector.Add时,你需要重新分配选择器,因为它会返回一个副本。

所以,将以下代码片段:

selector := labels.NewSelector()
selector.Add(*req)
fmt.Println(selector.String()) // 空字符串

替换为:

selector := labels.NewSelector()
selector = selector.Add(*req)
fmt.Println(selector.String()) // app=nats-box

你可以在这里查看更多信息。

英文:

If you want to keep using the k8s.io/apimachinery/pkg/labels and selection packages, you need to re-assign selector when calling selector.Add because it returns a copy.

So, replace

selector := labels.NewSelector()
selector.Add(*req)
fmt.Println(selector.String()) // empty string

with

selector := labels.NewSelector()
selector = selector.Add(*req)
fmt.Println(selector.String()) // app=nats-box

huangapple
  • 本文由 发表于 2021年11月3日 16:52:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/69821992.html
匿名

发表评论

匿名网友

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

确定