Skip to content

feat: add locked TTL field to template meta #8020

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

Merged
merged 8 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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: 9 additions & 2 deletions coderd/apidoc/docs.go

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

11 changes: 9 additions & 2 deletions coderd/apidoc/swagger.json

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

3 changes: 2 additions & 1 deletion coderd/database/dump.sql

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BEGIN;
ALTER TABLE templates DROP COLUMN locked_ttl;
COMMIT;
3 changes: 3 additions & 0 deletions coderd/database/migrations/000128_template_locked_ttl.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BEGIN;
ALTER TABLE templates ADD COLUMN locked_ttl BIGINT NOT NULL DEFAULT 0;
COMMIT;
9 changes: 5 additions & 4 deletions coderd/database/modelqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (q *sqlQuerier) GetAuthorizedTemplates(ctx context.Context, arg GetTemplate
pq.Array(arg.IDs),
)
if err != nil {
return nil, err
return nil, xerrors.Errorf("query context: %w", err)
}
defer rows.Close()
var items []Template
Expand Down Expand Up @@ -82,16 +82,17 @@ func (q *sqlQuerier) GetAuthorizedTemplates(ctx context.Context, arg GetTemplate
&i.AllowUserAutostop,
&i.FailureTTL,
&i.InactivityTTL,
&i.LockedTTL,
); err != nil {
return nil, err
return nil, xerrors.Errorf("scan: %w", err)
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
return nil, xerrors.Errorf("close: %w", err)
}
if err := rows.Err(); err != nil {
return nil, err
return nil, xerrors.Errorf("rows err: %w", err)
}
return items, nil
}
Expand Down
1 change: 1 addition & 0 deletions coderd/database/models.go

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

29 changes: 20 additions & 9 deletions coderd/database/queries.sql.go

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

3 changes: 2 additions & 1 deletion coderd/database/queries/templates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ SET
default_ttl = $5,
max_ttl = $6,
failure_ttl = $7,
inactivity_ttl = $8
inactivity_ttl = $8,
locked_ttl = $9
WHERE
id = $1
RETURNING
Expand Down
1 change: 1 addition & 0 deletions coderd/database/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ overrides:
failure_ttl: FailureTTL
inactivity_ttl: InactivityTTL
eof: EOF
locked_ttl: LockedTTL

sql:
- schema: "./dump.sql"
Expand Down
10 changes: 7 additions & 3 deletions coderd/schedule/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ type TemplateScheduleOptions struct {
//
// If set, users cannot disable automatic workspace shutdown.
MaxTTL time.Duration `json:"max_ttl"`
// If FailureTTL is set, all failed workspaces will be stopped automatically after this time has elapsed.
// FailureTTL dictates the duration after which failed workspaces will be stopped automatically.
FailureTTL time.Duration `json:"failure_ttl"`
// If InactivityTTL is set, all inactive workspaces will be deleted automatically after this time has elapsed.
// InactivityTTL dictates the duration after which inactive workspaces will be locked.
InactivityTTL time.Duration `json:"inactivity_ttl"`
// LockedTTL dictates the duration after which locked workspaces will be permanently deleted.
LockedTTL time.Duration `json:"locked_ttl"`
}

// TemplateScheduleStore provides an interface for retrieving template
Expand Down Expand Up @@ -51,11 +53,12 @@ func (*agplTemplateScheduleStore) GetTemplateScheduleOptions(ctx context.Context
UserAutostartEnabled: true,
UserAutostopEnabled: true,
DefaultTTL: time.Duration(tpl.DefaultTTL),
// Disregard the values in the database, since MaxTTL, FailureTTL, and InactivityTTL are enterprise
// Disregard the values in the database, since MaxTTL, FailureTTL, InactivityTTL, and LockedTTL are enterprise
// features.
MaxTTL: 0,
FailureTTL: 0,
InactivityTTL: 0,
LockedTTL: 0,
}, nil
}

Expand All @@ -76,5 +79,6 @@ func (*agplTemplateScheduleStore) SetTemplateScheduleOptions(ctx context.Context
MaxTTL: tpl.MaxTTL,
FailureTTL: tpl.FailureTTL,
InactivityTTL: tpl.InactivityTTL,
LockedTTL: tpl.LockedTTL,
})
}
13 changes: 12 additions & 1 deletion coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,12 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
if req.InactivityTTLMillis < 0 {
validErrs = append(validErrs, codersdk.ValidationError{Field: "inactivity_ttl_ms", Detail: "Must be a positive integer."})
}
if req.InactivityTTLMillis < 0 {
validErrs = append(validErrs, codersdk.ValidationError{Field: "inactivity_ttl_ms", Detail: "Must be a positive integer."})
}
if req.LockedTTLMillis < 0 {
validErrs = append(validErrs, codersdk.ValidationError{Field: "locked_ttl_ms", Detail: "Must be a positive integer."})
}

if len(validErrs) > 0 {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Expand All @@ -518,7 +524,8 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
req.DefaultTTLMillis == time.Duration(template.DefaultTTL).Milliseconds() &&
req.MaxTTLMillis == time.Duration(template.MaxTTL).Milliseconds() &&
req.FailureTTLMillis == time.Duration(template.FailureTTL).Milliseconds() &&
req.InactivityTTLMillis == time.Duration(template.InactivityTTL).Milliseconds() {
req.InactivityTTLMillis == time.Duration(template.InactivityTTL).Milliseconds() &&
req.FailureTTLMillis == time.Duration(template.LockedTTL).Milliseconds() {
return nil
}

Expand Down Expand Up @@ -546,11 +553,13 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
maxTTL := time.Duration(req.MaxTTLMillis) * time.Millisecond
failureTTL := time.Duration(req.FailureTTLMillis) * time.Millisecond
inactivityTTL := time.Duration(req.InactivityTTLMillis) * time.Millisecond
lockedTTL := time.Duration(req.LockedTTLMillis) * time.Millisecond

if defaultTTL != time.Duration(template.DefaultTTL) ||
maxTTL != time.Duration(template.MaxTTL) ||
failureTTL != time.Duration(template.FailureTTL) ||
inactivityTTL != time.Duration(template.InactivityTTL) ||
lockedTTL != time.Duration(template.LockedTTL) ||
req.AllowUserAutostart != template.AllowUserAutostart ||
req.AllowUserAutostop != template.AllowUserAutostop {
updated, err = (*api.TemplateScheduleStore.Load()).SetTemplateScheduleOptions(ctx, tx, updated, schedule.TemplateScheduleOptions{
Expand All @@ -563,6 +572,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
MaxTTL: maxTTL,
FailureTTL: failureTTL,
InactivityTTL: inactivityTTL,
LockedTTL: lockedTTL,
})
if err != nil {
return xerrors.Errorf("set template schedule options: %w", err)
Expand Down Expand Up @@ -716,5 +726,6 @@ func (api *API) convertTemplate(
AllowUserCancelWorkspaceJobs: template.AllowUserCancelWorkspaceJobs,
FailureTTLMillis: time.Duration(template.FailureTTL).Milliseconds(),
InactivityTTLMillis: time.Duration(template.InactivityTTL).Milliseconds(),
LockedTTLMillis: time.Duration(template.LockedTTL).Milliseconds(),
}
}
Loading