Skip to content

Commit 5367344

Browse files
committed
Fix pod log stream follow detection
1 parent f8c3ba2 commit 5367344

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

kubernetes/base/watch/watch.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from kubernetes import client
2020

2121
PYDOC_RETURN_LABEL = ":rtype:"
22-
PYDOC_FOLLOW_PARAM = ":param bool follow:"
22+
PYDOC_FOLLOW_PARAMS = (":param bool follow:", ":param follow:")
2323

2424
# Removing this suffix from return type name should give us event's object
2525
# type. e.g., if list_namespaces() returns "NamespaceList" type,
@@ -111,7 +111,8 @@ def get_return_type(self, func):
111111
return return_type
112112

113113
def get_watch_argument_name(self, func):
114-
if PYDOC_FOLLOW_PARAM in pydoc.getdoc(func):
114+
doc = pydoc.getdoc(func)
115+
if any(param in doc for param in PYDOC_FOLLOW_PARAMS):
115116
return 'follow'
116117
else:
117118
return 'watch'

kubernetes/base/watch/watch_test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from kubernetes import client, config
2121
from kubernetes.client import ApiException
22+
from kubernetes.client.exceptions import ApiTypeError
2223

2324
from .watch import Watch
2425

@@ -223,6 +224,44 @@ def test_watch_for_follow(self):
223224
fake_resp.close.assert_called_once()
224225
fake_resp.release_conn.assert_called_once()
225226

227+
def test_watch_for_follow_with_split_param_type_docstring(self):
228+
fake_resp = Mock()
229+
fake_resp.close = Mock()
230+
fake_resp.release_conn = Mock()
231+
fake_resp.stream = Mock(
232+
return_value=[
233+
'log_line_1\n',
234+
'log_line_2\n'])
235+
236+
def read_pod_log(*args, **kwargs):
237+
if 'watch' in kwargs:
238+
raise ApiTypeError(
239+
"Got an unexpected keyword argument 'watch'"
240+
" to method read_namespaced_pod_log")
241+
return fake_resp
242+
243+
fake_api = Mock()
244+
fake_api.read_namespaced_pod_log = Mock(side_effect=read_pod_log)
245+
fake_api.read_namespaced_pod_log.__doc__ = (
246+
':param follow: Follow the log stream of the pod. Defaults to false.\n'
247+
':type follow: bool\n'
248+
':rtype: str')
249+
250+
w = Watch()
251+
count = 1
252+
for e in w.stream(fake_api.read_namespaced_pod_log):
253+
self.assertEqual("log_line_1", e)
254+
count += 1
255+
if count == 2:
256+
w.stop()
257+
258+
fake_api.read_namespaced_pod_log.assert_called_once_with(
259+
_preload_content=False, follow=True)
260+
fake_resp.stream.assert_called_once_with(
261+
amt=None, decode_content=False)
262+
fake_resp.close.assert_called_once()
263+
fake_resp.release_conn.assert_called_once()
264+
226265
def test_watch_resource_version_set(self):
227266
# https://github.com/kubernetes-client/python/issues/700
228267
# ensure watching from a resource version does reset to resource

0 commit comments

Comments
 (0)