@@ -194,6 +194,41 @@ print(watcher.url)
194194print (watcher.thread_name)
195195```
196196
197+ more flexible watcher:
198+
199+ when use this kind of function, you can deal with the original json- object . And you can give some extra params.
200+
201+ ```python
202+ def deal_watch(* args):
203+ # "jsonObj" must be one of the params in function "deal"
204+ def deal(jsonObj = None ,args = args):
205+ # define your deal process
206+ print (dictToJsonString(jsonObj)[:20 ])
207+
208+ return deal
209+
210+ watcher = client.watchResourceBase(kind = " Pod" , namespace = " default" , handlerFunction = deal_watch())
211+ print (watcher.url)
212+ ```
213+
214+ Advanced usage:
215+
216+ If you want to add more request restrictions, such as " limit" ," continue" ," fieldSelector" and so on, you can simply give them to the python- function- params. For example:
217+
218+ ```python
219+ def deal_watch(* args):
220+ # "jsonObj" must be one of the params in function "deal"
221+ def deal(jsonObj = None ,args = args):
222+ # define your deal process
223+ print (dictToJsonString(jsonObj)[:20 ])
224+
225+ return deal
226+
227+ # set http_params as timeoutSeconds=3 & limit=1
228+ watcher = client.watchResourceBase(kind = " Pod" , namespace = " default" , handlerFunction = deal_watch(),timeoutSeconds = " 3" ,limit = " 1" )
229+ print (watcher.url)
230+ ```
231+
197232** the watcher will be close automatically when the main thead exit . If this is not your aim, you can set the param by `is_daemon=False ` **
198233
199234get how much watcher is running:
@@ -245,16 +280,13 @@ from kubesys.common import goodPrintDict
245280
246281print (goodPrintDict(response_dict))
247282```
248-
249-
250-
251283# # full-example
252284
253285see the result in [run- outputs](/ out.txt)
254286
255287```python
256288from kubesys.client import KubernetesClient
257- from kubesys.common import dictToJsonString, getActiveThreadCount
289+ from kubesys.common import dictToJsonString, getActiveThreadCount,goodPrintDict
258290from kubesys.watch_handler import WatchHandler
259291import time
260292# import kubesys
@@ -290,48 +322,60 @@ def test_CRUD(client):
290322 print (" --test list resources:" )
291323 response_dict,OK ,http_status_code = client.listResources(" Pod" )
292324 # print("response_dict: %s"%(goodPrintDict(response_dict,show_print=False)))
293- # print("is OK: ", OK)
294- # print("HTTP status code: ", http_status_code,"\n")
325+ print (" is OK: " , OK )
326+ print (" HTTP status code: " , http_status_code," \n " )
295327
296328 # test create resources
297329 print (" --test create resources:" )
298330 response_dict,OK ,http_status_code = client.createResource(pod_json)
299331 # print("response_dict: %s"%(goodPrintDict(response_dict,show_print=False)))
300- # print("is OK: ", OK)
301- # print("HTTP status code: ", http_status_code,"\n")
332+ print (" is OK: " , OK )
333+ print (" HTTP status code: " , http_status_code," \n " )
302334
303335 # test get one single Resources
304336 print (" --test get one single Resources" )
305337 response_dict,OK ,http_status_code = client.getResource(kind = " Pod" , namespace = " default" , name = " busybox" )
306338 # print("response_dict: %s"%(goodPrintDict(response_dict,show_print=False)))
307- # print("is OK: ", OK)
308- # print("HTTP status code: ", http_status_code,"\n")
339+ print (" is OK: " , OK )
340+ print (" HTTP status code: " , http_status_code," \n " )
309341
310342 # test delete pod
311343 print (" --test delete pod:" )
312344 response_dict,OK ,http_status_code = client.deleteResource(kind = " Pod" , namespace = " default" , name = " busybox" )
313345 # print("response_dict: %s"%(goodPrintDict(response_dict,show_print=False)))
314- # print("is OK: ", OK)
315- # print("HTTP status code: ", http_status_code,"\n")
346+ print (" is OK: " , OK )
347+ print (" HTTP status code: " , http_status_code," \n " )
316348
317349def test_watcher(client,namespce,kind,name = None ):
318350 print (" --start to watch..." )
319351 # client.watchResource(kind="Pod", namespace="default", name="busybox",watcherhandler=WatchHandler(add_func = lambda json_dict: print("ADDED: ", dictToJsonString(json_dict)), modify_func = lambda json_dict: print("MODIFIED: ", dictToJsonString(json_dict)),delete_func = lambda json_dict: print("DELETED: ", dictToJsonString(json_dict))))
320- watcher = client.watchResource(kind = kind, namespace = namespce, name = name,watcherhandler = WatchHandler(add_func = lambda json_dict : print (kind," -ADDED: " ,dictToJsonString(json_dict)[:20 ]), modify_func = lambda json_dict : print (kind," -MODIFIED: " ,dictToJsonString(json_dict)[:20 ]),delete_func = lambda json_dict : print (kind," -DELETED: " ,dictToJsonString(json_dict)[:20 ])))
352+ watcher = client.watchResource(kind = kind, namespace = namespce, name = name,watcherhandler = WatchHandler(add_func = lambda json_dict : print (kind," ADDED " ,dictToJsonString(json_dict)[:20 ]), modify_func = lambda json_dict : print (kind," MODIFIED " ,dictToJsonString(json_dict)[:20 ]),delete_func = lambda json_dict : print (kind," DELETED " ,dictToJsonString(json_dict)[:20 ])))
353+ print (watcher.url)
354+
355+ def deal_watch(* args):
356+ def tt(jsonObj = None ,args = args):
357+ print (dictToJsonString(jsonObj)[:20 ])
358+
359+ return tt
360+
361+ def test_watcher_base(client,namespce,kind,name = None ,handlerFunction = None ,** kwargs):
362+ print (" --start to watch..." )
363+ # client.watchResource(kind="Pod", namespace="default", name="busybox",watcherhandler=WatchHandler(add_func = lambda json_dict: print("ADDED: ", dictToJsonString(json_dict)), modify_func = lambda json_dict: print("MODIFIED: ", dictToJsonString(json_dict)),delete_func = lambda json_dict: print("DELETED: ", dictToJsonString(json_dict))))
364+ watcher = client.watchResourceBase(kind = kind, namespace = namespce, name = name,handlerFunction = handlerFunction,** kwargs)
321365 print (watcher.url)
322366
323367def main():
324368 url = " "
325369 token = " "
326370
327371 client = KubernetesClient(url = url,token = token)
328- test_watcher(client," default" ," DaemonSet" )
329- test_watcher(client," default" ," Pod" )
330- test_watcher(client," default" ," Service" )
331- test_watcher(client," default" ," Deployment" )
332- test_watcher(client," default" ," APIService" )
372+ test_watcher_base(client," default" ," Pod" ,handlerFunction = deal_watch(),timeoutSeconds = 3 )
333373 test_CRUD(client = client)
334- KubernetesClient.joinWatchers()
374+
375+ print (" current thread count: " ,KubernetesClient.getWatchThreadCount())
376+ time.sleep(7 )
377+ # because of the timeoutSecond=3, watching thread is leave.
378+ print (" current thread count: " ,KubernetesClient.getWatchThreadCount())
335379
336380if __name__ == ' __main__' :
337381 main()
0 commit comments