Skip to content

fix: complete job and mark workspace as deleted when no provisioners are available - #18465

Merged
johnstcn merged 9 commits into
mainfrom
cj/delete-orphan-fake-complete-job
Jun 23, 2025
Merged

johnstcn merged 9 commits into
mainfrom
cj/delete-orphan-fake-complete-job

Conversation

@johnstcn

@johnstcn johnstcn commented Jun 19, 2025 •

Copy link
Copy Markdown
Member

Alternate fix for #18080

Modifies wsbuilder to complete the provisioner job and mark the workspace as deleted if it is clear that no provisioner will be able to pick up the delete build.

This has a significant advantage of not deviating too much from the current semantics of POST /api/v2/workspacebuilds. #18460 ends up returning a 204 on orphan delete due to no build being created.

Downside is that we have to duplicate some responsibilities of provisionerdserver in wsbuilder.

There is a slight gotcha to this approach though: if you stop a provisioner and then immediately try to orphan-delete, the job will still be created because of the provisioner heartbeat interval. However you can cancel it and try again.

@johnstcn
johnstcn marked this pull request as ready for review June 20, 2025 10:17
@johnstcn
johnstcn requested a review from Copilot June 20, 2025 10:17

Copilot AI 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.

Pull Request Overview

This PR ensures that when an orphan-delete request finds no active provisioners, the builder marks the provisioner job as completed and the workspace as deleted. It also adds the UI hook for orphan deletion, updates in-memory DB behavior, and expands tests to cover the new orphan scenarios.

  • Add canOrphan flag and checkbox in the workspace delete dialog
  • Implement orphan-delete fallback in wsbuilder (complete job + delete workspace)
  • Expand unit and integration tests to cover orphan-delete paths

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
site/src/modules/workspaces/WorkspaceMoreActions/WorkspaceDeleteDialog.tsx Extract canOrphan and render orphan checkbox with updated text
coderd/wsbuilder/wsbuilder.go Orphan-delete logic: complete provisioner job and mark workspace deleted when no provisioners available
coderd/wsbuilder/wsbuilder_test.go New tests for orphan-delete with and without available provisioners
coderd/workspacebuilds.go Use qpr variable to pass provisioner job info into conversion
coderd/workspacebuilds_test.go Import dbfake and add subtests for orphan build errors and permissions
coderd/database/dbmem/dbmem.go Return empty slice instead of sql.ErrNoRows for no provisioner daemons
cli/delete_test.go Update CLI delete test to cover --orphan behavior and status codes
Comments suppressed due to low confidence (2)

site/src/modules/workspaces/WorkspaceMoreActions/WorkspaceDeleteDialog.tsx:53

  • [nitpick] Add frontend tests to verify that the "Orphan Resources" checkbox appears only when canOrphan is true and is hidden otherwise.
	const canOrphan =

coderd/wsbuilder/wsbuilder.go:495

  • Implement or remove the TODO for audit baggage so that delete transitions are properly logged in the audit system.
			// TODO: audit baggage?

Comment thread coderd/workspacebuilds.go Resolved
Comment thread coderd/database/dbmem/dbmem.go Resolved
Comment on lines -374 to -378
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
first := coderdtest.CreateFirstUser(t, client)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

review: refactored to use dbfake instead

Comment on lines -407 to -409
_, err = client.Workspace(ctx, workspace.ID)
require.Error(t, err)
require.Equal(t, http.StatusGone, coderdtest.SDKError(t, err).StatusCode())

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

review: moved this check to the relevant CLI test

@johnstcn
johnstcn requested review from dannykopping and mtojek June 20, 2025 12:04

@dannykopping dannykopping left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A few nits but LGTM

Comment thread coderd/wsbuilder/wsbuilder.go Resolved
Comment thread coderd/wsbuilder/wsbuilder.go Resolved
Comment thread coderd/wsbuilder/wsbuilder_test.go Resolved
Comment thread coderd/wsbuilder/wsbuilder_test.go Resolved

@mtojek mtojek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

@johnstcn johnstcn changed the title fix: wsbuilder: complete job and mark workspace deleted if no provisioners available fix: complete job and mark workspace as deleted when no provisioners are available Jun 23, 2025
@@ -484,7 +486,7 @@ func (b *Builder) buildTx(authFunc func(action policy.Action, object rbac.Object
// nolint: gocritic // At this moment, we are pretending to be provisionerd.
if err := store.UpdateProvisionerJobWithCompleteWithStartedAtByID(dbauthz.AsProvisionerd(b.ctx), database.UpdateProvisionerJobWithCompleteWithStartedAtByIDParams{

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we please log that this has occurred?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Logging in the API handler where we do the auditing.

@johnstcn
johnstcn merged commit 2f55e29 into main Jun 23, 2025
@johnstcn
johnstcn deleted the cj/delete-orphan-fake-complete-job branch June 23, 2025 13:07
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 23, 2025
@johnstcn

Copy link
Copy Markdown
Member Author

Follow-up issue: coder/internal#740

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants