informer增加过滤标签

labelOptions := informers.WithTweakListOptions(func(opts *metav1.ListOptions) {
	opts.LabelSelector = "a=b"
})
informerFactory := informers.NewSharedInformerFactoryWithOptions(kubeClient, 0, labelOptions)
hasSynced := informerFactory.Core().V1().Nodes().Informer().HasSynced
informerFactory.Core().V1().Nodes().Lister()
informer := informerFactory.Core().V1().Nodes().Informer()
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
	AddFunc: func(obj interface{}) {
		klog.Infof("add node %s", obj.(*core_v1.Node).Name)
	},
})

newStopCh := make(chan struct{})
informerFactory.Start(newStopCh)

klog.Infof("begin to wait cache sync")
if ok := cache.WaitForCacheSync(newStopCh, hasSynced); !ok {
	klog.Fatalf("failed to wait for caches to sync")
}
klog.Infof("wait cache sync end")

defer informerFactory.Shutdown()
defer close(newStopCh)

posted on 2024-02-04 22:02  王景迁  阅读(28)  评论(0编辑  收藏  举报

导航