Ephemeral port picker for LoadBalancer-type Kubernetes services in clusters with a single-address external address pool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
916 B

#!/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))