Skip to content

Resolution to Issue 3, IPAM and iptable rules' solution#5

Merged
cmyers-mieweb merged 3 commits intomainfrom
cmyers_issue3
Jul 11, 2025
Merged

Resolution to Issue 3, IPAM and iptable rules' solution#5
cmyers-mieweb merged 3 commits intomainfrom
cmyers_issue3

Conversation

@cmyers-mieweb
Copy link
Collaborator

Solution for Issue 3 that does the following.

  1. Copies over a present copy of the nginx port map from the load balancer into the pve1 temp directory
  2. Compiles a pct list of intern-phxdc-pve1 and intern-phxdc-pve2 and records current hostnames of systems.
  3. Checks for containers not present within the pct list and removes iptables rules for hostnames' correlated IP address and ports, freeing them for a new container.
  4. Logs all results to /var/log/prune_iptables.log
    Test results posted below.
    Screenshot 2025-06-30 143435
    image

Additional Considerations

  • Removing the DHCP lease was also considered, however after inspecting the dnsmasq lease file, it appears that dnsmasq already handles this. I am guessing that the permanent reservation will check whether or not the container is still active and reassign the address with more lease time. Otherwise reserve for when that MAC address re-enters the online state.
  • A cronjob still needs to be configured after this script is reviewed.

Solution for Issue 3 that does the following.
Copy link
Contributor

@maxklema maxklema left a comment

Choose a reason for hiding this comment

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

Some hosts that are still in the cluster have their IP rules removed when the script is ran (without the -u flag in pitfall).

image

With the -u flag in pitfall, running the script gives me an unbound variable error:

image

pct list gives the hostnames in the fourth column. Can the hostnames be scraped this way instead of iterating into each CTID's configuration file and scraping the hostname from the hostname field?

image

In addition to removing iptables, the script should also remove the JSON entry for all hostnames that need to be removed in /etc/nginx/port_map.json. After re-reading the code, I was reading the logic backwards. Scraping hostnames from the pct list and then checking if hostnames in the port_map.json are not in that list is the right way to go about this. I read it the other way around, originally.

@maxklema maxklema linked an issue Jul 1, 2025 that may be closed by this pull request
@cmyers-mieweb
Copy link
Collaborator Author

Fixes that have been requested have been implemented in the recent commit. Detailed logging has also been added to illustrate specific steps in the prune process to better show the script's behavior and functions. Verification has also been added to ensure that the port_map.json file is correct and stable to match the exact environment present at the time of script execution. If iptables rules have already been removed, manually, the script will identify this and notify in the logs that it could not locate an iptable rule for the given address tied to the removed container.

prprune4
prprune3
prprune2
prprune1

@cmyers-mieweb cmyers-mieweb requested a review from maxklema July 8, 2025 17:01
Copy link
Contributor

@maxklema maxklema left a comment

Choose a reason for hiding this comment

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

The script is mostly working as expected. Tested and deleted hosts were removed from the JSON port map, and that was copied over correctly to the nginx container.

The only issue is that the iptables for these hosts were not removed. I believe it has to do with the line:

# Capture rules into an array first to avoid subshell issues with 'while read'
mapfile -t RULES_TO_DELETE < <(sudo iptables -t nat -S | grep "$hostname" || true) # Added sudo, || true to prevent pipefail if grep finds nothing

Here, it is filtering by the hostname instead of the IP address. IP table rules do not contain the hostname, so there will never be any results. Changing this should return the expected behavior.

Image Image

@cmyers-mieweb
Copy link
Collaborator Author

Thanks for catching this, this was resolved by changing the conditional variable from hostname > ip with an additional 'w' flag on the grep to ensure we strictly check for the exact IP. See screenshots of runs below.

Container is made using create-container tool
prprune5

Rules are checked to ensure a rule was made.
prprune6

Container is destroyed and script is ran.
prprune7

Log Output
prprune8

@cmyers-mieweb cmyers-mieweb requested a review from maxklema July 10, 2025 21:51
Copy link
Contributor

@maxklema maxklema left a comment

Choose a reason for hiding this comment

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

Tested the script and it successfully pruned iptable rules for no-longer existing containers. Good to merge PR.

@cmyers-mieweb cmyers-mieweb merged commit 0328053 into main Jul 11, 2025
@cmyers-mieweb cmyers-mieweb deleted the cmyers_issue3 branch July 11, 2025 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detect Container Deletion and Update iptables + Port Maps

2 participants