Fix vm cleanup iptables/ipset misleading error logs (Fixes #12770)#12850
Fix vm cleanup iptables/ipset misleading error logs (Fixes #12770)#12850dheeraj12347 wants to merge 1 commit intoapache:4.22from
Conversation
There was a problem hiding this comment.
Pull request overview
Improves KVM VM security-group cleanup in security_group.py by making iptables/ip6tables chain and ipset removal more idempotent, aiming to reduce misleading ERROR logs when cleanup is re-run and resources are already gone.
Changes:
- Added helper functions to detect existence of iptables/ip6tables chains and ipsets before attempting flush/delete.
- Updated
destroy_network_rules_for_vm()to conditionally flush/delete chains and ipsets, and to log “missing” cases at DEBUG. - Tightened exception handling in DNAT cleanup blocks (catch
Exceptionexplicitly).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| try: | ||
| execute("iptables -S %s 2>/dev/null" % chain) | ||
| return True | ||
| except CalledProcessError as e: | ||
| if e.returncode == 1: | ||
| # Chain not found - normal for idempotent cleanup | ||
| logging.debug("iptables chain %s does not exist", chain) | ||
| return False |
| if ip6tables_chain_exists(chain): | ||
| try: | ||
| execute("ip6tables -F " + chain) | ||
| execute("ip6tables -X " + chain) | ||
| except Exception as e: | ||
| logging.error("Failed to flush/delete ip6tables chain %s: %s", chain, str(e)) | ||
| else: | ||
| logging.debug("ip6tables chain %s does not exist, skipping", chain) |
| for ipset in [vm_ipsetname, vm_ipsetname + '-6']: | ||
| if ipset_exists(ipset): | ||
| try: | ||
| execute('ipset -F ' + ipset) | ||
| execute('ipset -X ' + ipset) | ||
| except Exception as e: | ||
| logging.error("Failed to flush/delete ipset %s: %s", ipset, str(e)) | ||
| else: | ||
| logging.debug("Ipset %s does not exist, skipping", ipset) |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 4.22 #12850 +/- ##
============================================
+ Coverage 17.61% 18.02% +0.41%
- Complexity 15662 16448 +786
============================================
Files 5917 5968 +51
Lines 531415 537101 +5686
Branches 64973 65964 +991
============================================
+ Hits 93588 96799 +3211
- Misses 427271 429382 +2111
- Partials 10556 10920 +364
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Problem
During KVM VM cleanup,
security_group.pytries to flush and delete iptables/ip6tables chains and ipsets unconditionally. If the chain or set has already been removed (for example, when cleanup is rerun or after partial failures), the underlying commands fail, and the agent logs misleading ERROR messages and stack traces, even though there is nothing left to clean up. This makes idempotent cleanup noisy and harder to debug. [web:2]Changes
Added helper functions:
iptables_chain_exists(chain)ip6tables_chain_exists(chain)ipset_exists(setname)These use the existing
execute()wrapper to check whether a chain or ipset exists. Exit code 1 (not found) is treated as a normal condition: it is logged at DEBUG and returnsFalse, while other non‑zero exit codes are still raised as real errors. [file:69]Updated
destroy_network_rules_for_vm()to:Result