Skip to content

Commit

Permalink
Fix duplicate route table entries added to route table file
Browse files Browse the repository at this point in the history
Given duplicate route entries can be added because of different
route types, we need to account for this in the route table addition
logic to ensure only a single route table gets added in case multiple
routes from that table exist.

Fixes #11
  • Loading branch information
brakthehack committed Apr 22, 2021
1 parent c910699 commit a7a4b86
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ management_gw_key="network.management_gateway"
workload_gw_key="network.workload_gateway"
frontend_gw_key="network.frontend_gateway"

workload_routes_key="network.workload.routes"

# These are the display names for the nics
management_net_name="management"
workload_net_name="workload"
Expand Down Expand Up @@ -84,6 +86,10 @@ escapeString () {
echo "$escaped"
}

getWorkloadRoutes() {
routes=$(ovf-rpctool get.ovf "${workload_routes_key}")
}

# Persist a string to a file
# Input values:
# - The string to write
Expand Down
18 changes: 14 additions & 4 deletions ansible/roles/vmware/files/var/lib/vmware/routetablectl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,26 @@ function down_routes() {
mv -f "${RT_TABLES_FILE}.tmp" "${RT_TABLES_FILE}"
}

# Adds route tables to the route tables file. Prevents duplicates from being added.
add_route_tables() {
tables=$(grep -E '^\w' "${CONFIG_FILE}" | cut -d, -f1,2 | uniq)
for table in ${tables}; do
IFS=, read -ra line <<< "${table}"
cfg_table_id="${line[0]}"
cfg_table_name="${line[1]}"
echo2 "create new route table id=${cfg_table_id} name=${route_table_name}"
printf '%d\t%s\n' "${cfg_table_id}" "${route_table_name}" >>"${RT_TABLES_FILE}"
done
}

# Enables the custom route tables.
function up_routes() {
# Enabling the custom route tables first requires removing any custom route
# tables.
down_routes

add_route_tables

while IFS= read -r line; do
# Skip empty and commented lines.
if [ -z "${line}" ] || [ "${line::1}" == "#" ]; then
Expand All @@ -147,10 +161,6 @@ function up_routes() {
cfg_dev="$(dev_from_mac "${cfg_mac_addr}")"
route_table_name="${RT_TABLE_NAME_PREFIX}${cfg_table_name}"

# Create a new route table.
echo2 "create new route table id=${cfg_table_id} name=${route_table_name}"
printf '%d\t%s\n' "${cfg_table_id}" "${route_table_name}" >>"${RT_TABLES_FILE}"

if [[ "${cfg_gateway}" == "" ]]; then
cfg_destination=$(python3 -c "import sys; import ipaddress; print(ipaddress.ip_network(sys.argv[1], strict=False))" "${cfg_cidr}")
host="$(echo "${cfg_cidr}" | cut -d/ -f 1)"
Expand Down

0 comments on commit a7a4b86

Please sign in to comment.