Compare commits

..

No commits in common. "d71f7f1c49017b492cca7eac61455921781536c1" and "f9a27135308e95f2ae0830f3a721fc6f30b069b2" have entirely different histories.

1 changed files with 16 additions and 22 deletions

View File

@ -1,28 +1,23 @@
import asyncio import asyncio
from kubernetes_asyncio import client, config, watch from kubernetes_asyncio import client, config, watch
import os
# Configuration # Configuration
SERVICE_NAME = os.getenv("SERVICE_NAME", "traefik") SERVICE_NAME = "traefik"
SERVICE_NAME_LABEL_PATTERN = os.getenv( SERVICE_NAME_LABEL_PATTERN = "app.kubernetes.io/name=traefik" # Define the service name pattern or label to identify services
"SERVICE_NAME_LABEL_PATTERN", "app.kubernetes.io/name=traefik" NAMESPACE = "kube-system"
) ANNOTATION_KEY = "kube-vip.io/loadbalancerIPs"
NAMESPACE = os.getenv("NAMESPACE", "kube-system") ZERO_GATEWAY_IP = "172.28.10.1"
ANNOTATION_KEY = os.getenv("ANNOTATION_KEY", "kube-vip.io/loadbalancerIPs") NODE_LABEL = "svccontroller.k3s.cattle.io/enablelb=true"
ZERO_GATEWAY_IP = os.getenv("ZERO_GATEWAY_IP", "172.28.10.1")
NODE_LABEL = os.getenv("NODE_LABEL", "svccontroller.k3s.cattle.io/enablelb=true")
SERVICE_REQUEST_TIMEOUT = int(os.getenv("SERVICE_REQUEST_TIMEOUT", 300))
NODE_REQUEST_TIMEOUT = int(os.getenv("NODE_REQUEST_TIMEOUT", 300))
async def update_service_annotation(v1, service_name, external_ips): async def update_service_annotation(v1, service_name, external_ip):
try: try:
# Get the current service object # Get the current service object
service = await v1.read_namespaced_service(service_name, NAMESPACE) service = await 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(ANNOTATION_KEY)
target_annotation = ",".join(external_ips) + "," + ZERO_GATEWAY_IP target_annotation = ",".join({external_ip, ZERO_GATEWAY_IP})
if current_annotation != target_annotation: if current_annotation != target_annotation:
# Update the annotation # Update the annotation
body = {"metadata": {"annotations": {ANNOTATION_KEY: target_annotation}}} body = {"metadata": {"annotations": {ANNOTATION_KEY: target_annotation}}}
@ -44,22 +39,21 @@ async def watch_nodes():
while True: while True:
try: try:
async for event in w.stream( async for event in w.stream(
v1.list_node, v1.list_node, label_selector=NODE_LABEL, _request_timeout=300
label_selector=NODE_LABEL,
_request_timeout=NODE_REQUEST_TIMEOUT,
): ):
node = event["object"] node = event["object"]
node_name = node.metadata.name node_name = node.metadata.name
# Check for external IP # Check for external IP
external_ips = [] external_ip = None
for address in node.status.addresses: for address in node.status.addresses:
if address.type == "ExternalIP": if address.type == "ExternalIP":
external_ips.append(address.address) external_ip = address.address
break
if len(external_ips) > 0: if external_ip:
print( print(
f"Detected external IP {str(external_ips)} for node {node_name}", f"Detected external IP {external_ip} for node {node_name}",
flush=True, flush=True,
) )
@ -70,7 +64,7 @@ async def watch_nodes():
service = await v1.read_namespaced_service(SERVICE_NAME, NAMESPACE) service = await v1.read_namespaced_service(SERVICE_NAME, NAMESPACE)
# for service in services.items: # for service in services.items:
await update_service_annotation( await update_service_annotation(
v1, service.metadata.name, external_ips v1, service.metadata.name, external_ip
) )
except client.exceptions.ApiException as e: except client.exceptions.ApiException as e:
@ -97,7 +91,7 @@ async def watch_services():
v1.list_namespaced_service, v1.list_namespaced_service,
NAMESPACE, NAMESPACE,
label_selector=SERVICE_NAME_LABEL_PATTERN, label_selector=SERVICE_NAME_LABEL_PATTERN,
_request_timeout=SERVICE_REQUEST_TIMEOUT, _request_timeout=300,
): ):
service = event["object"] service = event["object"]
service_name = service.metadata.name service_name = service.metadata.name