k8s v1.19.0
Shutdown等待结束流程
第1步:reflector停止list/watch
第2步:Delta先进先出队列对象全部Pop
业务代码处理事件
场景1:Shutdown方法等待EventHandler回调方法完成
klog.InitFlags(nil)
flag.Parse()
defer klog.Flush()
cfg, err := clientcmd.BuildConfigFromFlags("", "/root/.kube/config")
if err != nil {
klog.Fatalf("Error building kubeconfig: %s", err.Error())
}
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
klog.Fatalf("Error building kubernetes clientset: %s", err.Error())
}
informerFactory := informers.NewSharedInformerFactory(kubeClient, 0)
hasSynced := informerFactory.Core().V1().Nodes().Informer().HasSynced
informerFactory.Core().V1().Nodes().Lister()
informerFactory.Core().V1().Nodes().Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
klog.Infof("handle node add")
time.Sleep(time.Minute)
},
})
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")
time.Sleep(5 * time.Second)
close(newStopCh)
klog.Infof("begin to shutdown")
informerFactory.Shutdown()
klog.Infof("shutdown end")
Shutdown方法等待EventHandler中AddFunc处理完成。
标签:err,informerFactory,cache,Infof,client,Shutdown,go,klog From: https://www.cnblogs.com/WJQ2017/p/18012610