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)
标签:informerFactory,标签,cache,newStopCh,过滤,informer,klog,Nodes
From: https://www.cnblogs.com/WJQ2017/p/18007092