feat: add locked TTL field to template meta (#8020)

This commit is contained in:
Jon Ayers
2023-06-19 22:37:55 -04:00
committed by GitHub
parent 1ecc371ade
commit c3aef9363b
27 changed files with 291 additions and 90 deletions

11
coderd/apidoc/docs.go generated
View File

@ -6684,7 +6684,11 @@ const docTemplate = `{
"type": "string"
},
"inactivity_ttl_ms": {
"description": "InactivityTTLMillis allows optionally specifying the max lifetime before Coder\ndeletes inactive workspaces created from this template.",
"description": "InactivityTTLMillis allows optionally specifying the max lifetime before Coder\nlocks inactive workspaces created from this template.",
"type": "integer"
},
"locked_ttl_ms": {
"description": "LockedTTL allows optionally specifying the max lifetime before Coder\npermanently deletes locked workspaces created from this template.",
"type": "integer"
},
"max_ttl_ms": {
@ -8500,7 +8504,7 @@ const docTemplate = `{
"type": "string"
},
"failure_ttl_ms": {
"description": "FailureTTLMillis and InactivityTTLMillis are enterprise-only. Their\nvalues are used if your license is entitled to use the advanced\ntemplate scheduling feature.",
"description": "FailureTTLMillis, InactivityTTLMillis, and LockedTTLMillis are enterprise-only. Their\nvalues are used if your license is entitled to use the advanced\ntemplate scheduling feature.",
"type": "integer"
},
"icon": {
@ -8513,6 +8517,9 @@ const docTemplate = `{
"inactivity_ttl_ms": {
"type": "integer"
},
"locked_ttl_ms": {
"type": "integer"
},
"max_ttl_ms": {
"description": "MaxTTLMillis is an enterprise feature. It's value is only used if your\nlicense is entitled to use the advanced template scheduling feature.",
"type": "integer"

View File

@ -5955,7 +5955,11 @@
"type": "string"
},
"inactivity_ttl_ms": {
"description": "InactivityTTLMillis allows optionally specifying the max lifetime before Coder\ndeletes inactive workspaces created from this template.",
"description": "InactivityTTLMillis allows optionally specifying the max lifetime before Coder\nlocks inactive workspaces created from this template.",
"type": "integer"
},
"locked_ttl_ms": {
"description": "LockedTTL allows optionally specifying the max lifetime before Coder\npermanently deletes locked workspaces created from this template.",
"type": "integer"
},
"max_ttl_ms": {
@ -7657,7 +7661,7 @@
"type": "string"
},
"failure_ttl_ms": {
"description": "FailureTTLMillis and InactivityTTLMillis are enterprise-only. Their\nvalues are used if your license is entitled to use the advanced\ntemplate scheduling feature.",
"description": "FailureTTLMillis, InactivityTTLMillis, and LockedTTLMillis are enterprise-only. Their\nvalues are used if your license is entitled to use the advanced\ntemplate scheduling feature.",
"type": "integer"
},
"icon": {
@ -7670,6 +7674,9 @@
"inactivity_ttl_ms": {
"type": "integer"
},
"locked_ttl_ms": {
"type": "integer"
},
"max_ttl_ms": {
"description": "MaxTTLMillis is an enterprise feature. It's value is only used if your\nlicense is entitled to use the advanced template scheduling feature.",
"type": "integer"

View File

@ -494,7 +494,8 @@ CREATE TABLE templates (
allow_user_autostart boolean DEFAULT true NOT NULL,
allow_user_autostop boolean DEFAULT true NOT NULL,
failure_ttl bigint DEFAULT 0 NOT NULL,
inactivity_ttl bigint DEFAULT 0 NOT NULL
inactivity_ttl bigint DEFAULT 0 NOT NULL,
locked_ttl bigint DEFAULT 0 NOT NULL
);
COMMENT ON COLUMN templates.default_ttl IS 'The default duration for autostop for workspaces created from this template.';

View File

@ -0,0 +1,3 @@
BEGIN;
ALTER TABLE templates DROP COLUMN locked_ttl;
COMMIT;

View File

@ -0,0 +1,3 @@
BEGIN;
ALTER TABLE templates ADD COLUMN locked_ttl BIGINT NOT NULL DEFAULT 0;
COMMIT;

View File

@ -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
@ -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
}

View File

@ -1561,6 +1561,7 @@ type Template struct {
AllowUserAutostop bool `db:"allow_user_autostop" json:"allow_user_autostop"`
FailureTTL int64 `db:"failure_ttl" json:"failure_ttl"`
InactivityTTL int64 `db:"inactivity_ttl" json:"inactivity_ttl"`
LockedTTL int64 `db:"locked_ttl" json:"locked_ttl"`
}
type TemplateVersion struct {

View File

@ -3240,7 +3240,7 @@ func (q *sqlQuerier) GetTemplateAverageBuildTime(ctx context.Context, arg GetTem
const getTemplateByID = `-- name: GetTemplateByID :one
SELECT
id, created_at, updated_at, organization_id, deleted, name, provisioner, active_version_id, description, default_ttl, created_by, icon, user_acl, group_acl, display_name, allow_user_cancel_workspace_jobs, max_ttl, allow_user_autostart, allow_user_autostop, failure_ttl, inactivity_ttl
id, created_at, updated_at, organization_id, deleted, name, provisioner, active_version_id, description, default_ttl, created_by, icon, user_acl, group_acl, display_name, allow_user_cancel_workspace_jobs, max_ttl, allow_user_autostart, allow_user_autostop, failure_ttl, inactivity_ttl, locked_ttl
FROM
templates
WHERE
@ -3274,13 +3274,14 @@ func (q *sqlQuerier) GetTemplateByID(ctx context.Context, id uuid.UUID) (Templat
&i.AllowUserAutostop,
&i.FailureTTL,
&i.InactivityTTL,
&i.LockedTTL,
)
return i, err
}
const getTemplateByOrganizationAndName = `-- name: GetTemplateByOrganizationAndName :one
SELECT
id, created_at, updated_at, organization_id, deleted, name, provisioner, active_version_id, description, default_ttl, created_by, icon, user_acl, group_acl, display_name, allow_user_cancel_workspace_jobs, max_ttl, allow_user_autostart, allow_user_autostop, failure_ttl, inactivity_ttl
id, created_at, updated_at, organization_id, deleted, name, provisioner, active_version_id, description, default_ttl, created_by, icon, user_acl, group_acl, display_name, allow_user_cancel_workspace_jobs, max_ttl, allow_user_autostart, allow_user_autostop, failure_ttl, inactivity_ttl, locked_ttl
FROM
templates
WHERE
@ -3322,12 +3323,13 @@ func (q *sqlQuerier) GetTemplateByOrganizationAndName(ctx context.Context, arg G
&i.AllowUserAutostop,
&i.FailureTTL,
&i.InactivityTTL,
&i.LockedTTL,
)
return i, err
}
const getTemplates = `-- name: GetTemplates :many
SELECT id, created_at, updated_at, organization_id, deleted, name, provisioner, active_version_id, description, default_ttl, created_by, icon, user_acl, group_acl, display_name, allow_user_cancel_workspace_jobs, max_ttl, allow_user_autostart, allow_user_autostop, failure_ttl, inactivity_ttl FROM templates
SELECT id, created_at, updated_at, organization_id, deleted, name, provisioner, active_version_id, description, default_ttl, created_by, icon, user_acl, group_acl, display_name, allow_user_cancel_workspace_jobs, max_ttl, allow_user_autostart, allow_user_autostop, failure_ttl, inactivity_ttl, locked_ttl FROM templates
ORDER BY (name, id) ASC
`
@ -3362,6 +3364,7 @@ func (q *sqlQuerier) GetTemplates(ctx context.Context) ([]Template, error) {
&i.AllowUserAutostop,
&i.FailureTTL,
&i.InactivityTTL,
&i.LockedTTL,
); err != nil {
return nil, err
}
@ -3378,7 +3381,7 @@ func (q *sqlQuerier) GetTemplates(ctx context.Context) ([]Template, error) {
const getTemplatesWithFilter = `-- name: GetTemplatesWithFilter :many
SELECT
id, created_at, updated_at, organization_id, deleted, name, provisioner, active_version_id, description, default_ttl, created_by, icon, user_acl, group_acl, display_name, allow_user_cancel_workspace_jobs, max_ttl, allow_user_autostart, allow_user_autostop, failure_ttl, inactivity_ttl
id, created_at, updated_at, organization_id, deleted, name, provisioner, active_version_id, description, default_ttl, created_by, icon, user_acl, group_acl, display_name, allow_user_cancel_workspace_jobs, max_ttl, allow_user_autostart, allow_user_autostop, failure_ttl, inactivity_ttl, locked_ttl
FROM
templates
WHERE
@ -3450,6 +3453,7 @@ func (q *sqlQuerier) GetTemplatesWithFilter(ctx context.Context, arg GetTemplate
&i.AllowUserAutostop,
&i.FailureTTL,
&i.InactivityTTL,
&i.LockedTTL,
); err != nil {
return nil, err
}
@ -3483,7 +3487,7 @@ INSERT INTO
allow_user_cancel_workspace_jobs
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) RETURNING id, created_at, updated_at, organization_id, deleted, name, provisioner, active_version_id, description, default_ttl, created_by, icon, user_acl, group_acl, display_name, allow_user_cancel_workspace_jobs, max_ttl, allow_user_autostart, allow_user_autostop, failure_ttl, inactivity_ttl
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) RETURNING id, created_at, updated_at, organization_id, deleted, name, provisioner, active_version_id, description, default_ttl, created_by, icon, user_acl, group_acl, display_name, allow_user_cancel_workspace_jobs, max_ttl, allow_user_autostart, allow_user_autostop, failure_ttl, inactivity_ttl, locked_ttl
`
type InsertTemplateParams struct {
@ -3543,6 +3547,7 @@ func (q *sqlQuerier) InsertTemplate(ctx context.Context, arg InsertTemplateParam
&i.AllowUserAutostop,
&i.FailureTTL,
&i.InactivityTTL,
&i.LockedTTL,
)
return i, err
}
@ -3556,7 +3561,7 @@ SET
WHERE
id = $3
RETURNING
id, created_at, updated_at, organization_id, deleted, name, provisioner, active_version_id, description, default_ttl, created_by, icon, user_acl, group_acl, display_name, allow_user_cancel_workspace_jobs, max_ttl, allow_user_autostart, allow_user_autostop, failure_ttl, inactivity_ttl
id, created_at, updated_at, organization_id, deleted, name, provisioner, active_version_id, description, default_ttl, created_by, icon, user_acl, group_acl, display_name, allow_user_cancel_workspace_jobs, max_ttl, allow_user_autostart, allow_user_autostop, failure_ttl, inactivity_ttl, locked_ttl
`
type UpdateTemplateACLByIDParams struct {
@ -3590,6 +3595,7 @@ func (q *sqlQuerier) UpdateTemplateACLByID(ctx context.Context, arg UpdateTempla
&i.AllowUserAutostop,
&i.FailureTTL,
&i.InactivityTTL,
&i.LockedTTL,
)
return i, err
}
@ -3649,7 +3655,7 @@ SET
WHERE
id = $1
RETURNING
id, created_at, updated_at, organization_id, deleted, name, provisioner, active_version_id, description, default_ttl, created_by, icon, user_acl, group_acl, display_name, allow_user_cancel_workspace_jobs, max_ttl, allow_user_autostart, allow_user_autostop, failure_ttl, inactivity_ttl
id, created_at, updated_at, organization_id, deleted, name, provisioner, active_version_id, description, default_ttl, created_by, icon, user_acl, group_acl, display_name, allow_user_cancel_workspace_jobs, max_ttl, allow_user_autostart, allow_user_autostop, failure_ttl, inactivity_ttl, locked_ttl
`
type UpdateTemplateMetaByIDParams struct {
@ -3695,6 +3701,7 @@ func (q *sqlQuerier) UpdateTemplateMetaByID(ctx context.Context, arg UpdateTempl
&i.AllowUserAutostop,
&i.FailureTTL,
&i.InactivityTTL,
&i.LockedTTL,
)
return i, err
}
@ -3709,11 +3716,12 @@ SET
default_ttl = $5,
max_ttl = $6,
failure_ttl = $7,
inactivity_ttl = $8
inactivity_ttl = $8,
locked_ttl = $9
WHERE
id = $1
RETURNING
id, created_at, updated_at, organization_id, deleted, name, provisioner, active_version_id, description, default_ttl, created_by, icon, user_acl, group_acl, display_name, allow_user_cancel_workspace_jobs, max_ttl, allow_user_autostart, allow_user_autostop, failure_ttl, inactivity_ttl
id, created_at, updated_at, organization_id, deleted, name, provisioner, active_version_id, description, default_ttl, created_by, icon, user_acl, group_acl, display_name, allow_user_cancel_workspace_jobs, max_ttl, allow_user_autostart, allow_user_autostop, failure_ttl, inactivity_ttl, locked_ttl
`
type UpdateTemplateScheduleByIDParams struct {
@ -3725,6 +3733,7 @@ type UpdateTemplateScheduleByIDParams struct {
MaxTTL int64 `db:"max_ttl" json:"max_ttl"`
FailureTTL int64 `db:"failure_ttl" json:"failure_ttl"`
InactivityTTL int64 `db:"inactivity_ttl" json:"inactivity_ttl"`
LockedTTL int64 `db:"locked_ttl" json:"locked_ttl"`
}
func (q *sqlQuerier) UpdateTemplateScheduleByID(ctx context.Context, arg UpdateTemplateScheduleByIDParams) (Template, error) {
@ -3737,6 +3746,7 @@ func (q *sqlQuerier) UpdateTemplateScheduleByID(ctx context.Context, arg UpdateT
arg.MaxTTL,
arg.FailureTTL,
arg.InactivityTTL,
arg.LockedTTL,
)
var i Template
err := row.Scan(
@ -3761,6 +3771,7 @@ func (q *sqlQuerier) UpdateTemplateScheduleByID(ctx context.Context, arg UpdateT
&i.AllowUserAutostop,
&i.FailureTTL,
&i.InactivityTTL,
&i.LockedTTL,
)
return i, err
}

View File

@ -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

View File

@ -56,6 +56,7 @@ overrides:
failure_ttl: FailureTTL
inactivity_ttl: InactivityTTL
eof: EOF
locked_ttl: LockedTTL
sql:
- schema: "./dump.sql"

View File

@ -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
@ -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
}
@ -76,5 +79,6 @@ func (*agplTemplateScheduleStore) SetTemplateScheduleOptions(ctx context.Context
MaxTTL: tpl.MaxTTL,
FailureTTL: tpl.FailureTTL,
InactivityTTL: tpl.InactivityTTL,
LockedTTL: tpl.LockedTTL,
})
}

View File

@ -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{
@ -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
}
@ -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{
@ -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)
@ -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(),
}
}

View File

@ -635,6 +635,7 @@ func TestPatchTemplateMeta(t *testing.T) {
const (
failureTTL = 7 * 24 * time.Hour
inactivityTTL = 180 * 24 * time.Hour
lockedTTL = 360 * 24 * time.Hour
)
t.Run("OK", func(t *testing.T) {
@ -647,9 +648,11 @@ func TestPatchTemplateMeta(t *testing.T) {
if atomic.AddInt64(&setCalled, 1) == 2 {
require.Equal(t, failureTTL, options.FailureTTL)
require.Equal(t, inactivityTTL, options.InactivityTTL)
require.Equal(t, lockedTTL, options.LockedTTL)
}
template.FailureTTL = int64(options.FailureTTL)
template.InactivityTTL = int64(options.InactivityTTL)
template.LockedTTL = int64(options.LockedTTL)
return template, nil
},
},
@ -659,6 +662,7 @@ func TestPatchTemplateMeta(t *testing.T) {
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
ctr.FailureTTLMillis = ptr.Ref(0 * time.Hour.Milliseconds())
ctr.InactivityTTLMillis = ptr.Ref(0 * time.Hour.Milliseconds())
ctr.LockedTTL = ptr.Ref(0 * time.Hour.Milliseconds())
})
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
@ -674,12 +678,14 @@ func TestPatchTemplateMeta(t *testing.T) {
AllowUserCancelWorkspaceJobs: template.AllowUserCancelWorkspaceJobs,
FailureTTLMillis: failureTTL.Milliseconds(),
InactivityTTLMillis: inactivityTTL.Milliseconds(),
LockedTTLMillis: lockedTTL.Milliseconds(),
})
require.NoError(t, err)
require.EqualValues(t, 2, atomic.LoadInt64(&setCalled))
require.Equal(t, failureTTL.Milliseconds(), got.FailureTTLMillis)
require.Equal(t, inactivityTTL.Milliseconds(), got.InactivityTTLMillis)
require.Equal(t, lockedTTL.Milliseconds(), got.LockedTTLMillis)
})
t.Run("IgnoredUnlicensed", func(t *testing.T) {
@ -691,6 +697,7 @@ func TestPatchTemplateMeta(t *testing.T) {
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
ctr.FailureTTLMillis = ptr.Ref(0 * time.Hour.Milliseconds())
ctr.InactivityTTLMillis = ptr.Ref(0 * time.Hour.Milliseconds())
ctr.LockedTTL = ptr.Ref(0 * time.Hour.Milliseconds())
})
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
@ -706,10 +713,12 @@ func TestPatchTemplateMeta(t *testing.T) {
AllowUserCancelWorkspaceJobs: template.AllowUserCancelWorkspaceJobs,
FailureTTLMillis: failureTTL.Milliseconds(),
InactivityTTLMillis: inactivityTTL.Milliseconds(),
LockedTTLMillis: lockedTTL.Milliseconds(),
})
require.NoError(t, err)
require.Zero(t, got.FailureTTLMillis)
require.Zero(t, got.InactivityTTLMillis)
require.Zero(t, got.LockedTTLMillis)
})
})