mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: allow templates to specify max_ttl or autostop_requirement (#10920)
This commit is contained in:
@@ -112,13 +112,12 @@ func CalculateAutostop(ctx context.Context, params CalculateAutostopParams) (Aut
|
||||
|
||||
// Use the old algorithm for calculating max_deadline if the instance isn't
|
||||
// configured or entitled to use the new feature flag yet.
|
||||
// TODO(@dean): remove this once the feature flag is enabled for all
|
||||
if !templateSchedule.UseAutostopRequirement && templateSchedule.MaxTTL > 0 {
|
||||
if templateSchedule.UseMaxTTL && templateSchedule.MaxTTL > 0 {
|
||||
autostop.MaxDeadline = now.Add(templateSchedule.MaxTTL)
|
||||
}
|
||||
|
||||
// TODO(@dean): remove extra conditional
|
||||
if templateSchedule.UseAutostopRequirement && templateSchedule.AutostopRequirement.DaysOfWeek != 0 {
|
||||
// Otherwise, use the autostop_requirement algorithm.
|
||||
if !templateSchedule.UseMaxTTL && templateSchedule.AutostopRequirement.DaysOfWeek != 0 {
|
||||
// The template has a autostop requirement, so determine the max deadline
|
||||
// of this workspace build.
|
||||
|
||||
@@ -130,8 +129,8 @@ func CalculateAutostop(ctx context.Context, params CalculateAutostopParams) (Aut
|
||||
}
|
||||
|
||||
// If the schedule is nil, that means the deployment isn't entitled to
|
||||
// use quiet hours or the default schedule has not been set. In this
|
||||
// case, do not set a max deadline on the workspace.
|
||||
// use quiet hours. In this case, do not set a max deadline on the
|
||||
// workspace.
|
||||
if userQuietHoursSchedule.Schedule != nil {
|
||||
loc := userQuietHoursSchedule.Schedule.Location()
|
||||
now := now.In(loc)
|
||||
|
||||
@@ -415,12 +415,12 @@ func TestCalculateAutoStop(t *testing.T) {
|
||||
templateScheduleStore := schedule.MockTemplateScheduleStore{
|
||||
GetFn: func(_ context.Context, _ database.Store, _ uuid.UUID) (schedule.TemplateScheduleOptions, error) {
|
||||
return schedule.TemplateScheduleOptions{
|
||||
UserAutostartEnabled: false,
|
||||
UserAutostopEnabled: c.templateAllowAutostop,
|
||||
DefaultTTL: c.templateDefaultTTL,
|
||||
MaxTTL: c.templateMaxTTL,
|
||||
UseAutostopRequirement: !c.useMaxTTL,
|
||||
AutostopRequirement: c.templateAutostopRequirement,
|
||||
UserAutostartEnabled: false,
|
||||
UserAutostopEnabled: c.templateAllowAutostop,
|
||||
DefaultTTL: c.templateDefaultTTL,
|
||||
MaxTTL: c.templateMaxTTL,
|
||||
UseMaxTTL: c.useMaxTTL,
|
||||
AutostopRequirement: c.templateAutostopRequirement,
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
|
||||
@@ -117,14 +117,12 @@ type TemplateScheduleOptions struct {
|
||||
UserAutostartEnabled bool `json:"user_autostart_enabled"`
|
||||
UserAutostopEnabled bool `json:"user_autostop_enabled"`
|
||||
DefaultTTL time.Duration `json:"default_ttl"`
|
||||
// TODO(@dean): remove MaxTTL once autostop_requirement is matured and the
|
||||
// default
|
||||
MaxTTL time.Duration `json:"max_ttl"`
|
||||
// UseAutostopRequirement dictates whether the autostop requirement should
|
||||
// be used instead of MaxTTL. This is governed by the feature flag and
|
||||
// licensing.
|
||||
MaxTTL time.Duration `json:"max_ttl"`
|
||||
// UseMaxTTL dictates whether the max_ttl should be used instead of
|
||||
// autostop_requirement for this template. This is governed by the template
|
||||
// and licensing.
|
||||
// TODO(@dean): remove this when we remove max_tll
|
||||
UseAutostopRequirement bool
|
||||
UseMaxTTL bool
|
||||
// AutostopRequirement dictates when the workspace must be restarted. This
|
||||
// used to be handled by MaxTTL.
|
||||
AutostopRequirement TemplateAutostopRequirement `json:"autostop_requirement"`
|
||||
@@ -185,8 +183,8 @@ func (*agplTemplateScheduleStore) Get(ctx context.Context, db database.Store, te
|
||||
DefaultTTL: time.Duration(tpl.DefaultTTL),
|
||||
// Disregard the values in the database, since AutostopRequirement,
|
||||
// FailureTTL, TimeTilDormant, and TimeTilDormantAutoDelete are enterprise features.
|
||||
UseAutostopRequirement: false,
|
||||
MaxTTL: 0,
|
||||
UseMaxTTL: false,
|
||||
MaxTTL: 0,
|
||||
AutostartRequirement: TemplateAutostartRequirement{
|
||||
// Default to allowing all days for AGPL
|
||||
DaysOfWeek: 0b01111111,
|
||||
@@ -220,6 +218,7 @@ func (*agplTemplateScheduleStore) Set(ctx context.Context, db database.Store, tp
|
||||
DefaultTTL: int64(opts.DefaultTTL),
|
||||
// Don't allow changing these settings, but keep the value in the DB (to
|
||||
// avoid clearing settings if the license has an issue).
|
||||
UseMaxTtl: tpl.UseMaxTtl,
|
||||
MaxTTL: tpl.MaxTTL,
|
||||
AutostopRequirementDaysOfWeek: tpl.AutostopRequirementDaysOfWeek,
|
||||
AutostopRequirementWeeks: tpl.AutostopRequirementWeeks,
|
||||
|
||||
Reference in New Issue
Block a user