kubernetes / client-go

Go client for Kubernetes.
Apache License 2.0
8.99k stars 2.94k forks source link

K8s client. PodList items aren't updated. #1248

Closed BreakTheBeta closed 1 year ago

BreakTheBeta commented 1 year ago

Package name: k8s.io/client-go v0.22.5

Issue: When getting a list of pods k8s.CoreV1().Pods(namespace).List(ctx, listOptions) If you monitor the list of returned pods and get their Phase "Running" will be returned even if the pod has been terminated by kubernetes for a new rendition of that pod:

pods, err := k8s.CoreV1().Pods(namespace).List(ctx, listOptions)
// err checking
for {
    for _, pod := range pods.Items {
        fmt.Println(pod.status.Phase) // Will always return "Running" even after pod has died
    }
}
liggitt commented 1 year ago

Only the List call communicates with the server. At that point, you have a static snapshot of Pod objects that does not update if more changes are made to the objects on the server.

To see new data, you would need to relist and get a new copy from the server, or better, watch for changes (see https://github.com/kubernetes/client-go/blob/master/examples/workqueue/main.go for an example of watching and handling add/update/delete events using an informer)