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

Commit3394644

Browse files
fix: update workspace TTL on template TTL change
1 parentc47e7af commit3394644

File tree

12 files changed

+324
-0
lines changed

12 files changed

+324
-0
lines changed

‎coderd/database/dbauthz/dbauthz.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4094,6 +4094,17 @@ func (q *querier) UpdateWorkspacesDormantDeletingAtByTemplateID(ctx context.Cont
40944094
returnq.db.UpdateWorkspacesDormantDeletingAtByTemplateID(ctx,arg)
40954095
}
40964096

4097+
func (q*querier)UpdateWorkspacesTTLByTemplateID(ctx context.Context,arg database.UpdateWorkspacesTTLByTemplateIDParams)error {
4098+
template,err:=q.db.GetTemplateByID(ctx,arg.TemplateID)
4099+
iferr!=nil {
4100+
returnxerrors.Errorf("get template by id: %w",err)
4101+
}
4102+
iferr:=q.authorizeContext(ctx,policy.ActionUpdate,template);err!=nil {
4103+
returnerr
4104+
}
4105+
returnq.db.UpdateWorkspacesTTLByTemplateID(ctx,arg)
4106+
}
4107+
40974108
func (q*querier)UpsertAnnouncementBanners(ctx context.Context,valuestring)error {
40984109
iferr:=q.authorizeContext(ctx,policy.ActionUpdate,rbac.ResourceDeploymentConfig);err!=nil {
40994110
returnerr

‎coderd/database/dbauthz/dbauthz_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,12 @@ func (s *MethodTestSuite) TestTemplate() {
10171017
TemplateID:t1.ID,
10181018
}).Asserts(t1,policy.ActionUpdate)
10191019
}))
1020+
s.Run("UpdateWorkspacesTTLByTemplateID",s.Subtest(func(db database.Store,check*expects) {
1021+
t1:=dbgen.Template(s.T(),db, database.Template{})
1022+
check.Args(database.UpdateWorkspacesTTLByTemplateIDParams{
1023+
TemplateID:t1.ID,
1024+
}).Asserts(t1,policy.ActionUpdate)
1025+
}))
10201026
s.Run("UpdateTemplateActiveVersionByID",s.Subtest(func(db database.Store,check*expects) {
10211027
t1:=dbgen.Template(s.T(),db, database.Template{
10221028
ActiveVersionID:uuid.New(),

‎coderd/database/dbmem/dbmem.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10117,6 +10117,26 @@ func (q *FakeQuerier) UpdateWorkspacesDormantDeletingAtByTemplateID(_ context.Co
1011710117
returnaffectedRows,nil
1011810118
}
1011910119

10120+
func (q*FakeQuerier)UpdateWorkspacesTTLByTemplateID(_ context.Context,arg database.UpdateWorkspacesTTLByTemplateIDParams)error {
10121+
err:=validateDatabaseType(arg)
10122+
iferr!=nil {
10123+
returnerr
10124+
}
10125+
10126+
q.mutex.Lock()
10127+
deferq.mutex.Unlock()
10128+
10129+
fori,ws:=rangeq.workspaces {
10130+
ifws.TemplateID!=arg.TemplateID {
10131+
continue
10132+
}
10133+
10134+
q.workspaces[i].Ttl=arg.Ttl
10135+
}
10136+
10137+
returnnil
10138+
}
10139+
1012010140
func (q*FakeQuerier)UpsertAnnouncementBanners(_ context.Context,datastring)error {
1012110141
q.mutex.RLock()
1012210142
deferq.mutex.RUnlock()

‎coderd/database/dbmetrics/querymetrics.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/dbmock/dbmock.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/queries.sql.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/queries/workspaces.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,14 @@ SET
474474
WHERE
475475
id= $1;
476476

477+
-- name: UpdateWorkspacesTTLByTemplateID :exec
478+
UPDATE
479+
workspaces
480+
SET
481+
ttl= $2
482+
WHERE
483+
template_id= $1;
484+
477485
-- name: UpdateWorkspaceLastUsedAt :exec
478486
UPDATE
479487
workspaces

‎coderd/schedule/template.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package schedule
22

33
import (
44
"context"
5+
"database/sql"
56
"time"
67

78
"github.com/google/uuid"
@@ -228,6 +229,23 @@ func (*agplTemplateScheduleStore) Set(ctx context.Context, db database.Store, tp
228229
returnxerrors.Errorf("update template schedule: %w",err)
229230
}
230231

232+
// Users running the AGPL version are unable to customize their workspaces
233+
// autostop, so we want to keep their workspaces in track with any template
234+
// TTL changes.
235+
iftpl.DefaultTTL!=int64(opts.DefaultTTL) {
236+
varttl sql.NullInt64
237+
ifopts.DefaultTTL!=0 {
238+
ttl= sql.NullInt64{Valid:true,Int64:int64(opts.DefaultTTL)}
239+
}
240+
241+
iferr=db.UpdateWorkspacesTTLByTemplateID(ctx, database.UpdateWorkspacesTTLByTemplateIDParams{
242+
TemplateID:tpl.ID,
243+
Ttl:ttl,
244+
});err!=nil {
245+
returnxerrors.Errorf("update workspace ttl by template id %q: %w",tpl.ID,err)
246+
}
247+
}
248+
231249
template,err=db.GetTemplateByID(ctx,tpl.ID)
232250
iferr!=nil {
233251
returnxerrors.Errorf("fetch updated template: %w",err)

‎coderd/schedule/template_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package schedule_test
2+
3+
import (
4+
"database/sql"
5+
"testing"
6+
"time"
7+
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/coder/coder/v2/coderd/database"
11+
"github.com/coder/coder/v2/coderd/database/dbgen"
12+
"github.com/coder/coder/v2/coderd/database/dbtestutil"
13+
"github.com/coder/coder/v2/coderd/database/dbtime"
14+
"github.com/coder/coder/v2/coderd/schedule"
15+
"github.com/coder/coder/v2/testutil"
16+
)
17+
18+
funcTestTemplateTTL(t*testing.T) {
19+
t.Parallel()
20+
21+
t.Run("ModifiesWorkspaceTTL",func(t*testing.T) {
22+
t.Parallel()
23+
24+
var (
25+
db,_=dbtestutil.NewDB(t)
26+
ctx=testutil.Context(t,testutil.WaitLong)
27+
user=dbgen.User(t,db, database.User{})
28+
file=dbgen.File(t,db, database.File{CreatedBy:user.ID})
29+
templateJob=dbgen.ProvisionerJob(t,db,nil, database.ProvisionerJob{
30+
FileID:file.ID,
31+
InitiatorID:user.ID,
32+
Tags: database.StringMap{"foo":"bar"},
33+
})
34+
defaultTTL=24*time.Hour
35+
templateVersion=dbgen.TemplateVersion(t,db, database.TemplateVersion{
36+
CreatedBy:user.ID,
37+
JobID:templateJob.ID,
38+
OrganizationID:templateJob.OrganizationID,
39+
})
40+
template=dbgen.Template(t,db, database.Template{
41+
ActiveVersionID:templateVersion.ID,
42+
CreatedBy:user.ID,
43+
OrganizationID:templateJob.OrganizationID,
44+
DefaultTTL:int64(defaultTTL),
45+
})
46+
workspace=dbgen.Workspace(t,db, database.WorkspaceTable{
47+
OwnerID:user.ID,
48+
TemplateID:template.ID,
49+
OrganizationID:templateJob.OrganizationID,
50+
LastUsedAt:dbtime.Now(),
51+
Ttl: sql.NullInt64{Valid:true,Int64:int64(defaultTTL)},
52+
})
53+
)
54+
55+
templateScheduleStore:=schedule.NewAGPLTemplateScheduleStore()
56+
57+
// We've created a template with a TTL of 24 hours, so we expect our
58+
// workspace to have a TTL of 24 hours.
59+
require.Equal(t, sql.NullInt64{Valid:true,Int64:int64(defaultTTL)},workspace.Ttl)
60+
61+
// We expect an AGPL template schedule store to always update
62+
// the TTL of existing workspaces.
63+
_,err:=templateScheduleStore.Set(ctx,db,template, schedule.TemplateScheduleOptions{
64+
DefaultTTL:1*time.Hour,
65+
})
66+
require.NoError(t,err)
67+
68+
// Verify that the workspace's TTL has been updated.
69+
ws,err:=db.GetWorkspaceByID(ctx,workspace.ID)
70+
require.NoError(t,err)
71+
require.Equal(t, sql.NullInt64{Valid:true,Int64:int64(1*time.Hour)},ws.Ttl)
72+
})
73+
}

‎enterprise/coderd/schedule/template.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,22 @@ func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.S
192192
returnxerrors.Errorf("get updated template schedule: %w",err)
193193
}
194194

195+
// If this template disallows users from customizing their own autostop, then
196+
// we want to keep their workspaces in track with any template TTL changes.
197+
if!template.AllowUserAutostop&&int64(opts.DefaultTTL)!=tpl.DefaultTTL {
198+
varttl sql.NullInt64
199+
ifopts.DefaultTTL!=0 {
200+
ttl= sql.NullInt64{Valid:true,Int64:int64(opts.DefaultTTL)}
201+
}
202+
203+
iferr=tx.UpdateWorkspacesTTLByTemplateID(ctx, database.UpdateWorkspacesTTLByTemplateIDParams{
204+
TemplateID:template.ID,
205+
Ttl:ttl,
206+
});err!=nil {
207+
returnxerrors.Errorf("update workspaces ttl by template id %q: %w",template.ID,err)
208+
}
209+
}
210+
195211
// Recalculate max_deadline and deadline for all running workspace
196212
// builds on this template.
197213
err=s.updateWorkspaceBuilds(ctx,tx,template)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp