From 573f16c5c2261ae3fd95ab7e4bc0001ac651f28c Mon Sep 17 00:00:00 2001 From: Malar Invention Date: Fri, 8 Nov 2024 01:46:22 +0530 Subject: [PATCH] accumulate external ips before updating service --- node_external_ip_controller_async.py | 43 ++++++++++++---------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/node_external_ip_controller_async.py b/node_external_ip_controller_async.py index c888fc3..7d4f80c 100644 --- a/node_external_ip_controller_async.py +++ b/node_external_ip_controller_async.py @@ -12,7 +12,7 @@ ANNOTATION_KEY = os.getenv("ANNOTATION_KEY", "kube-vip.io/loadbalancerIPs") ZERO_GATEWAY_IP = os.getenv("ZERO_GATEWAY_IP", "172.28.10.1") NODE_LABEL = os.getenv("NODE_LABEL", "svccontroller.k3s.cattle.io/enablelb=true") SERVICE_REQUEST_TIMEOUT = int(os.getenv("SERVICE_REQUEST_TIMEOUT", 300)) -NODE_REQUEST_TIMEOUT = int(os.getenv("NODE_REQUEST_TIMEOUT", 300)) +NODE_REQUEST_TIMEOUT = int(os.getenv("NODE_REQUEST_TIMEOUT", 30)) async def update_service_annotation(v1, service_name, external_ips): @@ -43,6 +43,8 @@ async def watch_nodes(): while True: try: + # Check for external IP + external_ips = [] async for event in w.stream( v1.list_node, label_selector=NODE_LABEL, @@ -51,27 +53,22 @@ async def watch_nodes(): node = event["object"] node_name = node.metadata.name - # Check for external IP - external_ips = [] for address in node.status.addresses: if address.type == "ExternalIP": external_ips.append(address.address) + if len(external_ips) > 0: + print( + f"Detected external IP {str(external_ips)} for node {node_name}", + flush=True, + ) - if len(external_ips) > 0: - print( - f"Detected external IP {str(external_ips)} for node {node_name}", - flush=True, - ) - - # Get all services that need to be updated with this external IP - # services = await v1.list_namespaced_service( - # NAMESPACE, label_selector=SERVICE_NAME_PATTERN - # ) - service = await v1.read_namespaced_service(SERVICE_NAME, NAMESPACE) - # for service in services.items: - await update_service_annotation( - v1, service.metadata.name, external_ips - ) + # Get all services that need to be updated with this external IP + # services = await v1.list_namespaced_service( + # NAMESPACE, label_selector=SERVICE_NAME_PATTERN + # ) + service = await v1.read_namespaced_service(SERVICE_NAME, NAMESPACE) + # for service in services.items: + await update_service_annotation(v1, service.metadata.name, external_ips) except client.exceptions.ApiException as e: print(f"API Exception in watch_nodes: {e}", flush=True) @@ -101,19 +98,17 @@ async def watch_services(): ): service = event["object"] service_name = service.metadata.name - if event["type"] == "ADDED": + if event["type"] == "ADDED" and service_name == SERVICE_NAME: print(f"New service detected: {service_name}", flush=True) # Fetch current external IP for nodes that match the label nodes = await v1.list_node(label_selector=NODE_LABEL) + external_ips = [] for node in nodes.items: for address in node.status.addresses: if address.type == "ExternalIP": - external_ip = address.address - await update_service_annotation( - v1, service_name, external_ip - ) - break + external_ips.append(address.address) + await update_service_annotation(v1, service_name, external_ips) except client.exceptions.ApiException as e: print(f"API Exception in watch_services: {e}", flush=True)