fix: update workspace TTL on template TTL change (#15761)

Relates to https://github.com/coder/coder/issues/15390

Currently when a user creates a workspace, their workspace's TTL is
determined by the template's default TTL. If the Coder instance is AGPL,
or if the template has disallowed the user from configuring autostop,
then it is not possible to change the workspace's TTL after creation.
Any changes to the template's default TTL only takes effect on _new_
workspaces.

This PR modifies the behaviour slightly so that on AGPL Coder, or on
enterprise when a template does not allow user's to configure their
workspace's TTL, updating the template's default TTL will also update
any workspace's TTL to match this value.
This commit is contained in:
Danielle Maywood
2024-12-06 11:01:39 +00:00
committed by GitHub
parent 67553a7bbe
commit 40624bf78b
12 changed files with 518 additions and 0 deletions
+18
View File
@@ -2,6 +2,7 @@ package schedule
import (
"context"
"database/sql"
"time"
"github.com/google/uuid"
@@ -228,6 +229,23 @@ func (*agplTemplateScheduleStore) Set(ctx context.Context, db database.Store, tp
return xerrors.Errorf("update template schedule: %w", err)
}
// Users running the AGPL version are unable to customize their workspaces
// autostop, so we want to keep their workspaces in track with any template
// TTL changes.
if tpl.DefaultTTL != int64(opts.DefaultTTL) {
var ttl sql.NullInt64
if opts.DefaultTTL != 0 {
ttl = sql.NullInt64{Valid: true, Int64: int64(opts.DefaultTTL)}
}
if err = db.UpdateWorkspacesTTLByTemplateID(ctx, database.UpdateWorkspacesTTLByTemplateIDParams{
TemplateID: tpl.ID,
Ttl: ttl,
}); err != nil {
return xerrors.Errorf("update workspace ttl by template id %q: %w", tpl.ID, err)
}
}
template, err = db.GetTemplateByID(ctx, tpl.ID)
if err != nil {
return xerrors.Errorf("fetch updated template: %w", err)