|
| 1 | +--- |
| 2 | +# Sometimes, typically after restarting OVN services, the priorities of entries |
| 3 | +# in the ha_chassis and gateway_chassis tables in the OVN northbound database |
| 4 | +# can become misaligned. This results in broken routing for external (bare |
| 5 | +# metal/SR-IOV) ports. |
| 6 | + |
| 7 | +# This playbook can be used to fix the issue by realigning the priorities of |
| 8 | +# the table entries. It executes a small inline shell script against the |
| 9 | +# OVN northbound database to ensure that, for each router, the HA chassis |
| 10 | +# backing its internal networks is aligned with the chassis currently hosting |
| 11 | +# the router's external gateway interface. |
| 12 | + |
| 13 | +# The playbook can be run as follows: |
| 14 | +# kayobe playbook run $KAYOBE_CONFIG_PATH/ansible/fixes/ovn-fix-chassis-priorities.yml |
| 15 | +# By default this runs in dry-run mode; pass '-e apply=yes' to perform the updates. |
| 16 | + |
| 17 | +# If the 'controllers' group does not align with the group used to deploy the |
| 18 | +# OVN NB DB, this can be overridden by passing the following: |
| 19 | +# '-e ovn_nb_db_group=some_other_group' |
| 20 | + |
| 21 | +- name: Find OVN NB DB Leader |
| 22 | + hosts: "{{ ovn_nb_db_group | default('controllers') }}" |
| 23 | + tasks: |
| 24 | + - name: Find OVN NB DB Leader |
| 25 | + when: kolla_enable_ovn | bool |
| 26 | + block: |
| 27 | + - name: Find the OVN NB DB leader |
| 28 | + ansible.builtin.command: docker exec ovn_nb_db ovn-nbctl get-connection |
| 29 | + changed_when: false |
| 30 | + failed_when: false |
| 31 | + register: ovn_check_result |
| 32 | + check_mode: false |
| 33 | + |
| 34 | + - name: Group hosts by leader/follower role |
| 35 | + ansible.builtin.group_by: |
| 36 | + key: ovn_nb_{{ 'leader' if ovn_check_result.rc == 0 else 'follower' }} |
| 37 | + changed_when: false |
| 38 | + |
| 39 | + - name: Assert one leader exists |
| 40 | + ansible.builtin.assert: |
| 41 | + that: |
| 42 | + - groups['ovn_nb_leader'] | default([]) | length == 1 |
| 43 | + |
| 44 | +- name: Fix OVN chassis priorities |
| 45 | + hosts: ovn_nb_leader |
| 46 | + gather_facts: false |
| 47 | + vars: |
| 48 | + apply_updates: "{{ apply | default(false) | bool }}" |
| 49 | + tasks: |
| 50 | + - name: Realign HA chassis priorities with active gateways |
| 51 | + when: kolla_enable_ovn | bool |
| 52 | + ansible.builtin.shell: | |
| 53 | + docker exec -i ovn_nb_db bash -s <<'EOF' |
| 54 | + set -euo pipefail |
| 55 | +
|
| 56 | + MAX_PRIORITY=32767 |
| 57 | + APPLY="{{ 'yes' if apply_updates else 'no' }}" |
| 58 | +
|
| 59 | + if [ "$APPLY" = "yes" ]; then |
| 60 | + echo "APPLY MODE: Updating OVN HA priorities" |
| 61 | + else |
| 62 | + echo "DRY-RUN MODE: Showing proposed changes only" |
| 63 | + echo "Re-run with -e apply=yes to apply changes" |
| 64 | + fi |
| 65 | + echo "" |
| 66 | +
|
| 67 | + # Get all external gateway ports |
| 68 | + ext_ports=$(ovn-nbctl --data=bare --no-headings --columns=name find logical_router_port 'external_ids:"neutron:is_ext_gw"="True"') |
| 69 | +
|
| 70 | + for ext_port in $ext_ports; do |
| 71 | + # Get router name |
| 72 | + router=$(ovn-nbctl --data=bare --no-headings get logical_router_port "$ext_port" 'external_ids:"neutron:router_name"' | tr -d '"') |
| 73 | +
|
| 74 | + if [ -z "$router" ]; then |
| 75 | + echo "Skipping $ext_port: no router name found" |
| 76 | + continue |
| 77 | + fi |
| 78 | +
|
| 79 | + # Get gateway chassis list (ordered by priority) |
| 80 | + gateway_chassis="" |
| 81 | + gateway_info=$(ovn-nbctl lrp-get-gateway-chassis "$ext_port" 2>/dev/null || true) |
| 82 | +
|
| 83 | + while IFS= read -r line; do |
| 84 | + # Strip prefix |
| 85 | + chassis=$(echo "$line" | awk '{print $1}' | cut -d'_' -f2-) |
| 86 | + gateway_chassis="$gateway_chassis $chassis" |
| 87 | + done <<< "$gateway_info" |
| 88 | +
|
| 89 | + gateway_chassis=${gateway_chassis# } |
| 90 | +
|
| 91 | + if [ -z "$gateway_chassis" ]; then |
| 92 | + echo "Router $router: no gateway chassis configured" |
| 93 | + continue |
| 94 | + fi |
| 95 | +
|
| 96 | + # The first chassis in the list is the active gateway |
| 97 | + active_gateway=$(echo "$gateway_chassis" | awk '{print $1}') |
| 98 | + echo "Router: $router | Port: $ext_port | Active Gateway: $active_gateway" |
| 99 | +
|
| 100 | + # Process all internal ports on this router |
| 101 | + router_ports=$(ovn-nbctl --data=bare --no-headings --columns=name \ |
| 102 | + find logical_router_port "external_ids:\"neutron:router_name\"=\"$router\"") |
| 103 | +
|
| 104 | + for port in $router_ports; do |
| 105 | +
|
| 106 | + # Skip external gateway ports |
| 107 | + is_external=$(ovn-nbctl --data=bare --no-headings get logical_router_port "$port" 'external_ids:"neutron:is_ext_gw"' 2>/dev/null) |
| 108 | + [ "$is_external" = "True" ] && continue |
| 109 | +
|
| 110 | + # Get network name and HA chassis group |
| 111 | + network=$(ovn-nbctl --data=bare --no-headings get logical_router_port "$port" 'external_ids:"neutron:network_name"' 2>/dev/null) |
| 112 | + ha_group=$(ovn-nbctl --data=bare --no-headings --columns=_uuid find ha_chassis_group name="$network") |
| 113 | +
|
| 114 | + if [ -z "$ha_group" ]; then |
| 115 | + echo " Port $port: no HA group found for network '$network'" |
| 116 | + continue |
| 117 | + fi |
| 118 | +
|
| 119 | + echo " Port: $port | Network: $network" |
| 120 | +
|
| 121 | + # Update priorities for each chassis in the HA group |
| 122 | + ha_chassis_list=$(ovn-nbctl --data=bare --no-headings get ha_chassis_group "$ha_group" ha_chassis | tr -d '[],') |
| 123 | +
|
| 124 | + for uuid in $ha_chassis_list; do |
| 125 | + chassis_name=$(ovn-nbctl --data=bare --no-headings get ha_chassis "$uuid" chassis_name) |
| 126 | + current_priority=$(ovn-nbctl --data=bare --no-headings get ha_chassis "$uuid" priority) |
| 127 | +
|
| 128 | + # Calculate desired priority |
| 129 | + desired_priority="" |
| 130 | + index=0 |
| 131 | + for gw in $gateway_chassis; do |
| 132 | + if [ "$chassis_name" = "$gw" ]; then |
| 133 | + desired_priority=$((MAX_PRIORITY - index)) |
| 134 | + break |
| 135 | + fi |
| 136 | + index=$((index + 1)) |
| 137 | + done |
| 138 | + [ -z "$desired_priority" ] && continue |
| 139 | +
|
| 140 | + # Apply or report change |
| 141 | + if [ "$current_priority" -ne "$desired_priority" ]; then |
| 142 | + if [ "$APPLY" = "yes" ]; then |
| 143 | + ovn-nbctl set ha_chassis "$uuid" priority=$desired_priority |
| 144 | + echo " $chassis_name: updated priority $current_priority to $desired_priority" |
| 145 | + else |
| 146 | + echo " $chassis_name: would update priority $current_priority to $desired_priority" |
| 147 | + fi |
| 148 | + else |
| 149 | + echo " $chassis_name: priority $current_priority (no change needed)" |
| 150 | + fi |
| 151 | + done |
| 152 | + done |
| 153 | + echo "" |
| 154 | + done |
| 155 | + EOF |
| 156 | + register: fix_output |
| 157 | + changed_when: apply_updates and ('updated priority' in (fix_output.stdout | default(''))) |
| 158 | + |
| 159 | + - name: Display results |
| 160 | + ansible.builtin.debug: |
| 161 | + msg: "{{ fix_output.stdout }}" |
0 commit comments