Skip to content
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
17 changes: 15 additions & 2 deletions kubernetes/dynamic/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ def server_side_apply(self, resource, body=None, name=None, namespace=None, forc

return self.request('patch', path, body=body, force_conflicts=force_conflicts, **kwargs)

def watch(self, resource, namespace=None, name=None, label_selector=None, field_selector=None, resource_version=None, timeout=None, watcher=None, allow_watch_bookmarks=None, send_initial_events=None, resource_version_match=None):
def watch(
self, resource, namespace=None, name=None, label_selector=None,
field_selector=None, resource_version=None, timeout=None, watcher=None,
allow_watch_bookmarks=None, send_initial_events=None,
resource_version_match=None, _request_timeout=None
):
"""
Stream events for a resource from the Kubernetes API

Expand All @@ -169,7 +174,10 @@ def watch(self, resource, namespace=None, name=None, label_selector=None, field_
:param field_selector: The field selector with which to filter results
:param resource_version: The version with which to filter results. Only events with
a resource_version greater than this value will be returned
:param timeout: The amount of time in seconds to wait before terminating the stream
:param timeout: The server-side amount of time in seconds to wait before terminating
the stream
:param _request_timeout: The client-side request timeout in seconds, or a
(connect, read) timeout tuple
:param watcher: The Watcher object that will be used to stream the resource
:param allow_watch_bookmarks: Ask the API server to send BOOKMARK events
:param send_initial_events: Ask the API server to begin the stream with synthetic events
Expand Down Expand Up @@ -199,6 +207,10 @@ def watch(self, resource, namespace=None, name=None, label_selector=None, field_
if name:
field_selector = f"metadata.name={name}"

request_timeout_kwargs = {}
if _request_timeout is not None:
request_timeout_kwargs['_request_timeout'] = _request_timeout

for event in watcher.stream(
resource.get,
namespace=namespace,
Expand All @@ -210,6 +222,7 @@ def watch(self, resource, namespace=None, name=None, label_selector=None, field_
allow_watch_bookmarks=allow_watch_bookmarks,
send_initial_events=send_initial_events,
resource_version_match=resource_version_match,
**request_timeout_kwargs,
):
event['object'] = ResourceInstance(resource, event['object'])
yield event
Expand Down
37 changes: 37 additions & 0 deletions kubernetes/dynamic/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from .client import DynamicClient
from .exceptions import NotFoundError
from .resource import Resource


class DynamicClientTest(unittest.TestCase):
Expand Down Expand Up @@ -235,6 +236,42 @@ def get(self, **kwargs):
self.assertEqual(True, watcher.kwargs['send_initial_events'])
self.assertEqual('NotOlderThan', watcher.kwargs['resource_version_match'])

def test_resource_watch_forwards_request_timeout(self):
class FakeWatcher:
def __init__(self):
self.kwargs = None

def stream(self, func, **kwargs):
self.kwargs = kwargs
return iter(())

dynamic = DynamicClient.__new__(DynamicClient)
resource = Resource(
prefix='apis',
api_version='v1',
kind='Pod',
name='pods',
client=dynamic,
)

for request_timeout in (5, (2, 5)):
watcher = FakeWatcher()
list(resource.watch(
watcher=watcher,
timeout=30,
_request_timeout=request_timeout,
))

self.assertEqual(
request_timeout, watcher.kwargs['_request_timeout'])
self.assertEqual(30, watcher.kwargs['timeout_seconds'])

watcher = FakeWatcher()
list(resource.watch(watcher=watcher, timeout=30))

self.assertNotIn('_request_timeout', watcher.kwargs)
self.assertEqual(30, watcher.kwargs['timeout_seconds'])

def test_request_builds_send_initial_events_query_params(self):
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
Expand Down