SharedInformerFactoryWithOptions - 无法根据标签进行过滤
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)
})
我得到了所有 pods 的输出,命名空间正在过滤,但标签没有。无法弄清楚我在哪里犯了错误。
您可以直接在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)
如果要继续使用k8s.io/apimachinery/pkg/labels
和选择包,调用selector.Add
时需要re-assign选择器 because it returns a copy.
所以,替换
selector := labels.NewSelector()
selector.Add(*req)
fmt.Println(selector.String()) // empty string
和
selector := labels.NewSelector()
selector = selector.Add(*req)
fmt.Println(selector.String()) // app=nats-box
我想查看某个应用程序的 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)
})
我得到了所有 pods 的输出,命名空间正在过滤,但标签没有。无法弄清楚我在哪里犯了错误。
您可以直接在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)
如果要继续使用k8s.io/apimachinery/pkg/labels
和选择包,调用selector.Add
时需要re-assign选择器 because it returns a copy.
所以,替换
selector := labels.NewSelector()
selector.Add(*req)
fmt.Println(selector.String()) // empty string
和
selector := labels.NewSelector()
selector = selector.Add(*req)
fmt.Println(selector.String()) // app=nats-box