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

Fix policy_based_routing.sh script on simple-nva module #1226

Merged
merged 10 commits into from
Mar 10, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,35 @@
# limitations under the License.

IF_NAME=$1
IP_LB=$(ip r show table local | grep "$IF_NAME proto 66" | cut -f 2 -d " ")
IF_NUMBER=$(echo $IF_NAME | sed -e s/eth//)

# Sleep while there's no load balancer IP route for this IF
while [ -z $IP_LB ] ; do
sleep 2
IP_LB=$(ip r show table local | grep "$IF_NAME proto 66" | cut -f 2 -d " ")
done
# Check whether there are LB for this network interface
# Curl to forwarded-ips endpoint return indexes zero based (e.g. 0 \n 1) for every LB associated to the IF, "" otherwise
IF_LB=$(curl http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/$IF_NUMBER/forwarded-ips/ -H "Metadata-Flavor: Google" | tr -s '\n' ' ')
IP_LB=$(ip r show table local | grep "$IF_NAME proto 66" | cut -f 2 -d " " | tr -s '\n' ' ')

if [ ! -z "$IF_LB" ] ; then
# Sleep while there's no load balancer IP route for this IF
while [ -z "$IP_LB" ] ; do
sleep 2
IP_LB=$(ip r show table local | grep "$IF_NAME proto 66" | cut -f 2 -d " " | tr -s '\n' ' ')
done
fi

IF_NUMBER=$(echo $IF_NAME | sed -e s/eth//)
IF_GW=$(curl http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/$IF_NUMBER/gateway -H "Metadata-Flavor: Google")
IF_IP=$(curl http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/$IF_NUMBER/ip -H "Metadata-Flavor: Google")
IF_NETMASK=$(curl http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/$IF_NUMBER/subnetmask -H "Metadata-Flavor: Google")
IF_IP_PREFIX=$(/var/run/nva/ipprefix_by_netmask.sh $IF_NETMASK)
grep -qxF "$((200 + $IF_NUMBER)) hc-$IF_NAME" /etc/iproute2/rt_tables || echo "$((200 + $IF_NUMBER)) hc-$IF_NAME" >>/etc/iproute2/rt_tables
ip route add $IF_GW src $IF_IP dev $IF_NAME table hc-$IF_NAME
ip route add default via $IF_GW dev $IF_NAME table hc-$IF_NAME
ip rule add from $IP_LB/32 table hc-$IF_NAME

# Skip route configuration when no LB is associated to the network interface
if [ ! -z "$IF_LB" ] ; then
# Convert whitespace separated list of IPs to array
IPS_LB=($IP_LB)
for IP in "${IPS_LB[@]}"
do
ip rule add from $IP/32 table hc-$IF_NAME
done
fi
LucaPrete marked this conversation as resolved.
Show resolved Hide resolved