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.
More on Kubernetes, Docker & SSH clients
How does the official Kubernetes Python client authenticate?BeginnerHow do you list pods with high restart counts using the Python client?BeginnerHow do you patch or scale a Deployment with the Python client?IntermediateHow do you apply a YAML manifest from Python?AdvancedHow do you use the Docker SDK for Python?BeginnerHow do you clean up dangling Docker images and stopped containers with Python?Intermediate