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

Commit 34a4b58

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

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
@@ -18,6 +18,7 @@
1818
from kubernetes import client
1919

2020
PYDOC_RETURN_LABEL = ":return:"
21+
PYDOC_FOLLOW_PARAM = ":param bool follow:"
2122

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

6869
def stop(self):
6970
self._stop = True
@@ -76,8 +77,17 @@ def get_return_type(self, func):
7677
return return_type[:-len(TYPE_LIST_SUFFIX)]
7778
return return_type
7879

80+
def get_watch_argument_name(self, func):
81+
if PYDOC_FOLLOW_PARAM in pydoc.getdoc(func):
82+
return 'follow'
83+
else:
84+
return 'watch'
85+
7986
def unmarshal_event(self, data, return_type):
80-
js = json.loads(data)
87+
try:
88+
js = json.loads(data)
89+
except ValueError:
90+
return data
8191
js['raw_object'] = js['object']
8292
if return_type:
8393
obj = SimpleNamespace(data=json.dumps(js['raw_object']))
@@ -120,7 +130,7 @@ def stream(self, func, *args, **kwargs):
120130

121131
self._stop = False
122132
return_type = self.get_return_type(func)
123-
kwargs['watch'] = True
133+
kwargs[self.get_watch_argument_name(func)] = True
124134
kwargs['_preload_content'] = False
125135

126136
timeouts = ('timeout_seconds' in kwargs)
@@ -132,9 +142,12 @@ def stream(self, func, *args, **kwargs):
132142
if self._stop:
133143
break
134144
finally:
135-
kwargs['resource_version'] = self.resource_version
136145
resp.close()
137146
resp.release_conn()
147+
if self.resource_version is not None:
148+
kwargs['resource_version'] = self.resource_version
149+
else:
150+
break
138151

139152
if timeouts or self._stop:
140153
break

0 commit comments

Comments
 (0)