@@ -144,7 +144,7 @@ def patch(self, resource, body=None, name=None, namespace=None, **kwargs):
144
144
145
145
return self .request ('patch' , path , body = body , content_type = content_type , ** kwargs )
146
146
147
- def watch (self , resource , namespace = None , name = None , label_selector = None , field_selector = None , resource_version = None , timeout = None ):
147
+ def watch (self , resource , namespace = None , name = None , label_selector = None , field_selector = None , resource_version = None , timeout = None , watcher = None ):
148
148
"""
149
149
Stream events for a resource from the Kubernetes API
150
150
@@ -156,6 +156,7 @@ def watch(self, resource, namespace=None, name=None, label_selector=None, field_
156
156
:param resource_version: The version with which to filter results. Only events with
157
157
a resource_version greater than this value will be returned
158
158
:param timeout: The amount of time in seconds to wait before terminating the stream
159
+ :param watcher: The Watcher object that will be used to stream the resource
159
160
160
161
:return: Event object with these keys:
161
162
'type': The type of event such as "ADDED", "DELETED", etc.
@@ -164,13 +165,17 @@ def watch(self, resource, namespace=None, name=None, label_selector=None, field_
164
165
165
166
Example:
166
167
client = DynamicClient(k8s_client)
168
+ watcher = watch.Watch()
167
169
v1_pods = client.resources.get(api_version='v1', kind='Pod')
168
170
169
- for e in v1_pods.watch(resource_version=0, namespace=default, timeout=5):
171
+ for e in v1_pods.watch(resource_version=0, namespace=default, timeout=5, watcher=watcher ):
170
172
print(e['type'])
171
173
print(e['object'].metadata)
174
+ # If you want to gracefully stop the stream watcher
175
+ watcher.stop()
172
176
"""
173
- watcher = watch .Watch ()
177
+ if not watcher : watcher = watch .Watch ()
178
+
174
179
for event in watcher .stream (
175
180
resource .get ,
176
181
namespace = namespace ,
0 commit comments