2 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊
您可以直接在中提供標(biāo)簽字符串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)

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個(gè)贊
如果您想繼續(xù)使用k8s.io/apimachinery/pkg/labels和選擇包,您需要在調(diào)用時(shí)重新分配選擇器,selector.Add 因?yàn)樗祷匾粋€(gè)副本。
所以,更換
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
- 2 回答
- 0 關(guān)注
- 115 瀏覽
添加回答
舉報(bào)