Interview Prep/Python for DevOps/Kubernetes, Docker & SSH clients

How does `watch.Watch()` work, and what is a `410 Gone`?

AdvancedConceptKubernetes, Docker & SSH clients
Answer

A watch streams ADDED/MODIFIED/DELETED events from a resourceVersion onward.

from kubernetes import watch
w = watch.Watch()
rv = v1.list_namespaced_pod("prod").metadata.resource_version
for ev in w.stream(v1.list_namespaced_pod, "prod", resource_version=rv, timeout_seconds=300):
    print(ev["type"], ev["object"].metadata.name)

The API server only keeps a window of history. If you reconnect with an old resourceVersion, you get 410 Gone ("too old resource version") and must re-list and start again from the new version. Long-running watchers must handle that, plus timeouts and reconnects. For serious controllers use kopf or Go's controller-runtime, which handle informers and caching.

All Python for DevOps questions Roadmap: Python for automation

More on Kubernetes, Docker & SSH clients