From a7a4b86f622859fb491453e1c50a5d6b7ad2fb1f Mon Sep 17 00:00:00 2001 From: Brian Rak Date: Tue, 9 Mar 2021 09:45:17 -0600 Subject: [PATCH] Fix duplicate route table entries added to route table file 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 --- .../files/var/lib/vmware/ovf-to-cloud-init.sh | 6 ++++++ .../files/var/lib/vmware/routetablectl.sh | 18 ++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ansible/roles/cloudinit/files/var/lib/vmware/ovf-to-cloud-init.sh b/ansible/roles/cloudinit/files/var/lib/vmware/ovf-to-cloud-init.sh index 29c5142..bc04988 100644 --- a/ansible/roles/cloudinit/files/var/lib/vmware/ovf-to-cloud-init.sh +++ b/ansible/roles/cloudinit/files/var/lib/vmware/ovf-to-cloud-init.sh @@ -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" @@ -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 diff --git a/ansible/roles/vmware/files/var/lib/vmware/routetablectl.sh b/ansible/roles/vmware/files/var/lib/vmware/routetablectl.sh index 9c73430..89fe5ea 100644 --- a/ansible/roles/vmware/files/var/lib/vmware/routetablectl.sh +++ b/ansible/roles/vmware/files/var/lib/vmware/routetablectl.sh @@ -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 @@ -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)"