Skip to content

fix(network_filter_ng): ensure rollback for node and pod network filters#1462

Open
cynox-66 wants to merge 2 commits into
krkn-chaos:mainfrom
cynox-66:fix/network-filter-rollback
Open

fix(network_filter_ng): ensure rollback for node and pod network filters#1462
cynox-66 wants to merge 2 commits into
krkn-chaos:mainfrom
cynox-66:fix/network-filter-rollback

Conversation

@cynox-66

Copy link
Copy Markdown
Contributor

Summary

This PR adds exception-safe rollback to the network_filter_ng node and pod filter modules.

It follows the same goal as #1461 by ensuring temporary resources are cleaned up on exception and early-return paths, preventing leaked chaos pods while preserving the existing behaviour of successful executions.

Changes

NodeNetworkFilterModule

  • Add a private _rollback() helper
  • Clean up applied iptables rules before deleting the temporary chaos pod
  • Invoke rollback on exception paths
  • Track successful rule application with a rules_applied flag

PodNetworkFilterModule

  • Add a private _rollback() helper
  • Clean up namespaced iptables rules before deleting the temporary chaos pod
  • Clean up the temporary chaos pod on the no-interface early-return path
  • Invoke rollback on exception paths
  • Track successful rule application with a rules_applied flag

Why rules_applied is necessary

Unlike the tc cleanup used by the network chaos modules, the filter modules remove iptables rules using positional deletion (iptables -D ...).

The rule lists are generated before apply_network_rules() executes. If cleanup were triggered simply because rules had been generated, an exception before rule application could remove unrelated pre-existing firewall rules.

To avoid that, rollback is gated by a dedicated rules_applied flag that is set only after apply_network_rules() completes successfully.

Behaviour

The successful execution path is unchanged.

This PR only changes previously-unhandled cleanup paths:

  • exception before normal cleanup
  • exception after rules have been applied
  • pod no-interface early return

These paths now perform best-effort cleanup of temporary resources while preserving the existing error-handling behaviour.

Tests

This PR introduces comprehensive unit tests for both filter modules, including coverage for:

  • successful execution
  • rollback before rule application
  • rollback after rule application
  • rollback helper behaviour
  • exactly-once cleanup
  • serial and parallel (error_queue) execution
  • pod no-interface early return
  • cleanup failure handling
  • original error preservation

All existing network-related regression tests continue to pass.

NodeNetworkFilterModule and PodNetworkFilterModule left the privileged
chaos pod and any applied iptables rules orphaned on the cluster if an
exception occurred after the pod was deployed (including an interrupted
time.sleep) or, for the pod filter, on the no-interface early return.

Add a private _rollback() helper to each module and track application
state with a rules_applied flag set only after apply_network_rules()
completes. Because iptables cleanup is destructive and not idempotent,
the guard is disarmed immediately before the happy-path cleanup so the
exception handler can never run a second cleanup pass, and cleanup is
never attempted for rules that were only generated but not applied.

Cleanup stays inside the existing try/except so its failures continue to
flow through the error_queue / raise contract unchanged; the exception-
and early-return-path rollbacks are best-effort so a cleanup failure can
never mask the original error or turn the graceful no-interface skip into
a reported scenario failure. Behaviour is identical to before except that
the previously-leaked chaos pod and iptables rules are now cleaned up.

Add unit tests for both modules covering the success, early-return,
exception, cleanup-failure, exactly-once, and parallel/error_queue paths.

Signed-off-by: cynox-66 <devj2311@gmail.com>
@augmentcode

augmentcode Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

PR Risk Analyzer Agent🛡️

👀 Human Input Needed
A pair-review briefing is ready for you.

→ Pair Review Briefing

@augmentcode augmentcode Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep Code Review Agent🐛

Review completed with 2 suggestions.

Fix in Cosmos

Comment thread tests/test_node_network_filter.py Outdated
The exactly-once cleanup docstrings claimed rollback runs in `finally`.
It actually runs in the `except` block; the exactly-once guarantee comes
from disarming rules_applied (set False) before the happy-path clean, not
from finally semantics. Correct the two docstrings to match the code.
Documentation-only: no implementation or test logic changed.

Signed-off-by: cynox-66 <devj2311@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant