python kubernetes watch crashes

9/15/2020

I have been trying to use the python kubernetes API to stream the output of kubernetes logs. I plan to retrieve the logs and ultimately use web Socket to stream logs.

from kubernetes import watch
...
def getLog(pod,namespace):
   w = watch.Watch()
   return w.stream(v1.read_namespaced_pod_log, name=pod, namespace=namespace)
main():
   result = getLog(pod, namespace)
   for line in result:
      print(line)

However i get the error below.

Traceback (most recent call last):
  File "./kubernetes-project", line 176, in <module>
    main()
  File "...venv/lib/python3.6/site-packages/kubernetes/watch/watch.py", line 143, in stream
    yield self.unmarshal_event(line, return_type)
  File "...venv/lib/python3.6/site-packages/kubernetes/watch/watch.py", line 91, in unmarshal_event
    js['raw_object'] = js['object']
TypeError: 'int' object is not subscriptable

The expected output should be below. However, the logs were streaming until 128,. 3 is the expected output in the next line.

...
"random_crop_size": [
   384,
   128, **streaming stops here and error starts showing below**
   3
...

I suspect that the error could be due to the line with only 3 is being convert to an int object and not string thus watch can't process it.

Any idea how to solve this?

-- Ccc Av
kubernetes
python

1 Answer

9/16/2020

The issue had been tracked & resolved in the latest version of kubernetes. See https://github.com/kubernetes-client/python/issues/983 at v11.0.0

-- Ccc Av
Source: StackOverflow