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

Add watch_stop to DynamicClient #243

Merged
merged 1 commit into from
Aug 4, 2021
Merged
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
11 changes: 8 additions & 3 deletions dynamic/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def patch(self, resource, body=None, name=None, namespace=None, **kwargs):

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

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

Expand All @@ -156,6 +156,7 @@ def watch(self, resource, namespace=None, name=None, label_selector=None, field_
: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 watcher: The Watcher object that will be used to stream the resource

:return: Event object with these keys:
'type': The type of event such as "ADDED", "DELETED", etc.
Expand All @@ -164,13 +165,17 @@ def watch(self, resource, namespace=None, name=None, label_selector=None, field_

Example:
client = DynamicClient(k8s_client)
watcher = watch.Watch()
v1_pods = client.resources.get(api_version='v1', kind='Pod')

for e in v1_pods.watch(resource_version=0, namespace=default, timeout=5):
for e in v1_pods.watch(resource_version=0, namespace=default, timeout=5, watcher=watcher):
print(e['type'])
print(e['object'].metadata)
# If you want to gracefully stop the stream watcher
watcher.stop()
"""
watcher = watch.Watch()
if not watcher: watcher = watch.Watch()

for event in watcher.stream(
resource.get,
namespace=namespace,
Expand Down