Skip to content

Instantly share code, notes, and snippets.

@dosaboy
Last active September 17, 2020 13:16
Show Gist options
  • Save dosaboy/45ed41d0c28d1656357dde40a87056fe to your computer and use it in GitHub Desktop.
Save dosaboy/45ed41d0c28d1656357dde40a87056fe to your computer and use it in GitHub Desktop.
#!/bin/bash -u
# Origin: https://gist.github.com/dosaboy/45ed41d0c28d1656357dde40a87056fe
#
# Authors:
# - edward.hope-morley@canonical.com
#
# Tested on Ubuntu Bionic
#
# Description:
# Discover missing default routes on qrouter namespaces
#
# Adding then removing extraroutes can cause default routes
# to permanently disappear as documented in
# https://docs.openstack.org/api-ref/network/v2/#extra-routes-extension
#
declare -A hits=()
count=0
for ns in `find /var/run/netns/ -name qrouter-\*`; do
((count+=1))
ns=`basename $ns`
readarray -t interfaces<<<`sudo ip netns exec $ns ip -br a| egrep '^qr-'| awk '{print $1}'`
((${#interfaces[@]})) && [ -n "${interfaces[0]}" ] || continue
for iface in ${interfaces[@]}; do
sudo ip netns exec $ns ip route show table all| grep default| grep -q $iface
(($?)) || continue
addr=`sudo ip netns exec $ns ip -4 a s $iface| awk '$1=="inet" {print $2}'`
[ -n "$addr" ] || continue
# ignore interfaces that don't have an ip address
hits[${ns}_$iface]=$addr
done
done
num_hits=${#hits[@]}
echo -n "Checked $count routers and $num_hits found with missing routes"
if ((num_hits==0)); then
echo " - exiting"
exit 0
fi
echo " - proposed solutions will follow"
# __SNAT_SG_IFACE_ADDR__ can be found by doing:
# openstack port list --device-owner network:router_centralized_snat --network <net-uuid> --device <router-uuid>
for hit in ${!hits[@]}; do
ns=${hit%%_*}
iface=${hit##*_}
addr=${hits[$hit]}
table=`sudo ip netns exec $ns python3 -c "import netaddr; print(netaddr.IPNetwork(\"$addr\").value)"`
echo -e "\nsudo ip netns exec $ns ip -4 route replace default via __SNAT_SG_IFACE_ADDR__ dev $iface table $table"
echo "sudo ip netns exec $ns ip rule add prio $table from $addr lookup $table"
done
echo -e "\nDone."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment