My program (which eventually will be containerised) should be able to do the following:
- continuously listening for deployment and daemonsets in kubernetes (2 threads)
- collect info needed (like annotations)
- use the info collected in order to create objects in a remote database
I could use some peer review for the thread part, which looks like this currently:
#first thread to watch for deployments
def watch_deployments():
v1 = client.AppsV1Api()
w = watch.Watch()
last_seen_version = v1.list_deployment_for_all_namespaces().metadata.resource_version
while True:
for item in w.stream(v1.list_deployment_for_all_namespaces, pretty="pretty", resource_version=last_seen_version):
_ = {item["object"].metadata.name:[item["object"].kind, item["object"].metadata.namespace, item["object"].metadata.resource_version, item["object"].metadata.annotations]}
depl_lst.put(_)
def watch_daemonsets():
v1 = client.AppsV1Api()
w = watch.Watch()
last_seen_version = v1.list_daemon_set_for_all_namespaces().metadata.resource_version
while True:
for item in w.stream(v1.list_daemon_set_for_all_namespaces, pretty="pretty", resource_version=last_seen_version):
_ = {item["object"].metadata.name:[item["object"].kind, item["object"].metadata.namespace, item["object"].metadata.resource_version, item["object"].metadata.annotations]}
depl_lst.put(_)
if __name__ == '__main__':
current_obj = {}
depl_lst = Queue()
thread.start_new_thread(watch_deployments, ())
thread.start_new_thread(watch_daemonsets, ())
while True:
for i in range(depl_lst.qsize()):
current_obj = depl_lst.get()
#add object is the function in order to create the item in the remote database, not listed here
add_object()
depl_lst.task_done()
Its the same thing with what is being done here but in this case, the asyncio was being used: https://medium.com/@sebgoa/kubernets-async-watches-b8fa8a7ebfd4