Skip to content

feat(reaper): reclaim orphaned security groups and placement groups (#685) - #727

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/685-reap-net-resources
Oct 8, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/685-reap-net-resources

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Closes #685. The 7-day grace per your "I do not want anything long term durable".

What this closes

v0.121.0 fixed the ordering and race bugs and added discovery to spawn orphans/cleanup, but nothing reclaimed these without someone running a command — 19 security groups and 9 placement groups in one region of one account, the oldest three months old.

Seven days with no instance referencing them. The grace is a policy choice, not a technical one: the MPI security group is keyed on spawn-mpi-<job-array-name> and reused by name across runs, so a short grace would force a recreate on every repeat run of a named array. A week makes a weekly re-run free, and the recreate is cheap since #659 already backfills rules onto an existing group.

Three deliberate conservatisms

Membership is asked of EC2, not inferred from spawn's own instances — any member blocks the delete, including one spawn didn't create. Inferring would report a group empty and then fail to delete it.

An untimed resource is skipped, not assumed old. Neither describe call returns a creation time, so spawn's tag is the only source — and an untimed group could be seconds from use by a launch in flight.

DependencyViolation is tolerated, not alarmed on. A group can be referenced by another group's rule or an ENI, which the instance scan can't see. Alarming would fire every cycle.

IAM simulated in both directions

Because #652/#665 were policies IAM accepted and didn't authorize:

context DeleteSecurityGroup / DeletePlacementGroup
spawn:managed=true allowed
spawn:managed=false implicitDeny
no tag implicitDeny
DeleteVpc, DeleteSubnet, iam:DeleteRole implicitDeny — blast radius unchanged

Run against the rendered policy with simulate-custom-policy, so no deploy was needed to prove it.

Three gates caught things I'd have shipped broken

  1. The reaper's policy had none of the four actions — the sweep would have AccessDenied every cycle.
  2. A bidirectional drift gate rejected the grants as unused, because its matcher is ^type ([a-z0-9]+)API and my netResourceAPI has a capital letter. The gate was right that it couldn't see the calls; the matcher now allows camelCase.
  3. A cross-role parity gate caught that only the scan-self policy had them, not the CloudFormation cross-account role — the production configuration. Its own message says it: "a one-sided grant means the feature works in one deployment mode and silently no-ops in the other."

And one I found by hand: the cross-account account{} site uses acctCfg rather than base, so my regex missed it and the sweep would have skipped every assumed-role account while appearing to work on scan-self. netFor is wired at all three sites now.

Interaction with #725, worth knowing

This sweep keys on spawn:managed, and detached sweep rows don't carry it — so it's inert for sweep instances until #726 lands. That's why I did #726 first.

Not done here

Deploying the reaper. The code is tested and the IAM is simulated, but the last two reaper deploys each caused an incident, so that's a separate deliberate step.

@codecov

codecov Bot commented Oct 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…685)

The ordering and race bugs were fixed in v0.121.0 and discovery landed in `spawn
orphans`/`cleanup`, but nothing reclaimed these without someone running a
command: 19 security groups and 9 placement groups in one region of one account,
the oldest three months old.

Seven days with no instance referencing them, per the decision that nothing
should be durable indefinitely. The grace is a policy choice: the MPI security
group is keyed on spawn-mpi-<job-array-name> and reused BY NAME across runs
(CreateOrGetMPISecurityGroup is a get-or-create, and #659 backfills rules onto an
existing one), so a short grace would force a recreate on every repeat run of a
named array. A week makes a weekly re-run free, and the recreate is cheap. Neither
resource bills; the pressure is the per-VPC quota, which bites at launch time.

Three deliberate conservatisms:

  - Membership is asked of EC2, not inferred from spawn's own instances, because
    ANY member blocks the delete — including one spawn did not create. Inferring
    would report a group empty and then fail to delete it.
  - A resource with no creation tag is SKIPPED, not assumed old. Neither describe
    call returns a creation time, so spawn's tag is the only source, and an
    untimed group could be seconds from use by a launch in flight.
  - DependencyViolation is tolerated rather than recorded as an error. A group can
    be referenced by another group's rule or an ENI, which the instance scan
    cannot see; alarming on it would fire every cycle.

The IAM is simulated in BOTH directions rather than merely applied, because the

  spawn:managed=true          DeleteSecurityGroup/DeletePlacementGroup  allowed
  spawn:managed=false         both                                      implicitDeny
  no tag at all               both                                      implicitDeny
  DeleteVpc/DeleteSubnet/iam:DeleteRole                                 implicitDeny

Three gates caught things I would otherwise have shipped broken:

  - The reaper's policy had none of the four actions, so the sweep would have
    AccessDenied every cycle.
  - A bidirectional drift gate rejected the grants as unused, because its
    interface matcher was `^type ([a-z0-9]+)API` and my netResourceAPI has a
    capital. The gate was right that it could not see the calls; the matcher now
    allows camelCase.
  - A cross-role parity gate caught that only the scan-self policy had them, not
    the CloudFormation cross-account role — which is the PRODUCTION
    configuration, where a one-sided grant makes the feature silently no-op.

Also found by hand: the cross-account account{} construction site uses acctCfg
rather than base, so my regex missed it and the sweep would have skipped every
assumed-role account while appearing to work on scan-self. netFor is wired at all
three sites now.

Deploying the reaper is a separate step and not done here.
@scttfrdmn
scttfrdmn force-pushed the fix/685-reap-net-resources branch from 27cc8ce to 5475c2b Compare October 8, 2026 01:56
@scttfrdmn
scttfrdmn merged commit 2197e53 into main Oct 8, 2026
7 checks passed
@scttfrdmn
scttfrdmn deleted the fix/685-reap-net-resources branch October 8, 2026 02:05
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.

Nothing reaps managed MPI security groups or placement groups (19 + 9 orphans found); --mpi creates an SG before validating its own flags

1 participant