Skip to content
This repository has been archived by the owner on Oct 3, 2020. It is now read-only.

Pass arbitrary params to the watching GET queries #33

Merged
merged 1 commit into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pykube/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ def get_or_none(self, *args, **kwargs):
except ObjectDoesNotExist:
return None

def watch(self, since=None):
def watch(self, since=None, *, params=None):
query = self._clone(WatchQuery)
query.params = params
if since is now:
query.resource_version = self.response["metadata"]["resourceVersion"]
elif since is not None:
Expand Down Expand Up @@ -156,11 +157,13 @@ class WatchQuery(BaseQuery):

def __init__(self, *args, **kwargs):
self.resource_version = kwargs.pop("resource_version", None)
self.params = None
super(WatchQuery, self).__init__(*args, **kwargs)
self._response = None

def object_stream(self):
params = {"watch": "true"}
params = dict(self.params or {}) # shallow clone for local use
params["watch"] = "true"
if self.resource_version is not None:
params["resourceVersion"] = self.resource_version
kwargs = {
Expand Down
15 changes: 15 additions & 0 deletions tests/test_watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ def test_watch_response_is_set_on_iter(api):
assert api.get.call_count == 1
assert api.get.call_args_list[0][1]['stream'] is True
assert 'watch=true' in api.get.call_args_list[0][1]['url']


def test_watch_params_are_passed_through(api):
line1 = json.dumps({'type': 'ADDED', 'object': {}}).encode('utf-8')
expected_response = MagicMock()
expected_response.iter_lines.return_value = [line1]
api.get.return_value = expected_response

params = dict(timeoutSeconds=123, arbitraryParam=456)
stream = Query(api, Pod).watch(params=params)
next(iter(stream))

assert api.get.call_count == 1
assert 'timeoutSeconds=123' in api.get.call_args_list[0][1]['url']
assert 'arbitraryParam=456' in api.get.call_args_list[0][1]['url']