Reputation: 418
I'm trying to write code that behaves like
kubectl get pods --watch
.
This way I'm triggered every time the status of the pod changes.
I created a go
project (that runs in the cluster) and added the following code:
podsWatcher, err := restAPIClient.CoreV1().Pods("").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
}
}
I receive an event every time I make a major change in the pod, but not for all of the events. (events).
How can I get a trigger of every change that happened in the status of the pods (like the --watch
flag)?
Upvotes: 2
Views: 9015
Reputation: 418
Apparently the watch is triggered by every change (made in pod describe) and I was looking at the pod.status.phase
instead of looking at the pod.Status.ContainerStatuses
, that is why I thought I did not receive every event.
I added a function that will handle the events as following:
// get ContainerStatuses. If there is no containerStatus, return the pod phase
func getPodStatus(pod *core.Pod) string {
containerStatuses := pod.Status.ContainerStatuses
status := ""
if len(containerStatuses) > 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 == "" { // if none of the containers report an error
status = "Running"
}
}
}
}
if status == "" {
status = string(pod.Status.Phase)
}
return status
}
// PodWatch watch pod changes in all namespaces
func PodWatch() error {
podsWatcher, err := restAPIClient.CoreV1().Pods("").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))
}
}
}
This solution suits my needs, if you wish to implement the --watch
like the kubectl, you can find the implementation here.
Upvotes: 2