Browse Source

initial prototype

master
Tamás Gérczei 2 months ago
commit
4c37d08014
Signed by: tgerczei GPG Key ID: 5B59A7760597B1A1
2 changed files with 26 additions and 0 deletions
  1. +25
    -0
      hap.py
  2. +1
    -0
      requirements.txt

+ 25
- 0
hap.py View File

@@ -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))

+ 1
- 0
requirements.txt View File

@@ -0,0 +1 @@
kubernetes==10.0.1

Loading…
Cancel
Save