下面是我的片段
factory := informers.NewFilteredSharedInformerFactory(clientset, 0, "", func(o *metaV1.ListOptions) {
o.LabelSelector = "node-role.kubernetes.io/master="
})
nodeInformer := factory.Core().V1().Nodes().Informer()
i.lister = factory.Core().V1().Nodes().Lister()
nodeInformer.AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
i.updateIPs()
},
UpdateFunc: func(oldobj, newObj interface{}) {
i.updateIPs()
},
DeleteFunc: func(obj interface{}) {
i.updateIPs()
},
})
factory.Start(ctx.Done())
if !cache.WaitForCacheSync(ctx.Done(), nodeInformer.HasSynced) {
runtime.HandleError(fmt.Errorf("Timed out waiting for caches to sync"))
return
}
完整代码位于:https ://github.com/tweakmy/fotofona/blob/master/nodeinformer.go
这是我运行单元测试时的结果:
$ go test -v -run TestInformer
=== RUN TestInformer
Update ip host [10.0.0.1]
cache is synced
--- PASS: TestInformer (3.10s)
这是预期的行为吗?如何在缓存同步后将其列出并在缓存同步后对事件处理程序做出反应。