Skip to content

fix(sweep): dashboard cancel marks CANCELLED and starts the orchestrator, which terminates the members (#1349) - #1357

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/1349-sweep-cancel-orchestrator
Oct 27, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/1349-sweep-cancel-orchestrator

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Implements the owner decision of 2026-10-26 on #1349.

What changes

Dashboard sweep cancel (lambda/dashboard-api/sweeps.go). It no longer assumes SpawnSweepCrossAccountRole. That role's trust names only SpawnSweepOrchestratorRole, so the assume was always denied and the members ran on until their TTL. terminateSweepInstancesCrossAccount and awsAccountIDRe are deleted. The cancel keeps the existing ownership and terminal-state checks, marks the record CANCELLED with cancel_requested as before, and then invokes spawn-sweep-orchestrator asynchronously (InvocationType: Event, payload {"sweep_id": ...}), the way spawn launch --detach and the scheduler start it. The response says the orchestrator is terminating the instances: instances_terminated is now always 0 and instances_to_terminate is new. If the invoke fails, the cancel still succeeds and the message names spawn cancel <id>.

Sweep orchestrator (lambda/sweep-orchestrator/cancel.go, main.go). A cancelled sweep's live members (pending, running, stopping, stopped) are terminated in every region the sweep has, using the SpawnSweepCrossAccountRole the orchestrator already assumes:

  • At handler entry, before the params download, when the record is CANCELLED or cancel_requested. Before this change, that path exited without doing anything.
  • In both polling loops, when the reload finds the cancel. Before this change, the loops only stopped launching.

Members are found by the spawn:sweep-id and spawn:managed=true tags. Those tags are filtered on and also re-checked on every instance EC2 returns. A member launched after the last record save is therefore still found, and another sweep's instances or untagged ones are never touched. A Spot member's request is cancelled first with pkg/spotrequest.Cancel (#922), best effort. A re-run finds nothing live and writes nothing. The outcome is an UpdateItem (status CANCELLED, cancel_requested, completed_at if it is unset, ADD cancel_terminated), not a put of the orchestrator's copy. saveSweepState is now conditional on the record not being cancelled. Before this, a loop that had read the record before the cancel could put it back to RUNNING with cancel_requested=false. A refused save returns errSweepCancelled, and the next poll picks the cancel up.

How the orchestrator is triggered. It is started asynchronously by spawn launch --detach, spawn resume and scheduler-handler. While running, it polls the record every 10 s and re-invokes itself asynchronously before its 13-minute deadline. A live orchestrator therefore sees a cancel within one poll. Two cases had no invocation that would act on it:

  • A self-reinvocation that starts after the cancel. Its handler exited on CANCELLED; now it terminates.
  • An orchestrator that is no longer running, for example after an invocation errored past Lambda's async retries.

For the second case the dashboard now invokes the orchestrator itself. Only detached sweeps are in spawn-sweep-orchestration (records are written only by launchSweepDetached and scheduler-handler), so those are the only sweeps the dashboard can see or cancel. A foreground sweep's members are launched by the CLI, it has no record, the dashboard returns 404 for it, and nothing here changes for it. Single-region and multi-region detached sweeps both go through cancelSweep.

IAM.

  • setup-dashboard-lambda-role.sh: new StartSweepOrchestrator statement, lambda:InvokeFunction on function:spawn-sweep-orchestrator only (SPAWN_ORCHESTRATOR_FUNCTION_NAME overrides it, the same variable the Lambda reads).
  • setup-sweep-cross-account-role.sh: ec2:CancelSpotInstanceRequests, next to the unconditioned TerminateInstances that is already there. Members' Spot requests are untagged, so the spawn:managed condition would refuse them. Cancelling never terminates anything.
  • The dashboard role scripts held no grant that existed only for the old path. SpawnDashboardLambdaRole never granted sts:AssumeRole, which is why the ledger carried that entry.
  • internal/iamcoverage: the dashboard-api | SpawnDashboardLambdaRole | sts:AssumeRole entry is removed. It also covered cross_account.go's caller-credential assume (Dashboard cross-account EC2 reads have never worked: the role it assumes is not granted #815), which the gate attributed to the Lambda role. That assume is now its own function, callerAssumeRoleProvider, and it is cut from the dashboard-api inventory because it runs as the caller, not as SpawnDashboardLambdaRole. A source test keeps that function to the STS client and the provider, so the cut cannot hide anything else.
  • The two SpawnDashboardCrossAccountReadRole terminate entries are rewritten as permanent, explained decisions (Dashboard: terminate and sweep cancel in a member account have no role that may do them (owner decision) #1349 option 3). Instance terminate behaves as fix(iam): batch-queue results role; dashboard role scripts match the code (#1344) #1352 left it.
  • make cli-iam-actions: the CLI inventory is unchanged (TestCLIActionInventoryIsCurrent passes).

Gates (each watched failing against a revert that compiles)

The existing orchestrator and dashboard tests use in-process fakes, not Substrate, so these do too. The orchestrator gates run the real handler from a JSON payload through seams (loadState, memberClientsFor, recordCancelOutcome). The fake EC2 ignores filters and returns every instance, so the in-code ownership check is what the gate proves. The dashboard gates call handleCancelSweep with an aws.Config whose HTTP transport serves and records every AWS request.

Gate Revert Result
TestCancelledSweepTerminatesItsMembersOnly: members in 2 regions terminated, Spot cancelled first, other sweep / untagged / unmanaged / terminated untouched, outcome 4 recorded handler entry returns nil on CANCELLED (old behaviour) FAIL: terminated ""
same isSweepMember returns true FAIL: i-other-sweep, i-untagged, i-unmanaged, i-west-other terminated
TestCancelReRunIsANoOp always write the outcome FAIL: re-run wrote {sw-a 0}
same drop the live-state filter FAIL: re-run made EC2 calls [cancel-spot terminate]
TestLoopSaveDoesNotClobberACancel no condition on the loop's PutItem FAIL: PutItem carries no condition
TestDashboardSweepCancelAssumesNoRole re-add an sts.AssumeRole of SpawnSweepCrossAccountRole in the cancel FAIL: STS called 1 time (AssumeRole)
TestDashboardSweepCancelInvokesTheOrchestrator remove the invoke FAIL: invoked Lambda 0 times
same invoke before the UpdateItem FAIL: invoked before the record was marked cancelled
TestEveryPrincipalCallIsGranted drop the lambda:InvokeFunction grant FAIL: dashboard-api calls lambda:InvokeFunction, not granted
same drop the callerAssumeRoleProvider cut FAIL: dashboard-api calls sts:AssumeRole, not granted
same drop the CancelSpotInstanceRequests grant FAIL: sweep-orchestrator calls ec2:CancelSpotInstanceRequests, not granted

Live deploys this needs (none done here)

  1. setup-dashboard-lambda-role.sh: re-run in the infra account (966362334030) for the lambda:InvokeFunction grant. Until then, the cancel's invoke is denied. The cancel still marks the sweep and a live orchestrator still acts within one poll, but an orchestrator that is no longer running is not restarted. The message then names spawn cancel <id>.
  2. spawn-dashboard-api Lambda: redeploy (lambda/dashboard-api/deploy.sh).
  3. spawn-sweep-orchestrator Lambda: redeploy (scripts/deploy-sweep-orchestrator.sh). Until then, nothing terminates a cancelled sweep's members.
  4. scripts/setup-sweep-cross-account-role.sh: re-run in each member account that runs detached sweeps (435415984226), for ec2:CancelSpotInstanceRequests. Until then, the Spot cancel is denied and logged, and the terminate still happens. Orchestrator members are one-time Spot requests, which close with their instance.

Order: 3 before 2, so that a dashboard cancel never invokes an orchestrator that exits on CANCELLED. 1 and 4 are independent.

Follow-up

The dashboard web UI (spore-host/web/js/main.js:1573) alerts "Terminated ${data.instances_terminated} instance(s)", which will now say 0. Filed as spore-host/spore-host#613.

Fixes #1349

Checks: make check (all passed, including the build-tags step), make lint (0 issues in every module), semgrep scan --config=auto --error . (clean), go test ./internal/iamcoverage (ok).

@scttfrdmn
scttfrdmn merged commit cd820ed into main Oct 27, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the fix/1349-sweep-cancel-orchestrator branch October 27, 2026 04: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.

Dashboard: terminate and sweep cancel in a member account have no role that may do them (owner decision)

1 participant