How to watch changes to an object

In this notebook, we learn how kubernetes API resource Watch endpoint is used to observe resource changes. It can be used to get information about changes to any kubernetes object.


In [ ]:
from kubernetes import client, config, watch

Load config from default location.


In [ ]:
config.load_kube_config()

Create API instance


In [ ]:
api_instance = client.CoreV1Api()

Run a Watch on the Pods endpoint.

Watch would be executed and produce output about changes to any Pod. After running the cell below, You can test this by running the Pod notebook create_pod.ipynb and observing the additional output here. You can stop the cell from running by restarting the kernel.


In [ ]:
w = watch.Watch()
for event in w.stream(api_instance.list_pod_for_all_namespaces):
    print("Event: %s %s %s" % (event['type'],event['object'].kind, event['object'].metadata.name))

In [ ]: