Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions templates/horizon/config/local_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@
# retrieve the IP address, which we will then in turn add to the ALLOWED_HOSTS list.
def get_pod_ip():
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

ipstacks = [socket.AF_INET, socket.AF_INET6]
last_error = None

hostport = (
"{{ .horizonEndpointHost }}",
{{- if .isPublicHTTPS }}
Expand All @@ -73,16 +76,21 @@ def get_pod_ip():
80
{{- end }}
)
try:
s.connect(hostport)
return s.getsockname()[0]
except socket.gaierror:
s.close()
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
s.connect(hostport)
return "[{}]".format(s.getsockname()[0])
finally:
s.close()

for ipstack in ipstacks:
try:
with socket.socket(ipstack, socket.SOCK_DGRAM) as s:
s.settimeout(2)
s.connect((hostport))
ip = s.getsockname()[0]
s.close()
return ip if ipstack == socket.AF_INET else f"[{ip}]"
# Broad exception handling here since we will collect and return
# any exceptions after checking each ipstack.
except Exception as e:
last_error = e

raise RuntimeError(f"Could not determine pod IP: {last_error}")

ALLOWED_HOSTS = [get_pod_ip(), "{{ .horizonEndpointHost }}"]

Expand Down