initial commit

main
Malar Invention 2024-10-31 20:58:20 +05:30
commit 363c514f8a
3 changed files with 76 additions and 0 deletions

15
Makefile Normal file
View File

@ -0,0 +1,15 @@
# .PHONY:
# build
build:
docker buildx build --platform=linux/amd64,linux/arm64 -t code.whiteblossom.xyz/infra/node-external-ip-controller:latest --push -f node-external-ip-controller.Dockerfile .
push:
docker push code.whiteblossom.xyz/infra/node-external-ip-controller:latest
builder:
docker buildx create \
--name container-builder \
--driver docker-container \
--bootstrap --use

View File

@ -0,0 +1,14 @@
# Use Python base image
FROM python:3.13-alpine3.19
# Install the Kubernetes Python client
RUN pip install kubernetes
# Copy the controller script into the container
COPY node_external_ip_controller.py /app/node_external_ip_controller.py
# Set the working directory
WORKDIR /app
# Set the command to run the controller script
CMD ["python", "node_external_ip_controller.py"]

View File

@ -0,0 +1,47 @@
from kubernetes import client, config, watch
import os
# Load in-cluster config
config.load_incluster_config()
# Set up Kubernetes API client
v1 = client.CoreV1Api()
# Configuration
SERVICE_NAME = "YOUR_SERVICE_NAME"
NAMESPACE = "YOUR_NAMESPACE"
def update_service_annotation(external_ip):
# Get the current service object
service = v1.read_namespaced_service(SERVICE_NAME, NAMESPACE)
# Check if the annotation needs to be updated
current_annotation = service.metadata.annotations.get("external-ip")
if current_annotation != external_ip:
# Update the annotation
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}")
def main():
w = watch.Watch()
for event in w.stream(v1.list_node, _request_timeout=60):
node = event["object"]
node_name = node.metadata.name
# Extract the external IP if it exists
external_ip = None
for address in node.status.addresses:
if address.type == "ExternalIP":
external_ip = address.address
break
if external_ip:
print(f"Detected external IP {external_ip} for node {node_name}")
update_service_annotation(external_ip)
if __name__ == "__main__":
main()