Watching Kubernetes Pod, Namespace and Node status via Informer interface

8/9/2019

There is a way to set a watch on the K8s pod status and I could test the functionality.

The podWatchController then receives the events and respective handers are called. However, this mechanism does not work for Namespace and Nodes i.e. the options does not exist in NewListWatchFromClient constructor.

Please suggest how to use this patter to watch Node and Namespace status (add, delete, update).

    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)

I found another method based on NewSharedInformerFactory, which provides watcher for pods, nodes and namespaces; however, I did not see any notifications coming to the handler. What could be missing in this approach?

For 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)

For Namespace:

    // 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)
-- AnilJ
google-kubernetes-engine
kubernetes
kubernetes-pod

1 Answer

8/9/2019

I could get it working with following changes i.e. we have to call the Run() method of factory, informer and the controller.

sharedInformer.Start(podInformerChan)

podInformer.Run(podInformerChan)
podInformer.GetController().Run(podInformerChan)
nsInformer.Run(nsInformerChan)
nsInformer.GetController().Run(nsInformerChan)

There are however still some errors showing and trying to understand what they are. Currently, they are pointing to this line.

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>
-- AnilJ
Source: StackOverflow