Skip to content
This repository has been archived by the owner on Mar 13, 2022. It is now read-only.

Commit

Permalink
Making watch work with read_namespaced_pod_log.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitar committed Oct 16, 2018
1 parent 8d7b6f7 commit 34a4b58
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions watch/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from kubernetes import client

PYDOC_RETURN_LABEL = ":return:"
PYDOC_FOLLOW_PARAM = ":param bool follow:"

# Removing this suffix from return type name should give us event's object
# type. e.g., if list_namespaces() returns "NamespaceList" type,
Expand Down Expand Up @@ -63,7 +64,7 @@ def __init__(self, return_type=None):
self._raw_return_type = return_type
self._stop = False
self._api_client = client.ApiClient()
self.resource_version = 0
self.resource_version = None

def stop(self):
self._stop = True
Expand All @@ -76,8 +77,17 @@ def get_return_type(self, func):
return return_type[:-len(TYPE_LIST_SUFFIX)]
return return_type

def get_watch_argument_name(self, func):
if PYDOC_FOLLOW_PARAM in pydoc.getdoc(func):
return 'follow'
else:
return 'watch'

def unmarshal_event(self, data, return_type):
js = json.loads(data)
try:
js = json.loads(data)
except ValueError:
return data
js['raw_object'] = js['object']
if return_type:
obj = SimpleNamespace(data=json.dumps(js['raw_object']))
Expand Down Expand Up @@ -120,7 +130,7 @@ def stream(self, func, *args, **kwargs):

self._stop = False
return_type = self.get_return_type(func)
kwargs['watch'] = True
kwargs[self.get_watch_argument_name(func)] = True
kwargs['_preload_content'] = False

timeouts = ('timeout_seconds' in kwargs)
Expand All @@ -132,9 +142,12 @@ def stream(self, func, *args, **kwargs):
if self._stop:
break
finally:
kwargs['resource_version'] = self.resource_version
resp.close()
resp.release_conn()
if self.resource_version is not None:
kwargs['resource_version'] = self.resource_version
else:
break

if timeouts or self._stop:
break

0 comments on commit 34a4b58

Please sign in to comment.