Skip to content

Commit 76ce2b4

Browse files
committed
feat: ops-scripts to compare kube-ovn with neutron-ovn
1 parent 3837de4 commit 76ce2b4

File tree

2 files changed

+573
-0
lines changed

2 files changed

+573
-0
lines changed
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
# -----------------------------------------------
2+
# _ _
3+
# | | | |
4+
# __ _ ___ _ __ ___ ___| |_ __ _ ___| | __
5+
# / _` |/ _ \ '_ \ / _ \/ __| __/ _` |/ __| |/ /
6+
# | (_| | __/ | | | __/\__ \ || (_| | (__| <
7+
# \__, |\___|_| |_|\___||___/\__\__,_|\___|_|\_\
8+
# __/ | ops scripts
9+
# |___/
10+
# -----------------------------------------------
11+
#!/bin/bash
12+
13+
# SCRIPT FOCUS: Validate Neutron Ports against OVN Logical_Switch_Ports (LSPs).
14+
15+
KO_NBCTL_CMD="kubectl ko nbctl"
16+
17+
# --- Dependency Check ---
18+
19+
if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then
20+
echo "ERROR: DEPENDENCY MISSING: Bash version 4.0 or higher is required for mapfile/readarray support."
21+
echo "Current version: $BASH_VERSION"
22+
exit 2
23+
fi
24+
25+
if ! command -v openstack &> /dev/null; then
26+
echo "ERROR: DEPENDENCY MISSING: 'openstack' client command not found."
27+
echo "Please ensure the OpenStack CLI is installed and configured correctly (source openrc)."
28+
exit 2
29+
fi
30+
31+
if ! command -v kubectl &> /dev/null; then
32+
echo "ERROR: DEPENDENCY MISSING: 'kubectl' command not found."
33+
echo "Please ensure kubectl is installed and in your PATH."
34+
exit 2
35+
fi
36+
37+
if ! command -v awk &> /dev/null; then
38+
echo "ERROR: DEPENDENCY MISSING: 'awk' command not found. This is needed for data parsing."
39+
exit 2
40+
fi
41+
42+
if ! command -v grep &> /dev/null; then
43+
echo "ERROR: DEPENDENCY MISSING: 'grep' command not found. This is needed for filtering and comparison."
44+
exit 2
45+
fi
46+
47+
if ! $KO_NBCTL_CMD show &> /dev/null; then
48+
echo "ERROR: FAILED CONNECTION: Failed to connect to OVN NBDB using '$KO_NBCTL_CMD show'."
49+
echo "Please check your 'kubectl ko' configuration/alias and OVN controller status."
50+
exit 2
51+
fi
52+
# --- End Dependency Check ---
53+
54+
55+
FIX_MODE=false
56+
SCAN_MODE=false
57+
HELP_MODE=false
58+
STALE_FOUND=0
59+
60+
for arg in "$@"; do
61+
case "$arg" in
62+
--scan)
63+
SCAN_MODE=true
64+
;;
65+
--fix)
66+
FIX_MODE=true
67+
SCAN_MODE=true
68+
;;
69+
--help)
70+
HELP_MODE=true
71+
;;
72+
*)
73+
if [[ "$arg" != "--scan" && "$arg" != "--fix" ]]; then
74+
HELP_MODE=true
75+
fi
76+
;;
77+
esac
78+
done
79+
80+
show_help() {
81+
echo "Usage: $0 [OPTION]"
82+
echo ""
83+
echo "Compares standard Neutron Ports (excluding floating IPs) against OVN NBDB Logical_Switch_Ports (LSPs)."
84+
echo "Mapping is done by comparing Neutron Port UUIDs with the OVN LSP 'name' column."
85+
echo ""
86+
echo "Options:"
87+
echo " --scan Execute the comparison and diagnostic scan (read-only)."
88+
echo " --fix Execute the scan AND automatically deletes stale OVN LSPs from the OVN NBDB."
89+
echo " --help Display this help message and exit."
90+
echo ""
91+
echo "Exit Codes for Automation (CronJobs):"
92+
echo " 0: Script completed successfully (Fix mode) OR Scan mode found no stale resources."
93+
echo " 1: Scan mode found stale resources (Signals a cleanup action is required)."
94+
echo " 2: Fatal error during dependency check or OVN DB query."
95+
echo ""
96+
}
97+
98+
if [ "$#" -eq 0 ] || [ "$HELP_MODE" = true ]; then
99+
show_help
100+
exit 0
101+
fi
102+
103+
if [ "$SCAN_MODE" = true ]; then
104+
105+
if [ "$FIX_MODE" = true ]; then
106+
echo "=================================================================================="
107+
echo " WARNING: FIX MODE IS ACTIVE! Stale Logical Switch Ports will be DELETED."
108+
echo "=================================================================================="
109+
echo ""
110+
else
111+
echo "========================================================"
112+
echo " SCAN MODE ACTIVE (Read-Only). Use --fix to apply changes."
113+
echo "========================================================"
114+
echo ""
115+
fi
116+
117+
echo "## 1. Extracting Neutron Port IDs (Excluding Floating IPs)..."
118+
119+
# Get Neutron Port UUIDs, explicitly excluding ports with device_owner=network:floatingip
120+
NEUTRON_ALL_PORTS_DATA=$(openstack port list --long -f value -c ID -c device_owner)
121+
122+
NEUTRON_LSP_IDS=$(
123+
echo "$NEUTRON_ALL_PORTS_DATA" |
124+
grep -v "network:floatingip" |
125+
awk '{print $1}' |
126+
sort
127+
)
128+
129+
NEUTRON_PORT_COUNT=$(echo "$NEUTRON_LSP_IDS" | wc -l)
130+
131+
if [ $NEUTRON_PORT_COUNT -lt 1 ]; then
132+
echo " [INFO] Found 0 Neutron Ports (excluding FIPs). Skipping comparison."
133+
exit 0
134+
fi
135+
echo " -> Found $NEUTRON_PORT_COUNT Neutron Standard Ports (LSPs expected)."
136+
137+
echo ""
138+
139+
NEUTRON_LSP_ID_SET=" "
140+
for id in $NEUTRON_LSP_IDS; do
141+
NEUTRON_LSP_ID_SET="${NEUTRON_LSP_ID_SET}${id} "
142+
done
143+
144+
# --- Logical Switch Port Comparison (Neutron Port UUID <-> OVN LSP Name) ---
145+
echo "## 2. OVN Logical_Switch_Port Comparison"
146+
147+
declare -A LSP_UUIDS_MAP
148+
STALE_LSP_UUIDS=()
149+
150+
# Get LSP UUID and its name (which contains the Neutron Port UUID)
151+
OVN_LSP_DATA=$($KO_NBCTL_CMD --columns=_uuid,name --bare --format=csv find Logical_Switch_Port)
152+
153+
if [ $? -ne 0 ]; then
154+
echo " [FATAL ERROR] Failed to execute 'ovn-nbctl find Logical_Switch_Port' command."
155+
exit 2
156+
fi
157+
158+
# LSP_UUIDS_MAP uses Neutron UUID (from LSP name) as key, OVN LSP UUID as value
159+
eval "$(
160+
echo "$OVN_LSP_DATA" |
161+
awk '
162+
BEGIN { FS="," }
163+
NF==2 {
164+
ovn_uuid = $1
165+
neutron_id = $2
166+
167+
# Check for UUID format to identify Neutron ports
168+
if (neutron_id ~ /[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/) {
169+
# Output a Bash assignment statement for direct eval
170+
print "LSP_UUIDS_MAP[\"" neutron_id "\"]=\"" ovn_uuid "\""
171+
}
172+
}
173+
'
174+
)"
175+
176+
OVN_LSP_COUNT=${#LSP_UUIDS_MAP[@]}
177+
echo " -> Mapped $OVN_LSP_COUNT unique OVN LSPs believed to be Neutron ports."
178+
echo ""
179+
180+
# --- Comparison Report & Exit Code Setting ---
181+
182+
echo "## 3. Comparison Report (Standard Ports)"
183+
echo "----------------------------------------"
184+
185+
echo "### A. Missing Standard Ports (LSPs) in OVN NBDB"
186+
MISSING_PORTS=0
187+
188+
for id in $NEUTRON_LSP_IDS; do
189+
# Check if the Neutron UUID exists as a key in our LSP map
190+
if [[ ! ${LSP_UUIDS_MAP["$id"]} ]]; then
191+
echo " [MISSING] Neutron Port ID (LSP): $id"
192+
MISSING_PORTS=$((MISSING_PORTS + 1))
193+
fi
194+
done
195+
196+
if [ $MISSING_PORTS -eq 0 ]; then
197+
echo " [OK] All $NEUTRON_PORT_COUNT Neutron Standard Ports found as OVN LSPs."
198+
else
199+
echo " [ERROR] Total Missing Standard Ports: $MISSING_PORTS"
200+
fi
201+
echo ""
202+
203+
echo "### B. Stale Logical Switch Ports (LSPs) in OVN NBDB"
204+
STALE_PORTS=0
205+
206+
for neutron_id in "${!LSP_UUIDS_MAP[@]}"; do
207+
ovn_uuid="${LSP_UUIDS_MAP["$neutron_id"]}"
208+
209+
# Check if the Neutron UUID (from OVN LSP name) is NOT in the current Neutron LSP ID set
210+
if ! echo "$NEUTRON_LSP_ID_SET" | grep -q " ${neutron_id} "; then
211+
# If the ID is not in the Neutron set, the LSP is stale
212+
echo " [STALE] Port ID (LSP): $neutron_id (OVN LSP UUID: $ovn_uuid)"
213+
STALE_LSP_UUIDS+=("$ovn_uuid")
214+
STALE_PORTS=$((STALE_PORTS + 1))
215+
fi
216+
done
217+
218+
if [ $STALE_PORTS -eq 0 ]; then
219+
echo " [OK] No stale Neutron-managed LSPs found in OVN NBDB."
220+
else
221+
echo " [CLEANUP NEEDED] Total Stale Standard Ports: $STALE_PORTS"
222+
STALE_FOUND=1
223+
fi
224+
echo ""
225+
226+
# --- Remediation (Fix Mode) ---
227+
228+
if [ "$FIX_MODE" = true ] && [ "$STALE_FOUND" -eq 1 ]; then
229+
echo "## 4. Remediation: Deleting Stale Resources from OVN NBDB"
230+
231+
LSP_CLEANUP_COUNT=0
232+
LSP_TOTAL_TO_CLEANUP=${#STALE_LSP_UUIDS[@]}
233+
for lsp_uuid in "${STALE_LSP_UUIDS[@]}"; do
234+
LSP_CLEANUP_COUNT=$((LSP_CLEANUP_COUNT + 1))
235+
echo " -> [LSP $LSP_CLEANUP_COUNT/$LSP_TOTAL_TO_CLEANUP] Attempting to destroy STALE Logical_Switch_Port UUID: $lsp_uuid"
236+
$KO_NBCTL_CMD destroy Logical_Switch_Port "$lsp_uuid" || echo " -> WARNING: Failed to destroy Logical_Switch_Port $lsp_uuid. May require manual cleanup."
237+
done
238+
239+
echo " [COMPLETE] Remediation attempt finished."
240+
exit 0
241+
elif [ "$FIX_MODE" = true ] && [ "$STALE_FOUND" -eq 0 ]; then
242+
echo "## 4. Remediation"
243+
echo " [SKIP] Fix mode enabled, but no stale resources found to delete."
244+
exit 0
245+
fi
246+
247+
exit "$STALE_FOUND"
248+
249+
fi

0 commit comments

Comments
 (0)