Compare commits
No commits in common. "1f9f45baf57e8ccf8a528a688650e4329dee9981" and "363c514f8a078256b3048fffecf5c502425577b5" have entirely different histories.
1f9f45baf5
...
363c514f8a
|
|
@ -8,10 +8,8 @@ config.load_incluster_config()
|
||||||
v1 = client.CoreV1Api()
|
v1 = client.CoreV1Api()
|
||||||
|
|
||||||
# Configuration
|
# Configuration
|
||||||
SERVICE_NAME = "traefik"
|
SERVICE_NAME = "YOUR_SERVICE_NAME"
|
||||||
NAMESPACE = "kube-system"
|
NAMESPACE = "YOUR_NAMESPACE"
|
||||||
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):
|
||||||
|
|
@ -19,10 +17,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(ANNOTATION_KEY)
|
current_annotation = service.metadata.annotations.get("external-ip")
|
||||||
if current_annotation != external_ip:
|
if current_annotation != external_ip:
|
||||||
# Update the annotation
|
# 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)
|
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}")
|
||||||
|
|
||||||
|
|
@ -32,13 +30,11 @@ 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" and is_gateway == True:
|
if address.type == "ExternalIP":
|
||||||
external_ip = address.address
|
external_ip = address.address
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue