Skip to content

Commit 1e559d9

Browse files
committed
fix watching with a specified resource version
The watch code reset the version to the last found in the response. When you first list existing objects and then start watching from that resource version the existing versions are older than the version you wanted and the watch starts from the wrong version after the first restart. This leads to for example already deleted objects ending in the stream again. Fix this by setting the minimum resource version to reset from to the input resource version. As long as k8s returns all objects in order in the watch this should work. We cannot use the integer value of the resource version to order it as one should be treat the value as opaque. Closes kubernetes-client/python#700
1 parent 5c242ea commit 1e559d9

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

watch/watch.py

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ def stream(self, func, *args, **kwargs):
122122
return_type = self.get_return_type(func)
123123
kwargs['watch'] = True
124124
kwargs['_preload_content'] = False
125+
if 'resource_version' in kwargs:
126+
self.resource_version = kwargs['resource_version']
125127

126128
timeouts = ('timeout_seconds' in kwargs)
127129
while True:

watch/watch_test.py

+64-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414

1515
import unittest
1616

17-
from mock import Mock
17+
from mock import Mock, call
1818

1919
from .watch import Watch
20+
callcount = 0
2021

2122

2223
class WatchTests(unittest.TestCase):
@@ -62,6 +63,68 @@ def test_watch_with_decode(self):
6263
fake_resp.close.assert_called_once()
6364
fake_resp.release_conn.assert_called_once()
6465

66+
def test_watch_resource_version_set(self):
67+
# gh-700 ensure watching from a resource version does reset to resource
68+
# version 0 after k8s resets the watch connection
69+
fake_resp = Mock()
70+
fake_resp.close = Mock()
71+
fake_resp.release_conn = Mock()
72+
values = [
73+
'{"type": "ADDED", "object": {"metadata": {"name": "test1",'
74+
'"resourceVersion": "1"}, "spec": {}, "status": {}}}\n',
75+
'{"type": "ADDED", "object": {"metadata": {"name": "test2",'
76+
'"resourceVersion": "2"}, "spec": {}, "sta',
77+
'tus": {}}}\n'
78+
'{"type": "ADDED", "object": {"metadata": {"name": "test3",'
79+
'"resourceVersion": "3"}, "spec": {}, "status": {}}}\n'
80+
]
81+
# return nothing on the first call and values on the second
82+
# this emulates a watch from a rv that returns nothing in the first k8s
83+
# watch reset and values later
84+
def get_values(*args, **kwargs):
85+
global callcount
86+
callcount += 1
87+
if callcount == 1:
88+
return []
89+
else:
90+
return values
91+
92+
fake_resp.read_chunked = Mock(
93+
side_effect=get_values)
94+
95+
fake_api = Mock()
96+
fake_api.get_namespaces = Mock(return_value=fake_resp)
97+
fake_api.get_namespaces.__doc__ = ':return: V1NamespaceList'
98+
99+
w = Watch()
100+
count = 1
101+
# ensure we keep our requested resource version or the version latest
102+
# returned version when the existing versions are older than the
103+
# requested version
104+
# needed for the list existing objects, then watch from there use case
105+
calls = []
106+
first = True
107+
for e in w.stream(fake_api.get_namespaces, resource_version="5"):
108+
count += 1
109+
# first call must use the passed rv
110+
# following calls the last rv of the returned values
111+
if count % 3 == 0:
112+
if first:
113+
rv = "5"
114+
first = False
115+
else:
116+
# ideally we want 5 here but as rv must be treated as an
117+
# opaque value we cannot interpret it and order it so rely
118+
# on k8s returning the events completely and in order
119+
rv = "3"
120+
calls.append(call(_preload_content=False, watch=True,
121+
resource_version=rv))
122+
# returned
123+
if count == len(values) * 3:
124+
w.stop()
125+
126+
fake_api.get_namespaces.assert_has_calls(calls)
127+
65128
def test_watch_stream_twice(self):
66129
w = Watch(float)
67130
for step in ['first', 'second']:

0 commit comments

Comments
 (0)