34 lines
1.3 KiB
Bash
34 lines
1.3 KiB
Bash
#!/bin/sh
|
|
set -e
|
|
|
|
# Default value for API_SERVER_URL if not provided
|
|
API_SERVER_URL=${API_SERVER_URL:-http://api-server:8888/}
|
|
|
|
# Check if NAMESPACE is set and valid
|
|
if [ -n "$NAMESPACE" ]; then
|
|
# Kubernetes namespace validation
|
|
if echo "$NAMESPACE" | grep -Eq '^[a-z0-9]([-a-z0-9]*[a-z0-9])?$' && [ ${#NAMESPACE} -le 63 ]; then
|
|
# Check if API_SERVER_URL is standard FQDN
|
|
if echo "$API_SERVER_URL" | grep -q '\.svc\.freeleaps\.cluster'; then
|
|
# CHeck if is FQDN but not contains namespace
|
|
if ! echo "$API_SERVER_URL" | grep -q "\\.$NAMESPACE\\.svc\\.freeleaps\\.cluster"; then
|
|
API_SERVER_URL=$(echo "$API_SERVER_URL" | sed -E "s/([a-zA-Z0-9-]+)\.svc\.freeleaps\.cluster/\1.$NAMESPACE.svc.freeleaps.cluster/")
|
|
fi
|
|
else
|
|
# If not FQDN, convert to FQDN
|
|
host_part=$(echo "$API_SERVER_URL" | sed -nE 's!^(https?://)([^:/]+)(.*)$!\2!p')
|
|
if [ -n "$host_part" ]; then
|
|
# Replace host name part to FQDN
|
|
API_SERVER_URL=$(echo "$API_SERVER_URL" | sed -E "s!$host_part!$host_part.$NAMESPACE.svc.freeleaps.cluster!")
|
|
fi
|
|
fi
|
|
else
|
|
echo "ERROR: Invalid Namespace '$NAMESPACE', skip FQDN convertion"
|
|
fi
|
|
fi
|
|
|
|
# Replace the environment variable in the nginx config
|
|
envsubst '${API_SERVER_URL}' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf
|
|
|
|
# Start nginx
|
|
exec nginx -g 'daemon off;' |