Compare commits
2 Commits
363c514f8a
...
1f9f45baf5
| Author | SHA1 | Date |
|---|---|---|
|
|
1f9f45baf5 | |
|
|
564dc1d9a1 |
|
|
@ -8,8 +8,10 @@ config.load_incluster_config()
|
||||||
v1 = client.CoreV1Api()
|
v1 = client.CoreV1Api()
|
||||||
|
|
||||||
# Configuration
|
# Configuration
|
||||||
SERVICE_NAME = "YOUR_SERVICE_NAME"
|
SERVICE_NAME = "traefik"
|
||||||
NAMESPACE = "YOUR_NAMESPACE"
|
NAMESPACE = "kube-system"
|
||||||
|
ANNOTATION_KEY = "kube-vip.io/loadbalancerIPs"
|
||||||
|
NODE_ANNOTATION_KEY = "svccontroller.k3s.cattle.io/enablelb"
|
||||||
|
|
||||||
|
|
||||||
def update_service_annotation(external_ip):
|
def update_service_annotation(external_ip):
|
||||||
|
|
@ -17,10 +19,10 @@ def update_service_annotation(external_ip):
|
||||||
service = v1.read_namespaced_service(SERVICE_NAME, NAMESPACE)
|
service = v1.read_namespaced_service(SERVICE_NAME, NAMESPACE)
|
||||||
|
|
||||||
# Check if the annotation needs to be updated
|
# 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:
|
if current_annotation != external_ip:
|
||||||
# Update the annotation
|
# 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)
|
v1.patch_namespaced_service(SERVICE_NAME, NAMESPACE, body)
|
||||||
print(f"Updated service {SERVICE_NAME} with new external IP: {external_ip}")
|
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):
|
for event in w.stream(v1.list_node, _request_timeout=60):
|
||||||
node = event["object"]
|
node = event["object"]
|
||||||
node_name = node.metadata.name
|
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
|
# Extract the external IP if it exists
|
||||||
external_ip = None
|
external_ip = None
|
||||||
for address in node.status.addresses:
|
for address in node.status.addresses:
|
||||||
if address.type == "ExternalIP":
|
if address.type == "ExternalIP" and is_gateway == True:
|
||||||
external_ip = address.address
|
external_ip = address.address
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue