From dfb89f160878c1ee0c0d057bb02711c2643620fe Mon Sep 17 00:00:00 2001 From: Malar Invention Date: Thu, 31 Oct 2024 22:51:32 +0530 Subject: [PATCH] retry connection if timeout is reached --- node_external_ip_controller.py | 39 ++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/node_external_ip_controller.py b/node_external_ip_controller.py index 522333d..7e53b20 100644 --- a/node_external_ip_controller.py +++ b/node_external_ip_controller.py @@ -29,22 +29,33 @@ def update_service_annotation(external_ip): def main(): w = watch.Watch() - for event in w.stream(v1.list_node, _request_timeout=60): - node = event["object"] - node_name = node.metadata.name - is_gateway = node.metadata.annotations.get(NODE_ANNOTATION_KEY) - print(f"{node_name} is gateway?: {is_gateway}, type:{type(is_gateway)}") + while True: + try: + for event in w.stream(v1.list_node, _request_timeout=300): + node = event["object"] + node_name = node.metadata.name + print(f"Node annotations: {node.metadata.annotations}") + is_gateway = node.metadata.annotations.get(NODE_ANNOTATION_KEY, False) + print(f"{node_name} is gateway?: {is_gateway}, type:{type(is_gateway)}") - # Extract the external IP if it exists - external_ip = None - for address in node.status.addresses: - if address.type == "ExternalIP" and is_gateway == True: - external_ip = address.address - break + # Extract the external IP if it exists + external_ip = None + for address in node.status.addresses: + if address.type == "ExternalIP" and is_gateway == True: + external_ip = address.address + break - if external_ip: - print(f"Detected external IP {external_ip} for node {node_name}") - update_service_annotation(external_ip) + if external_ip: + print(f"Detected external IP {external_ip} for node {node_name}") + update_service_annotation(external_ip) + + except client.exceptions.ApiException as e: + print(f"API Exception: {e}") + time.sleep(5) # Wait before retrying + + except Exception as e: + print(f"Unexpected error: {e}") + time.sleep(5) if __name__ == "__main__":