Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infer host interface from default route #8

Merged
merged 1 commit into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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) }')"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice awk trick! :)


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

Expand Down