Skip to content

Commit 3b10d73

Browse files
committed
Use lib-common DownwardAPI to retrieve pod IP
This change uses the `lib-common` env `DownwardAPI` [1] to write the pods IP to the env vars of the pod. We then retrieve this in `local_settings.py` to populate `ALLOWED_HOSTS`. [1] https://github.com/openstack-k8s-operators/lib-common/blob/main/modules/common/env/env.go#L100-L114 Fixes #487 Jira: https://issues.redhat.com/browse/OSPRH-19216 Signed-off-by: Brendan Shephard <bshephar@bne-home.net>
1 parent 0b496dc commit 3b10d73

File tree

2 files changed

+8
-28
lines changed

2 files changed

+8
-28
lines changed

internal/horizon/deployment.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ func getEnvVars(configHash string, enabledServices map[string]string) map[string
191191
envVars["ENABLE_WATCHER"] = env.SetValue(enabledServices["watcher"])
192192
envVars["CONFIG_HASH"] = env.SetValue(configHash)
193193
envVars["UNPACK_THEME"] = env.SetValue("true")
194+
envVars["POD_IP"] = env.DownwardAPI("status.podIP")
194195

195196
return envVars
196197
}

templates/horizon/config/local_settings.py

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,14 @@
5656
# For more information see:
5757
# https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
5858

59-
# get_pod_ip retrieves the pod's primary interface IP address. This is necessary
60-
# due to the dynamic IP addressing of pods. The HealthCheck needs to be able to
61-
# check the specific pod. We can't simply check via the route, since such a check
62-
# could land on any of the replicas. Instead, we need to explicity check the pod
63-
# we're currently running on. Therefore, we need to execute this function to
64-
# retrieve the IP address, which we will then in turn add to the ALLOWED_HOSTS list.
65-
def get_pod_ip():
66-
import socket
67-
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
68-
hostport = (
69-
"{{ .horizonEndpointHost }}",
70-
{{- if .isPublicHTTPS }}
71-
443
72-
{{- else }}
73-
80
74-
{{- end }}
75-
)
76-
try:
77-
s.connect(hostport)
78-
return s.getsockname()[0]
79-
except socket.gaierror:
80-
s.close()
81-
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
82-
s.connect(hostport)
83-
return "[{}]".format(s.getsockname()[0])
84-
finally:
85-
s.close()
8659

87-
ALLOWED_HOSTS = [get_pod_ip(), "{{ .horizonEndpointHost }}"]
60+
# Use the environment variable to set pod_ip. We can then use this for
61+
# ALLOWED_HOSTS to enable liveness and readiness probes.
62+
pod_ip = os.environ.get('POD_IP', '')
63+
if pod_ip and ':' in pod_ip:
64+
pod_ip = f"[{pod_ip}]"
65+
66+
ALLOWED_HOSTS = [pod_ip, "{{ .horizonEndpointHost }}"] if pod_ip else ["{{ .horizonEndpointHost }}"]
8867

8968
USE_X_FORWARDED_HOST = True
9069

0 commit comments

Comments
 (0)