Is it possible to watch all the events on a Kubernetes cluster using the python API?

8/7/2018

Using the kubernetes Python API< you have to specify the events to watch? Is it possible to watch all events in the cluster

-- clout_chaser
kubernetes
kubernetes-python-client

2 Answers

3/19/2019

From what I understand you are looking for this.

import kubernetes as k8s

core_api = k8s.client.CoreV1Api()
watcher = k8s.watch.Watch()
stream = watcher.stream(core_api.list_event_for_all_namespaces, timeout_seconds=5)
for raw_event in stream:
    logging.info("Kubernetes Event: %s %s" % (raw_event['type'],raw_event['object'].metadata.name))

I haven't tested the snippet, but it should work.

-- amitection
Source: StackOverflow

8/8/2018

Yes.

I am pretty sure that there is a library of Python that already implement this but in my case i implement it using the command: --watch-only

For example: kubectl get pods --watch-only > -> will show only the changes in pods. Creating python process that collect the info from the will trigger only new changes.

-- Oron Golan
Source: StackOverflow