Compare commits

...

2 Commits

1 changed files with 9 additions and 5 deletions

View File

@ -8,8 +8,10 @@ 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"
NODE_ANNOTATION_KEY = "svccontroller.k3s.cattle.io/enablelb"
def update_service_annotation(external_ip):
@ -17,10 +19,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}")
@ -30,11 +32,13 @@ def main():
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)}")
# Extract the external IP if it exists
external_ip = None
for address in node.status.addresses:
if address.type == "ExternalIP":
if address.type == "ExternalIP" and is_gateway == True:
external_ip = address.address
break