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

Commit 8e6f043

Browse files
committed
Making watch work with read_namespaced_pod_log.
Fixes kubernetes-client/python#199.
1 parent d2ac885 commit 8e6f043

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

Diff for: watch/watch.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from kubernetes import client
2121

2222
PYDOC_RETURN_LABEL = ":return:"
23+
PYDOC_FOLLOW_PARAM = ":param bool follow:"
2324

2425
# Removing this suffix from return type name should give us event's object
2526
# type. e.g., if list_namespaces() returns "NamespaceList" type,
@@ -65,7 +66,7 @@ def __init__(self, return_type=None):
6566
self._raw_return_type = return_type
6667
self._stop = False
6768
self._api_client = client.ApiClient()
68-
self.resource_version = 0
69+
self.resource_version = None
6970

7071
def stop(self):
7172
self._stop = True
@@ -78,8 +79,17 @@ def get_return_type(self, func):
7879
return return_type[:-len(TYPE_LIST_SUFFIX)]
7980
return return_type
8081

82+
def get_watch_argument_name(self, func):
83+
if PYDOC_FOLLOW_PARAM in pydoc.getdoc(func):
84+
return 'follow'
85+
else:
86+
return 'watch'
87+
8188
def unmarshal_event(self, data, return_type):
82-
js = json.loads(data)
89+
try:
90+
js = json.loads(data)
91+
except ValueError:
92+
return data
8393
js['raw_object'] = js['object']
8494
if return_type:
8595
obj = SimpleNamespace(data=json.dumps(js['raw_object']))
@@ -122,7 +132,7 @@ def stream(self, func, *args, **kwargs):
122132

123133
self._stop = False
124134
return_type = self.get_return_type(func)
125-
kwargs['watch'] = True
135+
kwargs[self.get_watch_argument_name(func)] = True
126136
kwargs['_preload_content'] = False
127137
if 'resource_version' in kwargs:
128138
self.resource_version = kwargs['resource_version']
@@ -136,9 +146,12 @@ def stream(self, func, *args, **kwargs):
136146
if self._stop:
137147
break
138148
finally:
139-
kwargs['resource_version'] = self.resource_version
140149
resp.close()
141150
resp.release_conn()
151+
if self.resource_version is not None:
152+
kwargs['resource_version'] = self.resource_version
153+
else:
154+
break
142155

143156
if timeouts or self._stop:
144157
break

0 commit comments

Comments
 (0)