// https://www.cnblogs.com/yangyuliufeng/p/13611126.html
stopCH := make(chan struct{})
defer close(stopCH)
sharedInformers := informers.NewSharedInformerFactory(clientset, time.Minute)
informer := sharedInformers.Core().V1().Pods().Informer()
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ //为Pod资源添加资源事件回调方法
AddFunc: func(obj interface{}){
mObj := obj.(v1.Object)
log.Print("创建新Pod:",mObj.GetName())
},
UpdateFunc: func(oldObj, newObj interface{}){
oObj := oldObj.(v1.Object)
nObj := newObj.(v1.Object)
log.Print(oObj.GetName(),",",nObj.GetName())
},
DeleteFunc: func(obj interface{}) {
mObj :=obj.(v1.Object)
log.Print("删除旧Pod:",mObj.GetName())
},
})
informer.Run(stopCH)
k8s informer