<p>I need to write a <code>golang</code> application with the help of <a href="https://github.com/kubernetes/client-go" rel="nofollow noreferrer">client-go</a> which will listen/watch a particular namespace for any of these events:</p>
<ul>
<li>A new pod has been created</li>
<li>A pod has been deleted</li>
<li>A new container has been added to existing pods</li>
<li>Image for container for any pod has changed</li>
</ul>
<p>And I want to communicate this information to another application application running in other namespace.</p>
<p>I am really new to the <code>client-go</code> library and I searched their documentation but couldn't find something similar to <a href="https://kopf.readthedocs.io/en/latest/events/" rel="nofollow noreferrer">Events in Kopf</a></p>
<p><strong>I am new to this library and I couldn't find a method/function of doing this. I don't need to have the full code of doing this, but I appreciate where I can look into, so I can find my way out</strong></p>
<p>Can someone help me on this?</p>

Jananath Banuka
<p>You can create a clientset from parsing the kubeconfig file and then use this clientset to create a sharedInformerfactory for your particular namespace. Get a informer for your pods and add Event Handler functions. Implement those functions according to your requirement. You can check for container updates between oldPod and newPod in the <code>OnUpdate</code> function. Use the clientset for however you want to communicate with other applications. I would say explore the methods that clientset implements to get a detailed idea how it works.</p>
<pre><code>package main

import (
    &quot;flag&quot;
    &quot;fmt&quot;
    corev1 &quot;k8s.io/api/core/v1&quot;
    &quot;k8s.io/apimachinery/pkg/util/runtime&quot;
    &quot;k8s.io/client-go/informers&quot;
    &quot;k8s.io/client-go/kubernetes&quot;
    &quot;k8s.io/client-go/tools/cache&quot;
    &quot;k8s.io/client-go/tools/clientcmd&quot;
    &quot;k8s.io/client-go/util/homedir&quot;
    &quot;k8s.io/klog/v2&quot;
    &quot;path/filepath&quot;
    &quot;time&quot;
)

func main() {
    // parse the .kubeconfig file
    var kubeconfig *string
    if home := homedir.HomeDir(); home != &quot;&quot; {
        kubeconfig = flag.String(&quot;kubeconfig&quot;, filepath.Join(home, &quot;.kube&quot;, &quot;config&quot;), &quot;(optional) absolute path to the kubeconfig file&quot;)
    } else {
        kubeconfig = flag.String(&quot;kubeconfig&quot;, &quot;&quot;, &quot;absolute path to the kubeconfig file&quot;)
    }
    flag.Parse()

    // create config from the kubeconfig
    config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, *kubeconfig)
    if err != nil {
        panic(err)
    }

    // create the clientset
    clientSet, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err)
    }

    // stop signal for the informer
    stopper := make(chan struct{})
    defer close(stopper)
    
    // create shared informers for resources in all known API group versions with a reSync period and namespace
    factory := informers.NewSharedInformerFactoryWithOptions(clientSet, 10*time.Second, informers.WithNamespace(&quot;demo&quot;))
    podInformer := factory.Core().V1().Pods().Informer()

    defer runtime.HandleCrash()

    // start informer -&gt;
    go factory.Start(stopper)

    // start to sync and call list
    if !cache.WaitForCacheSync(stopper, podInformer.HasSynced) {
        runtime.HandleError(fmt.Errorf(&quot;Timed out waiting for caches to sync&quot;))
        return
    }

    podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
        AddFunc:    onAdd, // register add eventhandler
        UpdateFunc: onUpdate,
        DeleteFunc: onDelete,
    })

    // block the main go routine from exiting
    &lt;-stopper
}

func onAdd(obj interface{}) {
    pod := obj.(*corev1.Pod)
    klog.Infof(&quot;POD CREATED: %s/%s&quot;, pod.Namespace, pod.Name)
}

func onUpdate(oldObj interface{}, newObj interface{}) {
    oldPod := oldObj.(*corev1.Pod)
    newPod := newObj.(*corev1.Pod)
    klog.Infof(
        &quot;POD UPDATED. %s/%s %s&quot;,
        oldPod.Namespace, oldPod.Name, newPod.Status.Phase,
    )
}

func onDelete(obj interface{}) {
    pod := obj.(*corev1.Pod)
    klog.Infof(&quot;POD DELETED: %s/%s&quot;, pod.Namespace, pod.Name)
}

</code></pre>
