Skip to content

Commit 90d7232

Browse files
committed
Fix vm cleanup iptables/ipset misleading error logs (Fixes #12770)
1 parent 27bce46 commit 90d7232

File tree

1 file changed

+69
-21
lines changed

1 file changed

+69
-21
lines changed

scripts/vm/network/security_group.py

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,42 @@ def execute(cmd):
5555
logging.exception('Command exited non-zero: %s', cmd)
5656
raise
5757

58+
def iptables_chain_exists(chain):
59+
"""Check if iptables chain exists."""
60+
try:
61+
execute("iptables -S %s 2>/dev/null" % chain)
62+
return True
63+
except CalledProcessError as e:
64+
if e.returncode == 1:
65+
# Chain not found - normal for idempotent cleanup
66+
logging.debug("iptables chain %s does not exist", chain)
67+
return False
68+
# Other exit codes are real errors
69+
raise
70+
71+
72+
def ip6tables_chain_exists(chain):
73+
"""Check if ip6tables chain exists."""
74+
try:
75+
execute("ip6tables -S %s 2>/dev/null" % chain)
76+
return True
77+
except CalledProcessError as e:
78+
if e.returncode == 1:
79+
logging.debug("ip6tables chain %s does not exist", chain)
80+
return False
81+
raise
82+
83+
84+
def ipset_exists(setname):
85+
"""Check if ipset exists."""
86+
try:
87+
execute("ipset list %s 2>/dev/null" % setname)
88+
return True
89+
except CalledProcessError as e:
90+
if e.returncode == 1:
91+
logging.debug("ipset %s does not exist", setname)
92+
return False
93+
raise
5894

5995
def can_bridge_firewall(privnic):
6096
try:
@@ -195,7 +231,7 @@ def destroy_network_rules_for_vm(vm_name, vif=None):
195231
vmchain = iptables_chain_name(vm_name)
196232
vmchain_egress = egress_chain_name(vm_name)
197233
vmchain_default = None
198-
vm_ipsetname=ipset_chain_name(vm_name)
234+
vm_ipsetname = ipset_chain_name(vm_name)
199235

200236
delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
201237
if 1 in [vm_name.startswith(c) for c in ['r-', 's-', 'v-']]:
@@ -208,42 +244,54 @@ def destroy_network_rules_for_vm(vm_name, vif=None):
208244

209245
chains = [vmchain_default, vmchain, vmchain_egress]
210246
for chain in [_f for _f in chains if _f]:
211-
try:
212-
execute("iptables -F " + chain)
213-
execute('ip6tables -F ' + chain)
214-
except:
215-
logging.debug("Ignoring failure to flush chain: " + chain)
247+
# iptables
248+
if iptables_chain_exists(chain):
249+
try:
250+
execute("iptables -F " + chain)
251+
execute("iptables -X " + chain)
252+
except Exception as e:
253+
logging.error("Failed to flush/delete iptables chain %s: %s", chain, str(e))
254+
else:
255+
logging.debug("iptables chain %s does not exist, skipping", chain)
216256

217-
for chain in [_f for _f in chains if _f]:
218-
try:
219-
execute("iptables -X " + chain)
220-
execute('ip6tables -X ' + chain)
221-
except:
222-
logging.debug("Ignoring failure to delete chain: " + chain)
257+
# ip6tables
258+
if ip6tables_chain_exists(chain):
259+
try:
260+
execute("ip6tables -F " + chain)
261+
execute("ip6tables -X " + chain)
262+
except Exception as e:
263+
logging.error("Failed to flush/delete ip6tables chain %s: %s", chain, str(e))
264+
else:
265+
logging.debug("ip6tables chain %s does not exist, skipping", chain)
223266

224-
try:
225-
for ipset in [vm_ipsetname, vm_ipsetname + '-6']:
226-
execute('ipset -F ' + ipset)
227-
execute('ipset -X ' + ipset)
228-
except:
229-
logging.debug("Ignoring failure to delete ipset " + vmchain)
267+
for ipset in [vm_ipsetname, vm_ipsetname + '-6']:
268+
if ipset_exists(ipset):
269+
try:
270+
execute('ipset -F ' + ipset)
271+
execute('ipset -X ' + ipset)
272+
except Exception as e:
273+
logging.error("Failed to flush/delete ipset %s: %s", ipset, str(e))
274+
else:
275+
logging.debug("Ipset %s does not exist, skipping", ipset)
230276

231277
if vif:
232278
try:
233-
dnats = execute("""iptables -t nat -S | awk '/%s/ { sub(/-A/, "-D", $1) ; print }'""" % vif ).split("\n")
279+
dnats = execute("""iptables -t nat -S | awk '/%s/ { sub(/-A/, "-D", $1) ; print }'""" % vif).split("\n")
234280
for dnat in [_f for _f in dnats if _f]:
235281
try:
236282
execute("iptables -t nat " + dnat)
237-
except:
283+
except Exception:
238284
logging.debug("Ignoring failure to delete dnat: " + dnat)
239-
except:
285+
except Exception:
240286
pass
287+
241288
remove_rule_log_for_vm(vm_name)
242289
remove_secip_log_for_vm(vm_name)
243290

244291
return True
245292

246293

294+
247295
def destroy_ebtables_rules(vm_name, vif):
248296
eb_vm_chain = ebtables_chain_name(vm_name)
249297
delcmd = "ebtables -t nat -L PREROUTING | grep " + eb_vm_chain

0 commit comments

Comments
 (0)