- Notifications
You must be signed in to change notification settings - Fork907
feat: implement claiming of prebuilt workspaces#17458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
38 commits Select commitHold shift + click to select a range
8b9e30d
feat: implement claiming of prebuilt workspaces
evgeniy-scherbinad9497df
Merge branch 'main' of github.com:/coder/coder into yevhenii/512-clai…
dannykopping217e46f
Reverting unncessary changes
dannykoppingc459533
Refactoring
dannykoppingf905219
Removing unnecessary field
dannykopping8266338
make fmt
dannykopping4098ed7
CR's fixes
evgeniy-scherbina50d23e4
CR's fixes
evgeniy-scherbina546b7ae
Update coderd/workspaces.go
evgeniy-scherbina85f7926
CR's fixes
evgeniy-scherbinaee9908c
CR's fixes
evgeniy-scherbina70bf179
CR's fixes
evgeniy-scherbina7a72d03
CR's fixes
evgeniy-scherbinab246589
fix tests by updating noop.go
evgeniy-scherbinaff8d3de
Merge branch 'main' of github.com:/coder/coder into yevhenii/512-clai…
dannykoppingfcdbba8
test: add failure testcase scenario
evgeniy-scherbinae7b7f80
refactor: revert interface changes
evgeniy-scherbinae7455db
refactor: simplify signature of claim method
evgeniy-scherbina077d7f0
refactor: CR's fixes
evgeniy-scherbina0d426c7
Merge remote-tracking branch 'origin/main' into yevhenii/512-claim-pr…
evgeniy-scherbina663a8c0
refactor: CR's fixes
evgeniy-scherbinabc31fac
refactor: make lint
evgeniy-scherbina087bd20
refactor: fix linter
evgeniy-scherbina9f66169
refactor: remove PrebuildsOrchestrator context from claim tests
evgeniy-scherbina7f25f24
refactor: don't fail test in a goroutine
evgeniy-scherbinaff9eeb4
docs: fix ssh coder example in testing-templates doc (#17550)
EdwardAngert31b873f
fix: add websocket close handling (#17548)
jaaydenh4fe770d
ci: move go install tools to separate action (#17552)
ethanndickson8d8fc99
chore(dogfood): allow provider minor version updates (#17554)
matifaliaf32325
fix(examples/templates/docker-devcontainer): update folder path and p…
Aericio0826e8a
feat: enable masking password inputs instead of blocking echo (#17469)
ibetitsmikecbb6c12
fix(examples/templates/kubernetes-devcontainer): update coder provide…
matifalie466844
docs: add automatic release calendar updates in docs (#17531)
matifalif24557b
chore(dogfood): add windsurf module (#17558)
matifalifa65564
Merge branch 'main' of github.com:/coder/coder into yevhenii/512-clai…
dannykopping53b38d7
fix: use userCtx instead of prebuildCtx for claiming prebuild
evgeniy-scherbina6c41e03
refactor: CR's fixes
evgeniy-scherbina9173e9a
CR's fixes
evgeniy-scherbinaFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletionscoderd/coderd.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletionscoderd/prebuilds/api.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletionscoderd/prebuilds/noop.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
9 changes: 5 additions & 4 deletionscoderd/provisionerdserver/provisionerdserver.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
90 changes: 69 additions & 21 deletionscoderd/workspaces.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -18,6 +18,7 @@ import ( | ||
"golang.org/x/xerrors" | ||
"cdr.dev/slog" | ||
"github.com/coder/coder/v2/agent/proto" | ||
"github.com/coder/coder/v2/coderd/audit" | ||
"github.com/coder/coder/v2/coderd/database" | ||
@@ -28,6 +29,7 @@ import ( | ||
"github.com/coder/coder/v2/coderd/httpapi" | ||
"github.com/coder/coder/v2/coderd/httpmw" | ||
"github.com/coder/coder/v2/coderd/notifications" | ||
"github.com/coder/coder/v2/coderd/prebuilds" | ||
"github.com/coder/coder/v2/coderd/rbac" | ||
"github.com/coder/coder/v2/coderd/rbac/policy" | ||
"github.com/coder/coder/v2/coderd/schedule" | ||
@@ -636,33 +638,57 @@ func createWorkspace( | ||
workspaceBuild *database.WorkspaceBuild | ||
provisionerDaemons []database.GetEligibleProvisionerDaemonsByProvisionerJobIDsRow | ||
) | ||
err = api.Database.InTx(func(db database.Store) error { | ||
var ( | ||
workspaceID uuid.UUID | ||
claimedWorkspace *database.Workspace | ||
prebuildsClaimer = *api.PrebuildsClaimer.Load() | ||
) | ||
// If a template preset was chosen, try claim a prebuilt workspace. | ||
if req.TemplateVersionPresetID != uuid.Nil { | ||
// Try and claim an eligible prebuild, if available. | ||
claimedWorkspace, err = claimPrebuild(ctx, prebuildsClaimer, db, api.Logger, req, owner) | ||
if err != nil && !errors.Is(err, prebuilds.ErrNoClaimablePrebuiltWorkspaces) { | ||
return xerrors.Errorf("claim prebuild: %w", err) | ||
} | ||
} | ||
// No prebuild found; regular flow. | ||
if claimedWorkspace == nil { | ||
now := dbtime.Now() | ||
// Workspaces are created without any versions. | ||
minimumWorkspace, err := db.InsertWorkspace(ctx, database.InsertWorkspaceParams{ | ||
ID: uuid.New(), | ||
CreatedAt: now, | ||
UpdatedAt: now, | ||
OwnerID: owner.ID, | ||
OrganizationID: template.OrganizationID, | ||
TemplateID: template.ID, | ||
Name: req.Name, | ||
AutostartSchedule: dbAutostartSchedule, | ||
NextStartAt: nextStartAt, | ||
Ttl: dbTTL, | ||
// The workspaces page will sort by last used at, and it's useful to | ||
// have the newly created workspace at the top of the list! | ||
LastUsedAt: dbtime.Now(), | ||
AutomaticUpdates: dbAU, | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("insert workspace: %w", err) | ||
} | ||
workspaceID = minimumWorkspace.ID | ||
} else { | ||
// Prebuild found! | ||
workspaceID = claimedWorkspace.ID | ||
initiatorID = prebuildsClaimer.Initiator() | ||
dannykopping marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
// We have to refetch the workspace for the joined in fields. | ||
// TODO: We can use WorkspaceTable for the builder to not require | ||
// this extra fetch. | ||
workspace, err = db.GetWorkspaceByID(ctx,workspaceID) | ||
if err != nil { | ||
return xerrors.Errorf("get workspace by ID: %w", err) | ||
} | ||
@@ -676,6 +702,13 @@ func createWorkspace( | ||
if req.TemplateVersionID != uuid.Nil { | ||
builder = builder.VersionID(req.TemplateVersionID) | ||
} | ||
if req.TemplateVersionPresetID != uuid.Nil { | ||
builder = builder.TemplateVersionPresetID(req.TemplateVersionPresetID) | ||
} | ||
if claimedWorkspace != nil { | ||
builder = builder.MarkPrebuildClaimedBy(owner.ID) | ||
} | ||
if req.EnableDynamicParameters && api.Experiments.Enabled(codersdk.ExperimentDynamicParameters) { | ||
builder = builder.UsingDynamicParameters() | ||
} | ||
@@ -842,6 +875,21 @@ func requestTemplate(ctx context.Context, rw http.ResponseWriter, req codersdk.C | ||
return template, true | ||
} | ||
func claimPrebuild(ctx context.Context, claimer prebuilds.Claimer, db database.Store, logger slog.Logger, req codersdk.CreateWorkspaceRequest, owner workspaceOwner) (*database.Workspace, error) { | ||
claimedID, err := claimer.Claim(ctx, owner.ID, req.Name, req.TemplateVersionPresetID) | ||
if err != nil { | ||
// TODO: enhance this by clarifying whether this *specific* prebuild failed or whether there are none to claim. | ||
return nil, xerrors.Errorf("claim prebuild: %w", err) | ||
} | ||
lookup, err := db.GetWorkspaceByID(ctx, *claimedID) | ||
if err != nil { | ||
logger.Error(ctx, "unable to find claimed workspace by ID", slog.Error(err), slog.F("claimed_prebuild_id", claimedID.String())) | ||
return nil, xerrors.Errorf("find claimed workspace by ID %q: %w", claimedID.String(), err) | ||
} | ||
return &lookup, nil | ||
} | ||
func (api *API) notifyWorkspaceCreated( | ||
ctx context.Context, | ||
receiverID uuid.UUID, | ||
16 changes: 12 additions & 4 deletionscoderd/wsbuilder/wsbuilder.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletionsenterprise/coderd/prebuilds/claim.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package prebuilds | ||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
"github.com/google/uuid" | ||
"golang.org/x/xerrors" | ||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/prebuilds" | ||
) | ||
type EnterpriseClaimer struct { | ||
store database.Store | ||
} | ||
func NewEnterpriseClaimer(store database.Store) *EnterpriseClaimer { | ||
return &EnterpriseClaimer{ | ||
store: store, | ||
} | ||
} | ||
func (c EnterpriseClaimer) Claim( | ||
ctx context.Context, | ||
userID uuid.UUID, | ||
name string, | ||
presetID uuid.UUID, | ||
) (*uuid.UUID, error) { | ||
result, err := c.store.ClaimPrebuiltWorkspace(ctx, database.ClaimPrebuiltWorkspaceParams{ | ||
NewUserID: userID, | ||
NewName: name, | ||
PresetID: presetID, | ||
}) | ||
if err != nil { | ||
switch { | ||
// No eligible prebuilds found | ||
case errors.Is(err, sql.ErrNoRows): | ||
return nil, prebuilds.ErrNoClaimablePrebuiltWorkspaces | ||
default: | ||
return nil, xerrors.Errorf("claim prebuild for user %q: %w", userID.String(), err) | ||
} | ||
} | ||
return &result.ID, nil | ||
} | ||
func (EnterpriseClaimer) Initiator() uuid.UUID { | ||
return prebuilds.SystemUserID | ||
} | ||
var _ prebuilds.Claimer = &EnterpriseClaimer{} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.