Home > Net >  How to get list of events using the Python kubernetes API?
How to get list of events using the Python kubernetes API?

Time:06-14

I am trying to obtain the list of events from a minikube cluster usigh the Python Kubernetes api using the following code:

from kubernetes import config, client


config.load_kube_config()
api = client.EventsV1beta1Api()
print(api.list_event_for_all_namespaces())

I am getting the following error:

C:\Users\lameg\kubesense>python test.py 
Traceback (most recent call last):
  File "C:\Users\lameg\kubesense\test.py", line 6, in <module>
    print(api.list_event_for_all_namespaces())
  File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api\events_v1beta1_api.py", line 651, in list_event_for_all_namespaces
    return self.list_event_for_all_namespaces_with_http_info(**kwargs)  # noqa: E501
  File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api\events_v1beta1_api.py", line 758, in list_event_for_all_namespaces_with_http_info
    return self.api_client.call_api(
  File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 348, in call_api
    return self.__call_api(resource_path, method,
  File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 192, in __call_api
    return_data = self.deserialize(response_data, response_type)
  File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 264, in deserialize
    return self.__deserialize(data, response_type)
  File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 303, in __deserialize
    return self.__deserialize_model(data, klass)
  File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 639, in __deserialize_model
    kwargs[attr] = self.__deserialize(value, attr_type)
  File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 280, in __deserialize
    return [self.__deserialize(sub_data, sub_kls)
  File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 280, in <listcomp>
    return [self.__deserialize(sub_data, sub_kls)
  File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 303, in __deserialize
    return self.__deserialize_model(data, klass)
  File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\api_client.py", line 641, in __deserialize_model
    instance = klass(**kwargs)
  File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\models\v1beta1_event.py", line 112, in __init__
    self.event_time = event_time
  File "C:\Users\lameg\miniforge3\lib\site-packages\kubernetes\client\models\v1beta1_event.py", line 291, in event_time
    raise ValueError("Invalid value for `event_time`, must not be `None`")  # noqa: E501
ValueError: Invalid value for `event_time`, must not be `None`

Any ideas ?

CodePudding user response:

That looks like either a bug in the Python client, or a bug in the OpenAPI specification used to generate the client: clearly, null is a value for eventTime that is supported by the API.

I think the only workaround is to monkey-patch the kubernetes.client module so that it accepts null values. Something like this:

from kubernetes import config, client


config.load_kube_config()
api = client.EventsV1beta1Api()

# This is descriptor, see https://docs.python.org/3/howto/descriptor.html
class FakeEventTime:
    def __get__(self, obj, objtype=None):
        return obj._event_time

    def __set__(self, obj, value):
        obj._event_time = value


# Monkey-patch the `event_time` attribute of ` the V1beta1Event class.
client.V1beta1Event.event_time = FakeEventTime()

# Now this works.
events = api.list_event_for_all_namespaces()

The above code runs successfully against my OpenShift instance, whereas previously it would fail as you describe in your question.

  • Related