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

Commit 9d521a1

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 9d521a1

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-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

+66-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414

1515
import unittest
1616

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

1919
from .watch import Watch
2020

21+
callcount = 0
22+
2123

2224
class WatchTests(unittest.TestCase):
2325

@@ -62,6 +64,69 @@ def test_watch_with_decode(self):
6264
fake_resp.close.assert_called_once()
6365
fake_resp.release_conn.assert_called_once()
6466

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

0 commit comments

Comments
 (0)