From a54c7fcac07855a41bc1372698e93f8f920b0b39 Mon Sep 17 00:00:00 2001 From: Stefan Hellander Date: Mon, 22 Apr 2024 13:42:12 +0200 Subject: [PATCH] Catch resource too old error. --- event_listener.py | 78 ++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/event_listener.py b/event_listener.py index 6a8f4fa..b48340a 100644 --- a/event_listener.py +++ b/event_listener.py @@ -71,40 +71,50 @@ def sync_all_statuses(): def init_event_listener(): resource_version = "" while True: - for event in w.stream( - api.list_namespaced_pod, - namespace=namespace, - label_selector=label_selector, - timeout_seconds=20, - resource_version=resource_version, - ): - pod = event["object"] - - resource_version = pod.metadata.resource_version - status = get_status(pod) - - logging.info(f"Synchronizing status: {status}") - - # status = pod.status.phase - release = pod.metadata.labels["release"] - - event_type = event["type"] - - if latest_status.get(release) == status: - logging.info("Status not changed, skipping...") - - latest_status[release] = status - - if event_type != "DELETED": - logging.info(f"EVENT_TYPE: {event_type}") - logging.info(f"STATUS: {status}") - - data = { - "release": release, - "status": status, - } - - post(APP_STATUS_URL, data=data) + try: + for event in w.stream( + api.list_namespaced_pod, + namespace=namespace, + label_selector=label_selector, + timeout_seconds=20, + resource_version=resource_version, + ): + pod = event["object"] + + resource_version = pod.metadata.resource_version + status = get_status(pod) + + logging.info(f"Synchronizing status: {status}") + + # status = pod.status.phase + release = pod.metadata.labels["release"] + + event_type = event["type"] + + if latest_status.get(release) == status: + logging.info("Status not changed, skipping...") + + latest_status[release] = status + + if event_type != "DELETED": + logging.info(f"EVENT_TYPE: {event_type}") + logging.info(f"STATUS: {status}") + + data = { + "release": release, + "status": status, + } + + post(APP_STATUS_URL, data=data) + except client.exceptions.ApiException as e: + if e.status == 410: + # If the resource version is too old, log the issue and reset resource_version + logging.error("Resource version expired, restarting watch from latest version.") + resource_version = None + else: + logging.error(f"An error occurred: {e}") + continue + def get_status(pod):