Compare commits

..

No commits in common. "87869abf8b5c7691a0fe9b8e1bf3d2c99aed34ea" and "3e89eaaa26c76ef13d933132f0b57be38a339c5b" have entirely different histories.

3 changed files with 21 additions and 35 deletions

View File

@ -1,4 +0,0 @@
gateway_services:
- name: traefik-tcp
namespace: kube-system
label_pattern: app.kubernetes.io/name=traefik

View File

@ -2,11 +2,10 @@
FROM python:3.13-alpine3.19
# Install the Kubernetes Python client
RUN pip install kubernetes kubernetes_asyncio PyYAML
RUN pip install kubernetes kubernetes_asyncio
# Copy the controller script into the container
COPY node_external_ip_controller_async.py /app/node_external_ip_controller.py
COPY gateway_services.yaml /app/gateway_services.yaml
# Set the working directory
WORKDIR /app

View File

@ -3,13 +3,11 @@ from kubernetes_asyncio import client, config, watch
import os
# Configuration
import yaml
GATEWAY_SERVICES_FILE = os.getenv("GATEWAY_SERVICES_FILE")
with open(GATEWAY_SERVICES_FILE, "r") as f:
gateway_services = yaml.safe_load(f)
services = gateway_services["gateway_services"]
SERVICE_NAME = os.getenv("SERVICE_NAME", "traefik-tcp")
SERVICE_NAME_LABEL_PATTERN = os.getenv(
"SERVICE_NAME_LABEL_PATTERN", "app.kubernetes.io/name=traefik"
)
NAMESPACE = os.getenv("NAMESPACE", "kube-system")
ANNOTATION_KEY = os.getenv("ANNOTATION_KEY", "kube-vip.io/loadbalancerIPs")
ZERO_GATEWAY_IP = os.getenv("ZERO_GATEWAY_IP", "172.28.10.1")
NODE_LABEL = os.getenv("NODE_LABEL", "svccontroller.k3s.cattle.io/enablelb=true")
@ -17,20 +15,18 @@ SERVICE_REQUEST_TIMEOUT = int(os.getenv("SERVICE_REQUEST_TIMEOUT", 300))
NODE_REQUEST_TIMEOUT = int(os.getenv("NODE_REQUEST_TIMEOUT", 30))
async def update_service_annotation(v1, service, external_ips):
async def update_service_annotation(v1, service_name, external_ips):
try:
# Get the current service object
service_name = service["name"]
namespace = service["namespace"]
service_obj = 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
current_annotation = service_obj.metadata.annotations.get(ANNOTATION_KEY)
current_annotation = service.metadata.annotations.get(ANNOTATION_KEY)
target_annotation = ",".join(external_ips) + "," + ZERO_GATEWAY_IP
if current_annotation != target_annotation:
# Update the annotation
body = {"metadata": {"annotations": {ANNOTATION_KEY: target_annotation}}}
await v1.patch_namespaced_service(service_name, namespace, body)
await v1.patch_namespaced_service(service_name, NAMESPACE, body)
print(
f"Updated service {service_name} with new external IP: {target_annotation}",
flush=True,
@ -66,8 +62,8 @@ async def watch_nodes():
f"Detected external IPs {str(external_ips)} for node {str(node_names)}",
flush=True,
)
for service in services:
await update_service_annotation(v1, service, external_ips)
service = await v1.read_namespaced_service(SERVICE_NAME, NAMESPACE)
await update_service_annotation(v1, service.metadata.name, external_ips)
except client.exceptions.ApiException as e:
print(f"API Exception in watch_nodes: {e}", flush=True)
@ -82,27 +78,22 @@ async def watch_nodes():
await asyncio.sleep(5)
async def watch_service(service):
async def watch_services():
config.load_incluster_config()
v1 = client.CoreV1Api()
w = watch.Watch()
service_name = service["name"]
namespace = service["namespace"]
label_selector = service["label_pattern"]
while True:
try:
async for event in w.stream(
v1.list_namespaced_service,
namespace,
label_selector=label_selector,
NAMESPACE,
label_selector=SERVICE_NAME_LABEL_PATTERN,
_request_timeout=SERVICE_REQUEST_TIMEOUT,
):
service_obj = event["object"]
if (
event["type"] == "ADDED"
and service_obj.metadata.name == service_name
):
service = event["object"]
service_name = service.metadata.name
if event["type"] == "ADDED" and service_name == SERVICE_NAME:
print(f"New service detected: {service_name}", flush=True)
# Fetch current external IP for nodes that match the label
@ -112,7 +103,7 @@ async def watch_service(service):
for address in node.status.addresses:
if address.type == "ExternalIP":
external_ips.append(address.address)
await update_service_annotation(v1, service, external_ips)
await update_service_annotation(v1, service_name, external_ips)
except client.exceptions.ApiException as e:
print(f"API Exception in watch_services: {e}", flush=True)
@ -128,9 +119,9 @@ async def watch_service(service):
async def main():
# Run watch_nodes and watch_services concurrently
service_watchers = [watch_service(service) for service in services]
await asyncio.gather(watch_nodes(), *service_watchers)
await asyncio.gather(watch_nodes(), watch_services())
if __name__ == "__main__":