<p>I am new to <code>golang</code> and I have the following requirement:</p>
<ol>
<li>I have a deployment running with the image <code>nginx:latest</code></li>
<li>Then manually someone update this image to something else (eg: <code>nginx:1.22</code>)</li>
<li>I need to get the old image version and the new image version</li>
</ol>
<p>So, I researched on the &quot;Shared Informers&quot; in Go Lang. And I wrote this:</p>
<pre><code>
func main() {

. . . 

    // create shared informers for resources in all known API group versions with a reSync period and namespace
    factory := informers.NewSharedInformerFactoryWithOptions(clientSet, 1*time.Hour, informers.WithNamespace(namespace_to_watch))
    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,
    })
}

func onUpdate(oldObj interface{}, newObj interface{}) {
    oldPod := oldObj.(*corev1.Pod)
    newPod := newObj.(*corev1.Pod)

    for container_old := range oldPod.Spec.Containers {
       fmt.Printf(&quot;Container Old :%s&quot;, container_old.Image )
    }

    for container_new := range newPod.Spec.Containers {
       fmt.Printf(&quot;Container New :%s&quot;, container_new.Image )
    }
  
}
</code></pre>
<p>As above, according to the <code>onUpdate</code> function, I should be getting the two values, instead, I am getting the same value on each update as below:</p>
<blockquote>
<p>Container Old : nginx:latest</p>
</blockquote>
<blockquote>
<p>Container New : nginx:latest</p>
</blockquote>
<blockquote>
<p>Container Old : nginx:1.22</p>
</blockquote>
<blockquote>
<p>Container New : nginx:1.22</p>
</blockquote>
<blockquote>
<p>Container Old : nginx:1.22</p>
</blockquote>
<blockquote>
<p>Container New : nginx:1.22</p>
</blockquote>
<p>Above output is because somehow, the <code>onUpdate</code> function is triggered thrice, but as you can see in all the times, the values are the same (for each output).
But I was expecting something like below:</p>
<blockquote>
<p>Container Old : nginx:latest</p>
</blockquote>
<blockquote>
<p>Container New : nginx:1.22</p>
</blockquote>
<p>Can someone help me with this?</p>

Jananath Banuka
<p>When an image is updated in the deployments, the pods restart with new image containers and there's no trace of the old pod spec remains. You should watch the deployments instead of the pods and check the image between old deployment and new deployment object. Here's a sample of how you can modify your <code>onUpdate</code> method.</p>
<pre class="lang-golang prettyprint-override"><code>func onUpdate(oldObj interface{}, newObj interface{}) {
    oldDepl := oldObj.(*v1.Deployment)
    newDepl := newObj.(*v1.Deployment)

    for oldContainerID := range oldDepl.Spec.Template.Spec.Containers {
        for newContainerID := range newDepl.Spec.Template.Spec.Containers {
            if oldDepl.Spec.Template.Spec.Containers[oldContainerID].Name == newDepl.Spec.Template.Spec.Containers[newContainerID].Name {
                if oldDepl.Spec.Template.Spec.Containers[oldContainerID].Image != newDepl.Spec.Template.Spec.Containers[newContainerID].Image {
                    fmt.Printf(&quot;CONTAINER IMAGE UPDATED FROM %s to %s&quot;,
                        oldDepl.Spec.Template.Spec.Containers[oldContainerID].Image, newDepl.Spec.Template.Spec.Containers[newContainerID].Image)
                }
            }
        }
    }
}
</code></pre>
