有一种方法可以设置对K8s pod状态的监视,我可以测试其功能。
然后,podWatchController接收事件,并调用相应的处理程序。但是,此机制不适用于命名空间和节点,即NewListWatchFromClient构造函数中不存在这些选项。
请建议如何使用此模式查看Node和Namespace状态(添加、删除、更新)。
podWatchlist := cache.NewListWatchFromClient(
k8s.kubeClient.K8sClient.CoreV1().RESTClient(),
string(v1.ResourcePods),
v1.NamespaceAll,
fields.Everything(),
)
// K8s Pod watcher controller
_, podWatchController := cache.NewInformer( // also take a look at NewSharedIndexInformer
podWatchlist,
&v1.Pod{},
0, //Duration is int64
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
k8s.handleAddPod(obj)
},
DeleteFunc: func(obj interface{}) {
k8s.handleDeletePod(obj)
},
UpdateFunc: func(oldObj, newObj interface{}) {
k8s.handleUpdatePod(oldObj, newObj)
},
},
)
podStopChan := make(chan struct{})
go podWatchController.Run(podStopChan)我发现了另一种基于NewSharedInformerFactory的方法,它为pods、节点和名称空间提供了监视器;但是,我没有看到处理程序收到任何通知。这种方法可能会遗漏什么?
对于Pod:
// Add watcher for the Pod.
factory := informers.NewSharedInformerFactory(k8s.kubeClient.K8sClient, 0)
podInformer := factory.Core().V1().Pods().Informer()
podInformerChan := make(chan struct{})
defer close(podInformerChan)
// Pod informer state change handler
podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
// When a new pod gets created
AddFunc: func(obj interface{}) {
k8s.handleAddPod(obj)
},
// When a pod gets updated
UpdateFunc: func(oldObj interface{}, newObj interface{}) {
k8s.handleUpdatePod(oldObj, newObj)
},
// When a pod gets deleted
DeleteFunc: func(obj interface{}) {
k8s.handleDeletePod(obj)
},
})
go podInformer.GetController().Run(podInformerChan)对于命名空间:
// Add watcher for the Namespace.
factory := informers.NewSharedInformerFactory(k8s.kubeClient.K8sClient, 0)
nsInformer := factory.Core().V1().Namespaces().Informer()
nsInformerChan := make(chan struct{})
defer close(nsInformerChan)
// Namespace informer state change handler
nsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs {
// When a new namespace gets created
AddFunc: func(obj interface{}) {
k8s.handleAddNamespace(obj)
},
// When a namespace gets updated
UpdateFunc: func(oldObj interface{}, newObj interface{}) {
k8s.handleUpdateNamespace(obj)
},
// When a namespace gets deleted
DeleteFunc: func(obj interface{}) {
k8s.handleDeleteNamespace(obj)
},
})
go nsInformer.GetController().Run(nsInformerChan)发布于 2019-08-10 07:05:10
我可以通过以下更改让它工作,即我们必须调用工厂、通知程序和控制器的Run()方法。
sharedInformer.Start(podInformerChan)
podInformer.Run(podInformerChan)
podInformer.GetController().Run(podInformerChan)
nsInformer.Run(nsInformerChan)
nsInformer.GetController().Run(nsInformerChan)然而,仍然有一些错误显示并试图理解它们是什么。目前,他们正指向这条线。
https://github.com/kubernetes/client-go/blob/master/tools/cache/shared_informer.go#L612
E0809 15:33:39.187411 79537 shared_informer.go:611] unrecognized notification: <nil>
E0809 15:33:40.191304 79537 shared_informer.go:611] unrecognized notification: <nil>
E0809 15:33:48.227933 79537 shared_informer.go:611] unrecognized notification: <nil>
E0809 15:33:54.231458 79537 shared_informer.go:611] unrecognized notification: <nil>https://stackoverflow.com/questions/57436619
复制相似问题