<p>I'm trying to write code that behaves like
<code>kubectl get pods --watch</code>.
This way I'm triggered every time the status of the pod changes.</p>
<p>I created a <code>go</code> project (that runs in the cluster) and added the following code:</p>
<pre><code>podsWatcher, err := restAPIClient.CoreV1().Pods(&quot;&quot;).Watch(globalHTTPContext, metav1.ListOptions{Watch: true})
if err != nil {
    // do something
}
podsChan := podsWatcher.ResultChan()
for event := range podsChan {
    switch event.Type {
    case watch.Added:
        // do something
    case watch.Modified:
        // do something
    case watch.Deleted:
        // do something
    case watch.Bookmark:
        // do something
    case watch.Error:
        // do something
    }
}
</code></pre>
<p>I receive an event every time I make a major change in the pod, but not for all of the events. (<a href="https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/events/event.go" rel="nofollow noreferrer">events</a>).</p>
<p>How can I get a trigger of every change that happened in the status of the pods (like the <code>--watch</code> flag)?</p>

<p>Apparently the watch is triggered by every change (made in pod describe) and I was looking at the <code>pod.status.phase</code> instead of looking at the <code>pod.Status.ContainerStatuses</code>, that is why I thought I did not receive every event.</p>
<p>I added a function that will handle the events as following:</p>
<pre><code>// get ContainerStatuses. If there is no containerStatus, return the pod phase
func getPodStatus(pod *core.Pod) string {
    containerStatuses := pod.Status.ContainerStatuses
    status := &quot;&quot;
    if len(containerStatuses) &gt; 0 {
        for i := range containerStatuses {
            if containerStatuses[i].State.Terminated != nil {
                status = containerStatuses[i].State.Terminated.Reason
            }
            if containerStatuses[i].State.Waiting != nil {
                status = containerStatuses[i].State.Waiting.Reason
            }
            if containerStatuses[i].State.Running != nil {
                if status == &quot;&quot; { // if none of the containers report an error
                    status = &quot;Running&quot;
                }
            }
        }
    }
    if status == &quot;&quot; {
        status = string(pod.Status.Phase)
    }
    return status
}

// PodWatch watch pod changes in all namespaces
func PodWatch() error {
    podsWatcher, err := restAPIClient.CoreV1().Pods(&quot;&quot;).Watch(globalHTTPContext, metav1.ListOptions{Watch: true})
    if err != nil {
        return err
    }
    podsChan := podsWatcher.ResultChan()
    for event := range podsChan {
        pod, err := event.Object.(*core.Pod)
        if err != nil {
            return err
        }
        switch event.Type {
        case watch.Added:
            fmt.Println(getPodStatus(pod))
        case watch.Modified:
            fmt.Println(getPodStatus(pod))
        case watch.Deleted:
            fmt.Println(getPodStatus(pod))
        case watch.Bookmark:
            fmt.Println(getPodStatus(pod))
        case watch.Error:
            fmt.Println(getPodStatus(pod))
        }
    }
}
</code></pre>
<p>This solution suits my needs, if you wish to implement the <code>--watch</code> like the kubectl, you can find the implementation <a href="https://github.com/kubernetes/kubectl/blob/01523fda8977ab734a77d1b6e3ca47caf387e7ee/pkg/cmd/get/get.go#L175" rel="nofollow noreferrer">here</a>.</p>
