fix: setup proper logging

main
Malar Invention 2025-01-12 16:12:07 +05:30
parent 4b23c6bcd9
commit e72c1e3c1e
1 changed files with 14 additions and 10 deletions

View File

@ -1,6 +1,7 @@
import asyncio import asyncio
from kubernetes_asyncio import client, config, watch from kubernetes_asyncio import client, config, watch
import os import os
import logging
# Configuration # Configuration
ANNOTATION_KEY = os.getenv("ANNOTATION_KEY", "kube-vip.io/loadbalancerIPs") ANNOTATION_KEY = os.getenv("ANNOTATION_KEY", "kube-vip.io/loadbalancerIPs")
@ -11,6 +12,10 @@ SERVICE_LABEL_VALUE = os.getenv("SERVICE_LABEL_VALUE", "true")
SERVICE_REQUEST_TIMEOUT = int(os.getenv("SERVICE_REQUEST_TIMEOUT", 300)) SERVICE_REQUEST_TIMEOUT = int(os.getenv("SERVICE_REQUEST_TIMEOUT", 300))
NODE_REQUEST_TIMEOUT = int(os.getenv("NODE_REQUEST_TIMEOUT", 30)) NODE_REQUEST_TIMEOUT = int(os.getenv("NODE_REQUEST_TIMEOUT", 30))
# Logging configuration
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
async def update_service_annotation(v1, service, external_ips): async def update_service_annotation(v1, service, external_ips):
try: try:
@ -23,13 +28,12 @@ async def update_service_annotation(v1, service, external_ips):
if current_annotation != target_annotation: if current_annotation != target_annotation:
body = {"metadata": {"annotations": {ANNOTATION_KEY: target_annotation}}} body = {"metadata": {"annotations": {ANNOTATION_KEY: target_annotation}}}
await v1.patch_namespaced_service(service_name, namespace, body) await v1.patch_namespaced_service(service_name, namespace, body)
print( logger.info(
f"Updated service {service_name} with new external IP: {target_annotation}", f"Updated service {service_name} with new external IP: {target_annotation}"
flush=True,
) )
except client.exceptions.ApiException as e: except client.exceptions.ApiException as e:
print(f"API Exception in update_service_annotation: {e}", flush=True) logger.error(f"API Exception in update_service_annotation: {e}")
async def watch_nodes(v1, external_ips_update_queue): async def watch_nodes(v1, external_ips_update_queue):
@ -51,13 +55,13 @@ async def watch_nodes(v1, external_ips_update_queue):
await external_ips_update_queue.put(external_ips) await external_ips_update_queue.put(external_ips)
except client.exceptions.ApiException as e: except client.exceptions.ApiException as e:
print(f"API Exception in watch_nodes: {e}", flush=True) logger.error(f"API Exception in watch_nodes: {e}")
await asyncio.sleep(5) await asyncio.sleep(5)
except asyncio.CancelledError: except asyncio.CancelledError:
print("Watch task was cancelled.", flush=True) logger.info("Watch task was cancelled.")
break break
except Exception as e: except Exception as e:
print(f"Unexpected error in watch_nodes: {e}", flush=True) logger.error(f"Unexpected error in watch_nodes: {e}")
await asyncio.sleep(5) await asyncio.sleep(5)
@ -78,13 +82,13 @@ async def watch_services(v1, external_ips_update_queue):
await update_service_annotation(v1, service, external_ips) await update_service_annotation(v1, service, external_ips)
except client.exceptions.ApiException as e: except client.exceptions.ApiException as e:
print(f"API Exception in watch_services: {e}", flush=True) logger.error(f"API Exception in watch_services: {e}")
await asyncio.sleep(5) await asyncio.sleep(5)
except asyncio.CancelledError: except asyncio.CancelledError:
print("Watch task was cancelled.", flush=True) logger.info("Watch task was cancelled.")
break break
except Exception as e: except Exception as e:
print(f"Unexpected error in watch_services: {e}", flush=True) logger.error(f"Unexpected error in watch_services: {e}")
await asyncio.sleep(5) await asyncio.sleep(5)