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

Commit

Permalink
Pass arbitrary params to the watching GET queries (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
nolar authored and hjacobs committed Jul 12, 2019
1 parent 23ce164 commit 51736e1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
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']

0 comments on commit 51736e1

Please sign in to comment.