Skip to content

feat: allow templates to specify max_ttl or autostop_requirement #10920

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 10 commits into from
Dec 15, 2023
12 changes: 6 additions & 6 deletions cli/testdata/coder_server_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -447,15 +447,15 @@ USER QUIET HOURS SCHEDULE OPTIONS:
Allow users to set quiet hours schedules each day for workspaces to avoid
workspaces stopping during the day due to template max TTL.

--default-quiet-hours-schedule string, $CODER_QUIET_HOURS_DEFAULT_SCHEDULE
--default-quiet-hours-schedule string, $CODER_QUIET_HOURS_DEFAULT_SCHEDULE (default: CRON_TZ=UTC 0 0 * * *)
The default daily cron schedule applied to users that haven't set a
custom quiet hours schedule themselves. The quiet hours schedule
determines when workspaces will be force stopped due to the template's
max TTL, and will round the max TTL up to be within the user's quiet
hours window (or default). The format is the same as the standard cron
format, but the day-of-month, month and day-of-week must be *. Only
one hour and minute can be specified (ranges or comma separated values
are not supported).
autostop requirement, and will round the max deadline up to be within
the user's quiet hours window (or default). The format is the same as
the standard cron format, but the day-of-month, month and day-of-week
must be *. Only one hour and minute can be specified (ranges or comma
separated values are not supported).

⚠️ DANGEROUS OPTIONS:
--dangerous-allow-path-app-sharing bool, $CODER_DANGEROUS_ALLOW_PATH_APP_SHARING
Expand Down
14 changes: 7 additions & 7 deletions cli/testdata/server-config.yaml.golden
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,10 @@ wgtunnelHost: ""
userQuietHoursSchedule:
# The default daily cron schedule applied to users that haven't set a custom quiet
# hours schedule themselves. The quiet hours schedule determines when workspaces
# will be force stopped due to the template's max TTL, and will round the max TTL
# up to be within the user's quiet hours window (or default). The format is the
# same as the standard cron format, but the day-of-month, month and day-of-week
# must be *. Only one hour and minute can be specified (ranges or comma separated
# values are not supported).
# (default: <unset>, type: string)
defaultQuietHoursSchedule: ""
# will be force stopped due to the template's autostop requirement, and will round
# the max deadline up to be within the user's quiet hours window (or default). The
# format is the same as the standard cron format, but the day-of-month, month and
# day-of-week must be *. Only one hour and minute can be specified (ranges or
# comma separated values are not supported).
# (default: CRON_TZ=UTC 0 0 * * *, type: string)
defaultQuietHoursSchedule: CRON_TZ=UTC 0 0 * * *
10 changes: 6 additions & 4 deletions coderd/apidoc/docs.go

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

10 changes: 6 additions & 4 deletions coderd/apidoc/swagger.json

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

1 change: 1 addition & 0 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -6158,6 +6158,7 @@ func (q *FakeQuerier) UpdateTemplateScheduleByID(_ context.Context, arg database
tpl.AllowUserAutostop = arg.AllowUserAutostop
tpl.UpdatedAt = dbtime.Now()
tpl.DefaultTTL = arg.DefaultTTL
tpl.UseMaxTtl = arg.UseMaxTtl
tpl.MaxTTL = arg.MaxTTL
tpl.AutostopRequirementDaysOfWeek = arg.AutostopRequirementDaysOfWeek
tpl.AutostopRequirementWeeks = arg.AutostopRequirementWeeks
Expand Down
4 changes: 3 additions & 1 deletion coderd/database/dump.sql

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

19 changes: 19 additions & 0 deletions coderd/database/migrations/000180_template_use_max_ttl.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
DROP VIEW template_with_users;

ALTER TABLE templates DROP COLUMN use_max_ttl;

CREATE VIEW
template_with_users
AS
SELECT
templates.*,
coalesce(visible_users.avatar_url, '') AS created_by_avatar_url,
coalesce(visible_users.username, '') AS created_by_username
FROM
templates
LEFT JOIN
visible_users
ON
templates.created_by = visible_users.id;

COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.';
28 changes: 28 additions & 0 deletions coderd/database/migrations/000180_template_use_max_ttl.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- Add column with default true, so existing templates will function as usual
ALTER TABLE templates ADD COLUMN use_max_ttl boolean NOT NULL DEFAULT true;

-- Find any templates with autostop_requirement_days_of_week set and set them to
-- use_max_ttl = false
UPDATE templates SET use_max_ttl = false WHERE autostop_requirement_days_of_week != 0;

-- Alter column to default false, because we want autostop_requirement to be the
-- default from now on
ALTER TABLE templates ALTER COLUMN use_max_ttl SET DEFAULT false;

DROP VIEW template_with_users;

CREATE VIEW
template_with_users
AS
SELECT
templates.*,
coalesce(visible_users.avatar_url, '') AS created_by_avatar_url,
coalesce(visible_users.username, '') AS created_by_username
FROM
templates
LEFT JOIN
visible_users
ON
templates.created_by = visible_users.id;

COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.';
1 change: 1 addition & 0 deletions coderd/database/modelqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (q *sqlQuerier) GetAuthorizedTemplates(ctx context.Context, arg GetTemplate
&i.AutostartBlockDaysOfWeek,
&i.RequireActiveVersion,
&i.Deprecated,
&i.UseMaxTtl,
&i.CreatedByAvatarURL,
&i.CreatedByUsername,
); err != nil {
Expand Down
2 changes: 2 additions & 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: 18 additions & 11 deletions coderd/database/queries.sql.go

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

15 changes: 8 additions & 7 deletions coderd/database/queries/templates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,14 @@ SET
allow_user_autostart = $3,
allow_user_autostop = $4,
default_ttl = $5,
max_ttl = $6,
autostop_requirement_days_of_week = $7,
autostop_requirement_weeks = $8,
autostart_block_days_of_week = $9,
failure_ttl = $10,
time_til_dormant = $11,
time_til_dormant_autodelete = $12
use_max_ttl = $6,
max_ttl = $7,
autostop_requirement_days_of_week = $8,
autostop_requirement_weeks = $9,
autostart_block_days_of_week = $10,
failure_ttl = $11,
time_til_dormant = $12,
time_til_dormant_autodelete = $13
WHERE
id = $1
;
Expand Down
Loading