diff --git a/Dockerfile b/Dockerfile index d003611..a5e3c6a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ FROM alpine RUN set -x \ && apk add --update --no-cache ucarp -ENV KARP_INTERFACE eth0 +ENV KARP_INTERFACE= ENV KARP_HOST_IP= ENV KARP_VIRTUAL_IP 192.168.100.1 ENV KARP_SUBNET 24 diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 4cd5d8f..9846e60 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -19,9 +19,27 @@ if [ ! -z "${KARP_DEBUG}" ] ; then /usr/sbin/ucarp --help fi +# if no explicit host interface is set, we determine it from the default route +if [ -z "$KARP_INTERFACE" ] ; then + KARP_INTERFACE="$(ip r | awk '/^default / { for(i=1; i<=NF; i++) if ($i == "dev") print $(i+1) }')" + + if [ -z "$KARP_INTERFACE" ]; then + echo "Could not infer host interface" + exit 1 + fi + + echo "Auto host interface: ${KARP_INTERFACE}" +fi + # if no explicit host ip is set, we determine it from the interface if [ -z "${KARP_HOST_IP}" ] ; then KARP_HOST_IP=$(ip addr show ${KARP_INTERFACE} | grep inet | grep -v secondary | grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}" | head -n 1) + + if [ -z "$KARP_HOST_IP" ]; then + echo "Could not infer host ip" + exit 1 + fi + echo "Auto host IP: ${KARP_HOST_IP}" fi