update service ip based on external ip

This commit is contained in:
2024-10-31 21:24:39 +05:30
parent 363c514f8a
commit 564dc1d9a1

View File

@@ -8,8 +8,9 @@ config.load_incluster_config()
v1 = client.CoreV1Api()
# Configuration
SERVICE_NAME = "YOUR_SERVICE_NAME"
NAMESPACE = "YOUR_NAMESPACE"
SERVICE_NAME = "traefik"
NAMESPACE = "kube-system"
ANNOTATION_KEY = "kube-vip.io/loadbalancerIPs"
def update_service_annotation(external_ip):
@@ -17,10 +18,10 @@ def update_service_annotation(external_ip):
service = v1.read_namespaced_service(SERVICE_NAME, NAMESPACE)
# Check if the annotation needs to be updated
current_annotation = service.metadata.annotations.get("external-ip")
current_annotation = service.metadata.annotations.get(ANNOTATION_KEY)
if current_annotation != external_ip:
# Update the annotation
body = {"metadata": {"annotations": {"external-ip": external_ip}}}
body = {"metadata": {"annotations": {ANNOTATION_KEY: external_ip}}}
v1.patch_namespaced_service(SERVICE_NAME, NAMESPACE, body)
print(f"Updated service {SERVICE_NAME} with new external IP: {external_ip}")