-
Notifications
You must be signed in to change notification settings - Fork 963
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ssncferreira
wants to merge
4
commits into
main
Choose a base branch
from
ssncferreira/fix-api-prebuild-lifecycle-endpoints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+254
−17
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a5690c2
fix(api): disallow lifecycle configuration for prebuilt workspaces
ssncferreira 951cbca
Merge remote-tracking branch 'origin/main' into ssncferreira/fix-api-…
ssncferreira 7bbb045
test: add tests for lifecycle endpoints with prebuilds
ssncferreira 0884628
fix: minor fixes
ssncferreira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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{ | ||
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() | ||
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. If I understand correctly, all timestamps in the database are stored in UTC. |
||
|
||
nextStartAt := sql.NullTime{} | ||
if dbSched.Valid { | ||
next, err := schedule.NextAllowedAutostart(dbtime.Now(), dbSched.String, templateSchedule) | ||
if err == nil { | ||
nextStartAt = sql.NullTime{Valid: true, Time: dbtime.Time(next.UTC())} | ||
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(api.Clock.Now()), | ||
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.Now() | ||
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.Now().Add(time.Duration(tmpl.TimeTilDormant)) | ||
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(job.CompletedAt.Time, newDeadline); err != nil { | ||
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.Now(), | ||
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(startedAt, newDeadline time.Time) error { | ||
soon := time.Now().Add(29 * time.Minute) | ||
func validWorkspaceDeadline(now, startedAt, newDeadline time.Time) error { | ||
soon := now.Add(29 * time.Minute) | ||
if newDeadline.Before(soon) { | ||
return errDeadlineTooSoon | ||
} | ||
|
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
409 Conflict based on discussion: #19252 (comment)