From 4c37d080146c8661e409ed7d566854dcf9601e74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20G=C3=A9rczei?= Date: Sun, 2 Aug 2020 11:23:16 +0200 Subject: [PATCH] initial prototype --- hap.py | 25 +++++++++++++++++++++++++ requirements.txt | 1 + 2 files changed, 26 insertions(+) create mode 100755 hap.py create mode 100644 requirements.txt diff --git a/hap.py b/hap.py new file mode 100755 index 0000000..158e886 --- /dev/null +++ b/hap.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +# "Have-A-Port" | ephemeral port picker for LoadBalancer-type Kubernetes services in clusters with a single-address external address pool +from kubernetes import client, config +import random + +SVC_TYPE = 'LoadBalancer' + +# load client configuration (kubeconfig) +config.load_kube_config() + +# establish API connection +connection = client.CoreV1Api() +# query service objects +all_services = connection.list_service_for_all_namespaces(watch=False) + +# determine ports in use +ports_per_svc = [ svc.spec.ports for svc in all_services.items if svc.spec.type == SVC_TYPE] +used_ports = [svc_port.port for svc_ports in ports_per_svc for svc_port in svc_ports] + +# determine available ports +full_port_range = (range(49152, 65536)) # https://tools.ietf.org/html/rfc6335#section-8.1.2 +available_ports = list(set(full_port_range) - set(used_ports)) + +# pick an available port +print(random.choice(available_ports)) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..77043e3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +kubernetes==10.0.1