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

Commit ff02e98

Browse files
author
Chen Li
committed
Update continue the watch with resource_version
1 parent 8f7b490 commit ff02e98

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

Diff for: watch/watch.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def __init__(self, return_type=None):
6363
self._raw_return_type = return_type
6464
self._stop = False
6565
self._api_client = client.ApiClient()
66+
self.resource_version = 0
6667

6768
def stop(self):
6869
self._stop = True
@@ -81,16 +82,16 @@ def unmarshal_event(self, data, return_type):
8182
if return_type:
8283
obj = SimpleNamespace(data=json.dumps(js['raw_object']))
8384
js['object'] = self._api_client.deserialize(obj, return_type)
85+
if hasattr(js['object'],'metadata'):
86+
self.resource_version = js['object'].metadata.resource_version
8487
return js
8588

86-
def stream(self, func, keep=False, *args, **kwargs):
89+
def stream(self, func, *args, **kwargs):
8790
"""Watch an API resource and stream the result back via a generator.
8891
8992
:param func: The API function pointer. Any parameter to the function
9093
can be passed after this parameter.
9194
92-
:param keep: Flag to keep the watch work all the time.
93-
9495
:return: Event object with these keys:
9596
'type': The type of event such as "ADDED", "DELETED", etc.
9697
'raw_object': a dict representing the watched object.
@@ -116,6 +117,7 @@ def stream(self, func, keep=False, *args, **kwargs):
116117
kwargs['watch'] = True
117118
kwargs['_preload_content'] = False
118119

120+
timeouts = ('timeout_seconds' in kwargs)
119121
while True:
120122
resp = func(*args, **kwargs)
121123
try:
@@ -124,8 +126,9 @@ def stream(self, func, keep=False, *args, **kwargs):
124126
if self._stop:
125127
break
126128
finally:
129+
kwargs['resource_version'] = self.resource_version
127130
resp.close()
128131
resp.release_conn()
129132

130-
if not keep or self._stop:
133+
if timeouts or self._stop:
131134
break

Diff for: watch/watch_test.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def test_watch_stream_twice(self):
8585
fake_resp.close.assert_called_once()
8686
fake_resp.release_conn.assert_called_once()
8787

88-
def test_watch_stream_keep(self):
88+
def test_watch_stream_loop(self):
8989
w = Watch(float)
9090

9191
fake_resp = Mock()
@@ -99,12 +99,14 @@ def test_watch_stream_keep(self):
9999
fake_api.get_namespaces.__doc__ = ':return: V1NamespaceList'
100100

101101
count = 0
102-
for e in w.stream(fake_api.get_namespaces):
103-
count = count + 1
104102

103+
# when timeout_seconds is set, auto-exist when timeout reaches
104+
for e in w.stream(fake_api.get_namespaces, timeout_seconds=1):
105+
count = count + 1
105106
self.assertEqual(count, 1)
106107

107-
for e in w.stream(fake_api.get_namespaces, True):
108+
# when no timeout_seconds, only exist when w.stop() is called
109+
for e in w.stream(fake_api.get_namespaces):
108110
count = count + 1
109111
if count == 2:
110112
w.stop()

0 commit comments

Comments
 (0)