Search code examples
pythonkuberneteskubernetes-python-client

Python Kubernetes client equivalent of kubectl describe pod | grep Events


I'm creating a custom monitoring script for my self hosted kubernetes cluster on AWS. Among all other tools, Python client was of much interest for me. I have got a dictionary of pod and their status. Now I want to iterate the dict.keys (the pods in them basically) to check their Events from kubelet. I'm looking for the below data:

Events:
  Type     Reason       Age                    From                                                  Message
  ----     ------       ----                   ----                                                  -------
  Warning  FailedMount  20m (x25188 over 44d)  kubelet, ip-10-222-145-32.us-west-2.compute.internal  MountVolume.SetUp failed for volume "sag-license-volume" : configmap "my-licence" not found
  Warning  FailedMount  35s (x72078 over 44d)  kubelet, ip-10-222-155-32.us-west-2.compute.internal  (combined from similar events): MountVolume.SetUp failed for volume "my-license-volume" : configmap "my-license" not found

I looked over the web and could only find examples related to namespace but not for individual pods.

Kindly help me to get this data over Python client. Thanks in Advance !


Solution

  • Field selector did the trick, with list_namespaced_event this was resolved.

    field_selector='involvedObject.name='+'my_pod'
    stream = watch.Watch().stream(v1.list_namespaced_event, "my_ns", field_selector=field_selector, timeout_seconds=1)
    for event in stream:
        print(event['object'].message)