Signed-off-by: Danny Kopping <danny@coder.com>
This commit is contained in:
Danny Kopping
2025-02-10 11:40:02 +02:00
parent b151e1b631
commit 8849ad3f03
3 changed files with 58 additions and 57 deletions

View File

@ -633,62 +633,17 @@ func createWorkspace(
runningWorkspaceAgentID uuid.UUID
)
err = api.Database.InTx(func(db database.Store) error {
var claimedWorkspace *database.Workspace
// TODO: implement matching logic
if true {
//if req.ClaimPrebuildIfAvailable {
// TODO: authz // Can't use existing profiles (i.e. AsSystemRestricted) because of dbauthz rules
var ownerCtx = dbauthz.As(ctx, rbac.Subject{
ID: "owner",
Roles: rbac.RoleIdentifiers{rbac.RoleOwner()},
Groups: []string{},
Scope: rbac.ExpandableScope(rbac.ScopeAll),
})
claimCtx, cancel := context.WithTimeout(ownerCtx, time.Second*10) // TODO: don't use elevated authz context
defer cancel()
// TODO: pass down rich params for matching
claimedID, err := prebuilds.Claim(claimCtx, db, owner.ID, req.Name)
if err != nil {
// TODO: enhance this by clarifying whether this *specific* prebuild failed or whether there are none to claim.
api.Logger.Error(ctx, "failed to claim a prebuild", slog.Error(err))
goto regularPath
}
if claimedID == nil {
api.Logger.Warn(ctx, "no claimable prebuild available", slog.Error(err))
goto regularPath
}
lookup, err := api.Database.GetWorkspaceByID(ownerCtx, *claimedID) // TODO: don't use elevated authz context
if err != nil {
api.Logger.Warn(ctx, "unable to find claimed workspace by ID", slog.Error(err), slog.F("claimed_prebuild_id", (*claimedID).String()))
goto regularPath
}
claimedWorkspace = &lookup
agents, err := api.Database.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ownerCtx, claimedWorkspace.ID)
if err != nil {
api.Logger.Error(ctx, "failed to retrieve running agents of claimed prebuilt workspace",
slog.F("workspace_id", claimedWorkspace.ID), slog.Error(err))
}
if len(agents) >= 1 {
// TODO: handle multiple agents
runningWorkspaceAgentID = agents[0].ID
}
}
regularPath:
now := dbtime.Now()
var workspaceID uuid.UUID
if claimedWorkspace != nil {
workspaceID = claimedWorkspace.ID
initiatorID = prebuilds.PrebuildOwnerUUID
} else {
// Try and claim an eligible prebuild, if available.
claimedWorkspace, err := claimPrebuild(ctx, db, api.Logger, req, owner)
if err != nil {
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(),
@ -710,6 +665,20 @@ func createWorkspace(
return xerrors.Errorf("insert workspace: %w", err)
}
workspaceID = minimumWorkspace.ID
} else {
// Prebuild found!
workspaceID = claimedWorkspace.ID
initiatorID = prebuilds.PrebuildOwnerUUID
agents, err := api.Database.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx, claimedWorkspace.ID)
if err != nil {
api.Logger.Error(ctx, "failed to retrieve running agents of claimed prebuilt workspace",
slog.F("workspace_id", claimedWorkspace.ID), slog.Error(err))
}
if len(agents) >= 1 {
// TODO: handle multiple agents
runningWorkspaceAgentID = agents[0].ID
}
}
// We have to refetch the workspace for the joined in fields.
@ -828,6 +797,40 @@ func createWorkspace(
httpapi.Write(ctx, rw, http.StatusCreated, w)
}
func claimPrebuild(ctx context.Context, db database.Store, logger slog.Logger, req codersdk.CreateWorkspaceRequest, owner workspaceOwner) (*database.Workspace, error) {
// TODO: authz // Can't use existing profiles (i.e. AsSystemRestricted) because of dbauthz rules
var ownerCtx = dbauthz.As(ctx, rbac.Subject{
ID: "owner",
Roles: rbac.RoleIdentifiers{rbac.RoleOwner()},
Groups: []string{},
Scope: rbac.ExpandableScope(rbac.ScopeAll),
})
// TODO: do we need a timeout here?
claimCtx, cancel := context.WithTimeout(ownerCtx, time.Second*10) // TODO: don't use elevated authz context
defer cancel()
// TODO: implement matching logic
// TODO: pass down rich params for matching
claimedID, err := prebuilds.Claim(claimCtx, db, owner.ID, req.Name)
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)
}
// No prebuild available.
if claimedID == nil {
return nil, nil
}
lookup, err := db.GetWorkspaceByID(ownerCtx, *claimedID) // TODO: don't use elevated authz context
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, err
}
func (api *API) notifyWorkspaceCreated(
ctx context.Context,
receiverID uuid.UUID,