Compare commits

..

No commits in common. "1f9f45baf57e8ccf8a528a688650e4329dee9981" and "363c514f8a078256b3048fffecf5c502425577b5" have entirely different histories.

1 changed files with 5 additions and 9 deletions

View File

@ -8,10 +8,8 @@ config.load_incluster_config()
v1 = client.CoreV1Api()
# Configuration
SERVICE_NAME = "traefik"
NAMESPACE = "kube-system"
ANNOTATION_KEY = "kube-vip.io/loadbalancerIPs"
NODE_ANNOTATION_KEY = "svccontroller.k3s.cattle.io/enablelb"
SERVICE_NAME = "YOUR_SERVICE_NAME"
NAMESPACE = "YOUR_NAMESPACE"
def update_service_annotation(external_ip):
@ -19,10 +17,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(ANNOTATION_KEY)
current_annotation = service.metadata.annotations.get("external-ip")
if current_annotation != external_ip:
# Update the annotation
body = {"metadata": {"annotations": {ANNOTATION_KEY: external_ip}}}
body = {"metadata": {"annotations": {"external-ip": external_ip}}}
v1.patch_namespaced_service(SERVICE_NAME, NAMESPACE, body)
print(f"Updated service {SERVICE_NAME} with new external IP: {external_ip}")
@ -32,13 +30,11 @@ 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" and is_gateway == True:
if address.type == "ExternalIP":
external_ip = address.address
break