can now hand out multiple ports

This commit is contained in:
Tamás Gérczei 2020-08-04 08:15:13 +02:00
parent 4c37d08014
commit e4a61161a1
Signed by: tgerczei
GPG Key ID: 5B59A7760597B1A1
1 changed files with 10 additions and 3 deletions

13
hap.py
View File

@ -1,9 +1,16 @@
#!/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
import sys, random
SVC_TYPE = 'LoadBalancer'
PORT_COUNT = 1
if len(sys.argv) > 1:
try:
PORT_COUNT = int(sys.argv[-1])
except:
sys.exit('optional positional parameter must be a number (default: 1)')
# load client configuration (kubeconfig)
config.load_kube_config()
@ -21,5 +28,5 @@ used_ports = [svc_port.port for svc_ports in ports_per_svc for svc_port in svc_p
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))
# pick available ports
print(' '.join(map(str, random.sample(available_ports, PORT_COUNT))))