- Notifications
You must be signed in to change notification settings - Fork1k
fix: disallow lifecycle endpoints for prebuilt workspaces#19264
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
a5690c2
951cbca
7bbb045
0884628
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1089,6 +1089,17 @@ func (api *API) putWorkspaceAutostart(rw http.ResponseWriter, r *http.Request) { | ||
return | ||
} | ||
// Autostart configuration is not supported for prebuilt workspaces. | ||
// Prebuild lifecycle is managed by the reconciliation loop, with scheduling behavior | ||
// defined per preset at the template level, not per workspace. | ||
if workspace.IsPrebuild() { | ||
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 409 Conflict based on discussion:#19252 (comment) | ||
Message: "Autostart is not supported for prebuilt workspaces", | ||
Detail: "Prebuilt workspace scheduling is configured per preset at the template level. Workspace-level overrides are not supported.", | ||
}) | ||
return | ||
} | ||
dbSched, err := validWorkspaceSchedule(req.Schedule) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
@@ -1115,12 +1126,20 @@ func (api *API) putWorkspaceAutostart(rw http.ResponseWriter, r *http.Request) { | ||
return | ||
} | ||
// Use injected Clock to allow time mocking in tests | ||
now := api.Clock.Now() | ||
ContributorAuthor
| ||
nextStartAt := sql.NullTime{} | ||
if dbSched.Valid { | ||
next, err := schedule.NextAllowedAutostart(now, dbSched.String, templateSchedule) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error calculating workspace autostart schedule.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
nextStartAt = sql.NullTime{Valid: true, Time: dbtime.Time(next.UTC())} | ||
} | ||
err = api.Database.UpdateWorkspaceAutostart(ctx, database.UpdateWorkspaceAutostartParams{ | ||
@@ -1173,6 +1192,17 @@ func (api *API) putWorkspaceTTL(rw http.ResponseWriter, r *http.Request) { | ||
return | ||
} | ||
// TTL updates are not supported for prebuilt workspaces. | ||
// Prebuild lifecycle is managed by the reconciliation loop, with TTL behavior | ||
// defined per preset at the template level, not per workspace. | ||
if workspace.IsPrebuild() { | ||
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{ | ||
Message: "TTL updates are not supported for prebuilt workspaces", | ||
Detail: "Prebuilt workspace TTL is configured per preset at the template level. Workspace-level overrides are not supported.", | ||
}) | ||
return | ||
} | ||
var dbTTL sql.NullInt64 | ||
err := api.Database.InTx(func(s database.Store) error { | ||
@@ -1198,6 +1228,9 @@ func (api *API) putWorkspaceTTL(rw http.ResponseWriter, r *http.Request) { | ||
return xerrors.Errorf("update workspace time until shutdown: %w", err) | ||
} | ||
// Use injected Clock to allow time mocking in tests | ||
now := api.Clock.Now() | ||
// If autostop has been disabled, we want to remove the deadline from the | ||
// existing workspace build (if there is one). | ||
if !dbTTL.Valid { | ||
@@ -1225,7 +1258,7 @@ func (api *API) putWorkspaceTTL(rw http.ResponseWriter, r *http.Request) { | ||
// more information. | ||
Deadline: build.MaxDeadline, | ||
MaxDeadline: build.MaxDeadline, | ||
UpdatedAt: dbtime.Time(now), | ||
}); err != nil { | ||
return xerrors.Errorf("update workspace build deadline: %w", err) | ||
} | ||
@@ -1289,17 +1322,30 @@ func (api *API) putWorkspaceDormant(rw http.ResponseWriter, r *http.Request) { | ||
return | ||
} | ||
// Dormancy configuration is not supported for prebuilt workspaces. | ||
// Prebuilds are managed by the reconciliation loop and are not subject to dormancy. | ||
if oldWorkspace.IsPrebuild() { | ||
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{ | ||
Message: "Dormancy updates are not supported for prebuilt workspaces", | ||
Detail: "Prebuilt workspaces are not subject to dormancy. Dormancy behavior is only applicable to regular workspaces", | ||
}) | ||
return | ||
} | ||
// If the workspace is already in the desired state do nothing! | ||
if oldWorkspace.DormantAt.Valid == req.Dormant { | ||
rw.WriteHeader(http.StatusNotModified) | ||
return | ||
} | ||
// Use injected Clock to allow time mocking in tests | ||
now := api.Clock.Now() | ||
dormantAt := sql.NullTime{ | ||
Valid: req.Dormant, | ||
} | ||
if req.Dormant { | ||
dormantAt.Time = dbtime.Time(now) | ||
} | ||
newWorkspace, err := api.Database.UpdateWorkspaceDormantDeletingAt(ctx, database.UpdateWorkspaceDormantDeletingAtParams{ | ||
@@ -1339,7 +1385,7 @@ func (api *API) putWorkspaceDormant(rw http.ResponseWriter, r *http.Request) { | ||
} | ||
if initiatorErr == nil && tmplErr == nil { | ||
dormantTime := dbtime.Time(now).Add(time.Duration(tmpl.TimeTilDormant)) | ||
_, err = api.NotificationsEnqueuer.Enqueue( | ||
// nolint:gocritic // Need notifier actor to enqueue notifications | ||
dbauthz.AsNotifier(ctx), | ||
@@ -1433,6 +1479,17 @@ func (api *API) putExtendWorkspace(rw http.ResponseWriter, r *http.Request) { | ||
return | ||
} | ||
// Deadline extensions are not supported for prebuilt workspaces. | ||
// Prebuilds are managed by the reconciliation loop and must always have | ||
// Deadline and MaxDeadline unset. | ||
if workspace.IsPrebuild() { | ||
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{ | ||
Message: "Deadline extension is not supported for prebuilt workspaces", | ||
Detail: "Prebuilt workspaces do not support user deadline modifications. Deadline extension is only applicable to regular workspaces", | ||
}) | ||
return | ||
} | ||
code := http.StatusOK | ||
resp := codersdk.Response{} | ||
@@ -1469,8 +1526,11 @@ func (api *API) putExtendWorkspace(rw http.ResponseWriter, r *http.Request) { | ||
return xerrors.Errorf("workspace shutdown is manual") | ||
} | ||
// Use injected Clock to allow time mocking in tests | ||
now := api.Clock.Now() | ||
newDeadline := req.Deadline.UTC() | ||
if err := validWorkspaceDeadline(now,job.CompletedAt.Time, newDeadline); err != nil { | ||
// NOTE(Cian): Putting the error in the Message field on request from the FE folks. | ||
// Normally, we would put the validation error in Validations, but this endpoint is | ||
// not tied to a form or specific named user input on the FE. | ||
@@ -1486,7 +1546,7 @@ func (api *API) putExtendWorkspace(rw http.ResponseWriter, r *http.Request) { | ||
if err := s.UpdateWorkspaceBuildDeadlineByID(ctx, database.UpdateWorkspaceBuildDeadlineByIDParams{ | ||
ID: build.ID, | ||
UpdatedAt: dbtime.Time(now), | ||
Deadline: newDeadline, | ||
MaxDeadline: build.MaxDeadline, | ||
}); err != nil { | ||
@@ -2441,8 +2501,8 @@ func validWorkspaceAutomaticUpdates(updates codersdk.AutomaticUpdates) (database | ||
return dbAU, nil | ||
} | ||
func validWorkspaceDeadline(now,startedAt, newDeadline time.Time) error { | ||
soon :=now.Add(29 * time.Minute) | ||
if newDeadline.Before(soon) { | ||
return errDeadlineTooSoon | ||
} | ||
Uh oh!
There was an error while loading.Please reload this page.