Accept non-IP as KUBERNETES_SERVICE_HOST

KUBERNETES_SERVICE_HOST environment variable that we use to connect to
Kubernetes API might also be a bare hostname instead of IPv4/IPv6
address. This commit makes sure such a case is supported.

Change-Id: Id7e21317623b4328e6de96ff5a25809622089ed3
This commit is contained in:
Michał Dulko 2020-02-20 16:08:01 +01:00
parent 8782ace4cc
commit 330395590b
1 changed files with 7 additions and 3 deletions

View File

@ -83,9 +83,13 @@ def setup_kubernetes_client():
# K8s Pods.
host = os.environ['KUBERNETES_SERVICE_HOST']
port = os.environ['KUBERNETES_SERVICE_PORT_HTTPS']
addr = ipaddress.ip_address(host)
if addr.version == 6:
host = '[%s]' % host
try:
addr = ipaddress.ip_address(host)
if addr.version == 6:
host = '[%s]' % host
except ValueError:
# It's not an IP addres but a hostname, it's fine, move along.
pass
api_root = "https://%s:%s" % (host, port)
_clients[_KUBERNETES_CLIENT] = k8s_client.K8sClient(api_root)