Skip to content

Commit fc95dbe

Browse files
committed
allow setting template max_ttl to 0
1 parent c14aadd commit fc95dbe

File tree

5 files changed

+56
-19
lines changed

5 files changed

+56
-19
lines changed

coderd/database/databasefake/databasefake.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,10 +1564,6 @@ func (q *fakeQuerier) InsertTemplate(_ context.Context, arg database.InsertTempl
15641564
q.mutex.Lock()
15651565
defer q.mutex.Unlock()
15661566

1567-
// default values
1568-
if arg.MaxTtl == 0 {
1569-
arg.MaxTtl = int64(168 * time.Hour)
1570-
}
15711567
if arg.MinAutostartInterval == 0 {
15721568
arg.MinAutostartInterval = int64(time.Hour)
15731569
}

coderd/templates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
173173
}
174174

175175
maxTTL := maxTTLDefault
176-
if !ptr.NilOrZero(createTemplate.MaxTTLMillis) {
176+
if createTemplate.MaxTTLMillis != nil {
177177
maxTTL = time.Duration(*createTemplate.MaxTTLMillis) * time.Millisecond
178178
}
179179
if maxTTL < 0 {

coderd/templates_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,24 @@ func TestPostTemplateByOrganization(t *testing.T) {
148148
require.Contains(t, err.Error(), "max_ttl_ms: Cannot be greater than")
149149
})
150150

151+
t.Run("NoMaxTTL", func(t *testing.T) {
152+
t.Parallel()
153+
client := coderdtest.New(t, nil)
154+
user := coderdtest.CreateFirstUser(t, client)
155+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
156+
157+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
158+
defer cancel()
159+
160+
got, err := client.CreateTemplate(ctx, user.OrganizationID, codersdk.CreateTemplateRequest{
161+
Name: "testing",
162+
VersionID: version.ID,
163+
MaxTTLMillis: ptr.Ref(int64(0)),
164+
})
165+
require.NoError(t, err)
166+
require.Zero(t, got.MaxTTLMillis)
167+
})
168+
151169
t.Run("Unauthorized", func(t *testing.T) {
152170
t.Parallel()
153171
client := coderdtest.New(t, nil)

coderd/workspaces.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ import (
3232
"github.com/coder/coder/codersdk"
3333
)
3434

35-
const workspaceDefaultTTL = 12 * time.Hour
36-
3735
var (
3836
ttlMin = time.Minute //nolint:revive // min here means 'minimum' not 'minutes'
3937
ttlMax = 7 * 24 * time.Hour
@@ -312,10 +310,10 @@ func (api *API) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Req
312310
return
313311
}
314312

315-
if !dbTTL.Valid {
316-
// Default to min(12 hours, template maximum). Just defaulting to template maximum can be surprising.
317-
dbTTL = sql.NullInt64{Valid: true, Int64: min(template.MaxTtl, int64(workspaceDefaultTTL))}
318-
}
313+
// if !dbTTL.Valid {
314+
// Default to min(12 hours, template maximum). Just defaulting to template maximum can be surprising.
315+
// dbTTL = sql.NullInt64{Valid: true, Int64: min(template.MaxTtl, int64(workspaceDefaultTTL))}
316+
// }
319317

320318
workspace, err := api.Database.GetWorkspaceByOwnerIDAndName(r.Context(), database.GetWorkspaceByOwnerIDAndNameParams{
321319
OwnerID: apiKey.UserID,
@@ -923,7 +921,8 @@ func validWorkspaceTTLMillis(millis *int64, max time.Duration) (sql.NullInt64, e
923921
return sql.NullInt64{}, errTTLMax
924922
}
925923

926-
if truncated > max {
924+
// template level
925+
if max > 0 && truncated > max {
927926
return sql.NullInt64{}, xerrors.Errorf("time until shutdown must be below template maximum %s", max.String())
928927
}
929928

@@ -1050,10 +1049,3 @@ func splitQueryParameterByDelimiter(query string, delimiter rune, maintainQuotes
10501049

10511050
return parts
10521051
}
1053-
1054-
func min(x, y int64) int64 {
1055-
if x < y {
1056-
return x
1057-
}
1058-
return y
1059-
}

coderd/workspaces_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,26 @@ func TestPostWorkspacesByOrganization(t *testing.T) {
191191
_ = coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
192192
})
193193

194+
t.Run("TemplateNoTTL", func(t *testing.T) {
195+
t.Parallel()
196+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
197+
user := coderdtest.CreateFirstUser(t, client)
198+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
199+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
200+
ctr.MaxTTLMillis = ptr.Ref(int64(0))
201+
})
202+
// Given: the template has no max TTL set
203+
require.Zero(t, template.MaxTTLMillis)
204+
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
205+
206+
// When: we create a workspace with autostop not enabled
207+
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID, func(cwr *codersdk.CreateWorkspaceRequest) {
208+
cwr.TTLMillis = ptr.Ref(int64(0))
209+
})
210+
// Then: No TTL should be set by the template
211+
require.Nil(t, workspace.TTLMillis)
212+
})
213+
194214
t.Run("TemplateCustomTTL", func(t *testing.T) {
195215
t.Parallel()
196216
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
@@ -1051,6 +1071,17 @@ func TestWorkspaceUpdateTTL(t *testing.T) {
10511071
expectedError: "ttl_ms: time until shutdown must be below template maximum 8h0m0s",
10521072
modifyTemplate: func(ctr *codersdk.CreateTemplateRequest) { ctr.MaxTTLMillis = ptr.Ref((8 * time.Hour).Milliseconds()) },
10531073
},
1074+
{
1075+
name: "no template maximum ttl",
1076+
ttlMillis: ptr.Ref((7 * 24 * time.Hour).Milliseconds()),
1077+
modifyTemplate: func(ctr *codersdk.CreateTemplateRequest) { ctr.MaxTTLMillis = ptr.Ref(int64(0)) },
1078+
},
1079+
{
1080+
name: "above maximum ttl even with no template max",
1081+
ttlMillis: ptr.Ref((365 * 24 * time.Hour).Milliseconds()),
1082+
expectedError: "ttl_ms: time until shutdown must be less than 7 days",
1083+
modifyTemplate: func(ctr *codersdk.CreateTemplateRequest) { ctr.MaxTTLMillis = ptr.Ref(int64(0)) },
1084+
},
10541085
}
10551086

10561087
for _, testCase := range testCases {

0 commit comments

Comments
 (0)