Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: update workspace TTL on template TTL change#15761

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
DanielleMaywood merged 7 commits intomainfromdm-workspace-template-ttl
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletionscoderd/database/dbauthz/dbauthz.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4138,6 +4138,17 @@ func (q *querier) UpdateWorkspacesDormantDeletingAtByTemplateID(ctx context.Cont
return q.db.UpdateWorkspacesDormantDeletingAtByTemplateID(ctx, arg)
}

func (q *querier) UpdateWorkspacesTTLByTemplateID(ctx context.Context, arg database.UpdateWorkspacesTTLByTemplateIDParams) error {
template, err := q.db.GetTemplateByID(ctx, arg.TemplateID)
if err != nil {
return xerrors.Errorf("get template by id: %w", err)
}
if err := q.authorizeContext(ctx, policy.ActionUpdate, template); err != nil {
return err
}
return q.db.UpdateWorkspacesTTLByTemplateID(ctx, arg)
}

func (q *querier) UpsertAnnouncementBanners(ctx context.Context, value string) error {
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceDeploymentConfig); err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletionscoderd/database/dbauthz/dbauthz_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1017,6 +1017,12 @@ func (s *MethodTestSuite) TestTemplate() {
TemplateID: t1.ID,
}).Asserts(t1, policy.ActionUpdate)
}))
s.Run("UpdateWorkspacesTTLByTemplateID", s.Subtest(func(db database.Store, check *expects) {
t1 := dbgen.Template(s.T(), db, database.Template{})
check.Args(database.UpdateWorkspacesTTLByTemplateIDParams{
TemplateID: t1.ID,
}).Asserts(t1, policy.ActionUpdate)
}))
s.Run("UpdateTemplateActiveVersionByID", s.Subtest(func(db database.Store, check *expects) {
t1 := dbgen.Template(s.T(), db, database.Template{
ActiveVersionID: uuid.New(),
Expand Down
20 changes: 20 additions & 0 deletionscoderd/database/dbmem/dbmem.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10192,6 +10192,26 @@ func (q *FakeQuerier) UpdateWorkspacesDormantDeletingAtByTemplateID(_ context.Co
return affectedRows, nil
}

func (q *FakeQuerier) UpdateWorkspacesTTLByTemplateID(_ context.Context, arg database.UpdateWorkspacesTTLByTemplateIDParams) error {
err := validateDatabaseType(arg)
if err != nil {
return err
}

q.mutex.Lock()
defer q.mutex.Unlock()

for i, ws := range q.workspaces {
if ws.TemplateID != arg.TemplateID {
continue
}

q.workspaces[i].Ttl = arg.Ttl
}

return nil
}

func (q *FakeQuerier) UpsertAnnouncementBanners(_ context.Context, data string) error {
q.mutex.RLock()
defer q.mutex.RUnlock()
Expand Down
7 changes: 7 additions & 0 deletionscoderd/database/dbmetrics/querymetrics.go
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

14 changes: 14 additions & 0 deletionscoderd/database/dbmock/dbmock.go
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

1 change: 1 addition & 0 deletionscoderd/database/querier.go
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

19 changes: 19 additions & 0 deletionscoderd/database/queries.sql.go
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

8 changes: 8 additions & 0 deletionscoderd/database/queries/workspaces.sql
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -501,6 +501,14 @@ SET
WHERE
id = $1;

-- name: UpdateWorkspacesTTLByTemplateID :exec
UPDATE
workspaces
SET
ttl = $2
WHERE
template_id = $1;

-- name: UpdateWorkspaceLastUsedAt :exec
UPDATE
workspaces
Expand Down
18 changes: 18 additions & 0 deletionscoderd/schedule/template.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,7 @@ package schedule

import (
"context"
"database/sql"
"time"

"github.com/google/uuid"
Expand DownExpand Up@@ -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)
Expand Down
150 changes: 150 additions & 0 deletionscoderd/schedule/template_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
package schedule_test

import (
"database/sql"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbgen"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/schedule"
"github.com/coder/coder/v2/testutil"
)

func TestTemplateTTL(t *testing.T) {
t.Parallel()

tests := []struct {
name string
fromTTL time.Duration
toTTL time.Duration
expected sql.NullInt64
}{
{
name: "ModifyTTLDurationDown",
fromTTL: 24 * time.Hour,
toTTL: 1 * time.Hour,
expected: sql.NullInt64{Valid: true, Int64: int64(1 * time.Hour)},
},
{
name: "ModifyTTLDurationUp",
fromTTL: 24 * time.Hour,
toTTL: 36 * time.Hour,
expected: sql.NullInt64{Valid: true, Int64: int64(36 * time.Hour)},
},
{
name: "ModifyTTLDurationSame",
fromTTL: 24 * time.Hour,
toTTL: 24 * time.Hour,
expected: sql.NullInt64{Valid: true, Int64: int64(24 * time.Hour)},
},
{
name: "DisableTTL",
fromTTL: 24 * time.Hour,
toTTL: 0,
expected: sql.NullInt64{},
},
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

var (
db, _ = dbtestutil.NewDB(t)
ctx = testutil.Context(t, testutil.WaitLong)
user = dbgen.User(t, db, database.User{})
file = dbgen.File(t, db, database.File{CreatedBy: user.ID})
// Create first template
templateJob = dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{
FileID: file.ID,
InitiatorID: user.ID,
Tags: database.StringMap{"foo": "bar"},
})
templateVersion = dbgen.TemplateVersion(t, db, database.TemplateVersion{
CreatedBy: user.ID,
JobID: templateJob.ID,
OrganizationID: templateJob.OrganizationID,
})
template = dbgen.Template(t, db, database.Template{
ActiveVersionID: templateVersion.ID,
CreatedBy: user.ID,
OrganizationID: templateJob.OrganizationID,
})
// Create second template
otherTTL = tt.fromTTL + 6*time.Hour
otherTemplateJob = dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{
FileID: file.ID,
InitiatorID: user.ID,
Tags: database.StringMap{"foo": "bar"},
})
otherTemplateVersion = dbgen.TemplateVersion(t, db, database.TemplateVersion{
CreatedBy: user.ID,
JobID: otherTemplateJob.ID,
OrganizationID: otherTemplateJob.OrganizationID,
})
otherTemplate = dbgen.Template(t, db, database.Template{
ActiveVersionID: otherTemplateVersion.ID,
CreatedBy: user.ID,
OrganizationID: otherTemplateJob.OrganizationID,
})
)

templateScheduleStore := schedule.NewAGPLTemplateScheduleStore()

// Set both template's default TTL
template, err := templateScheduleStore.Set(ctx, db, template, schedule.TemplateScheduleOptions{
DefaultTTL: tt.fromTTL,
})
require.NoError(t, err)
otherTemplate, err = templateScheduleStore.Set(ctx, db, otherTemplate, schedule.TemplateScheduleOptions{
DefaultTTL: otherTTL,
})
require.NoError(t, err)

// We create two workspaces here, one with the template we're modifying, the
// other with a different template. We want to ensure we only modify one
// of the workspaces.
workspace := dbgen.Workspace(t, db, database.WorkspaceTable{
OwnerID: user.ID,
TemplateID: template.ID,
OrganizationID: templateJob.OrganizationID,
LastUsedAt: dbtime.Now(),
Ttl: sql.NullInt64{Valid: true, Int64: int64(tt.fromTTL)},
})
otherWorkspace := dbgen.Workspace(t, db, database.WorkspaceTable{
OwnerID: user.ID,
TemplateID: otherTemplate.ID,
OrganizationID: otherTemplateJob.OrganizationID,
LastUsedAt: dbtime.Now(),
Ttl: sql.NullInt64{Valid: true, Int64: int64(otherTTL)},
})

// Ensure the workspace's start with the correct TTLs
require.Equal(t, sql.NullInt64{Valid: true, Int64: int64(tt.fromTTL)}, workspace.Ttl)
require.Equal(t, sql.NullInt64{Valid: true, Int64: int64(otherTTL)}, otherWorkspace.Ttl)

// Update _only_ the primary template's TTL
_, err = templateScheduleStore.Set(ctx, db, template, schedule.TemplateScheduleOptions{
DefaultTTL: tt.toTTL,
})
require.NoError(t, err)

// Verify the primary workspace's TTL has been updated.
ws, err := db.GetWorkspaceByID(ctx, workspace.ID)
require.NoError(t, err)
require.Equal(t, tt.expected, ws.Ttl)

// Verify that the other workspace's TTL has not been touched.
ws, err = db.GetWorkspaceByID(ctx, otherWorkspace.ID)
require.NoError(t, err)
require.Equal(t, sql.NullInt64{Valid: true, Int64: int64(otherTTL)}, ws.Ttl)
})
}
}
17 changes: 17 additions & 0 deletionsenterprise/coderd/schedule/template.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -195,6 +195,23 @@ func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.S
return xerrors.Errorf("get updated template schedule: %w", err)
}

// Update all workspace's TTL using this template if either of the following:
// - The template's AllowUserAutostop has just been disabled
// - The template's TTL has been modified and AllowUserAutostop is disabled
if !opts.UserAutostopEnabled && (tpl.AllowUserAutostop || int64(opts.DefaultTTL) != tpl.DefaultTTL) {
var ttl sql.NullInt64
if opts.DefaultTTL != 0 {
ttl = sql.NullInt64{Valid: true, Int64: int64(opts.DefaultTTL)}
}

if err = tx.UpdateWorkspacesTTLByTemplateID(ctx, database.UpdateWorkspacesTTLByTemplateIDParams{
TemplateID: template.ID,
Ttl: ttl,
}); err != nil {
return xerrors.Errorf("update workspaces ttl by template id %q: %w", template.ID, err)
}
}

// Recalculate max_deadline and deadline for all running workspace
// builds on this template.
err = s.updateWorkspaceBuilds(ctx, tx, template)
Expand Down
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp