I'm trying to watch the kubernetes resource like pods with kubernetes python client(version 9.0), but the method of watch.stream() seems to be suspended when i watch the k8s resource after few minutes later
v1 = client.CoreV1Api()
w = watch.Watch()
for resource in w.stream(v1.list_namespaced_pod):
dosomething(resource)
how do i always keep watching the kebernetes pod
You are hitting probably this issue, and as workaround mentioned in this comment you can try like below:
from kubernetes import client, config, watch
from urllib3.exceptions import ProtocolError
config.load_kube_config()
api_instance = client.CoreV1Api()
while True:
w = watch.Watch()
try:
for event in w.stream(api_instance.list_namespaced_pod, namespace="default"):
print("Event: %s %s %s" % (event['type'],event['object'].kind, event['object'].metadata.name))
except ProtocolError:
print("watchPodEvents ProtocolError, continuing..")
Hope it helps!