From b086079736f889e419c02a129380fe74079c4aa7 Mon Sep 17 00:00:00 2001 From: Giles Knap Date: Fri, 6 Sep 2024 16:49:04 +0100 Subject: [PATCH] implement launch_gateway.py --- Dockerfile | 3 +- set_addr_list.py => launch_gateway.py | 45 ++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) rename set_addr_list.py => launch_gateway.py (76%) diff --git a/Dockerfile b/Dockerfile index c3cd9e6..f5ff6ef 100644 --- a/Dockerfile +++ b/Dockerfile @@ -33,9 +33,10 @@ RUN apt update && apt install -y net-tools tcpdump iproute2 iputils-ping vim # install python libraries for set_addr_list.py RUN pip3 install setuptools scapy kubernetes ipython +COPY launch_gateway.py /launch_gateway.py ENTRYPOINT ["bash"] -CMD ["-c", "/epics/ca-gateway/bin/linux_x86-64/gateway"] +CMD ["-c", "/epics/ca-gateway/bin/linux-x86_64/gateway"] ## ====================================== # 3rd stage: "dockerize" the application - copy executable, lib dependencies diff --git a/set_addr_list.py b/launch_gateway.py similarity index 76% rename from set_addr_list.py rename to launch_gateway.py index 9ea9f60..4549d44 100644 --- a/set_addr_list.py +++ b/launch_gateway.py @@ -5,6 +5,9 @@ import queue import threading import time +import os +import subprocess + from collections import namedtuple from typing import Generator, List, Set, Tuple @@ -106,6 +109,22 @@ def handle_events(eventq: 'queue.Queue[ServiceEvent]', else: log.error("Incorrect service event type: %s", event.type) +def get_ioc_ips(v1: client.CoreV1Api, namespace: str): + """Get the list cluster IPs of IOCs running in a namespace + + Args: + v1: kubernetes client + namespace: namespace to get the IOCs from + """ + ips = set() + ret = v1.list_namespaced_pod(namespace) + for pod in ret.items: + if "is_ioc" in pod.metadata.labels: + ips.add(pod.status.pod_ip) + + return ips + + def parse_args(): parser = argparse.ArgumentParser() @@ -119,11 +138,27 @@ def main(): args = parse_args() search_endpoints = set() logging.basicConfig(level=level2num.get(args.loglevel.lower(), "info")) - eventq = queue.Queue() - threading.Thread(None, services_events_task, "services_events", - args=(args.namespace, args.port, eventq)).start() - while True: - handle_events(eventq, search_endpoints) + + # configure K8S and make a Core API client + config.load_incluster_config() + v1 = client.CoreV1Api() + + namespace = os.getenv("MY_POD_NAMESPACE", "default") + port = os.getenv("GATEWAY_SERVER_PORT", 5064) + + ips = get_ioc_ips(v1, namespace) + ipstr = ",".join(ips) + + command = f"/epics/ca-gateway/bin/linux-x86_64/gateway -sport {port} -cip {ipstr} -pvlist /config/pvlist -access /config/access -log /dev/stdout -debug 1" + + print(f"Running command: {command}") + subprocess.run(["bash", "-c", command], check=True) + # eventq = queue.Queue() + # threading.Thread(None, services_events_task, "services_events", + # args=(args.namespace, args.port, eventq)).start() + + # while True: + # handle_events(eventq, search_endpoints) if __name__ == "__main__":