Skip to content

Commit 1c53074

Browse files
refactor: improve documentation
1 parent 0882626 commit 1c53074

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

enterprise/coderd/prebuilds/reconcile.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,22 @@ func (c *StoreReconciler) ReconcilePreset(ctx context.Context, ps prebuilds.Pres
361361
slog.F("preset_name", ps.Preset.Name),
362362
)
363363

364-
if !ps.Preset.Deleted && ps.Preset.UsingActiveVersion {
365-
c.metrics.trackHardLimitedStatus(ps.Preset.OrganizationName, ps.Preset.TemplateName, ps.Preset.Name, ps.IsHardLimited)
366-
}
364+
// Report a preset as hard-limited only if all the following conditions are met:
365+
// - The preset is marked as hard-limited
366+
// - The preset is using the active version of its template, and the template has not been deleted
367+
//
368+
// The second condition is important because a hard-limited preset that has become outdated is no longer relevant.
369+
// Its associated prebuilt workspaces were likely deleted, and it's not meaningful to continue reporting it
370+
// as hard-limited to the admin.
371+
reportAsHardLimited := ps.IsHardLimited && ps.Preset.UsingActiveVersion && !ps.Preset.Deleted
372+
c.metrics.trackHardLimitedStatus(ps.Preset.OrganizationName, ps.Preset.TemplateName, ps.Preset.Name, reportAsHardLimited)
367373

368374
// If the preset reached the hard failure limit for the first time during this iteration:
369375
// - Mark it as hard-limited in the database
370376
// - Send notifications to template admins
377+
// - Continue execution, we disallow only creation operation for hard-limited presets. Deletion is allowed.
371378
if ps.Preset.PrebuildStatus != database.PrebuildStatusHardLimited && ps.IsHardLimited {
372-
logger.Warn(ctx, "skipping hard limited preset")
379+
logger.Warn(ctx, "preset is hard limited, notifying template admins")
373380

374381
err := c.store.UpdatePresetPrebuildStatus(ctx, database.UpdatePresetPrebuildStatusParams{
375382
Status: database.PrebuildStatusHardLimited,
@@ -447,12 +454,11 @@ func (c *StoreReconciler) ReconcilePreset(ctx context.Context, ps prebuilds.Pres
447454
actions.Create = desired
448455
}
449456

450-
if actions.Create > 0 {
451-
// If the preset is hard-limited, log it and exit early.
452-
if ps.Preset.PrebuildStatus == database.PrebuildStatusHardLimited || ps.IsHardLimited {
453-
logger.Warn(ctx, "skipping hard limited preset")
454-
return nil
455-
}
457+
// If preset is hard-limited, and it's a create operation, log it and exit early.
458+
// Creation operation is disallowed for hard-limited preset.
459+
if ps.IsHardLimited && actions.Create > 0 {
460+
logger.Warn(ctx, "skipping hard limited preset for create operation")
461+
return nil
456462
}
457463

458464
var multiErr multierror.Error

enterprise/coderd/prebuilds/reconcile_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,18 @@ func TestHardLimitedPresetShouldNotBlockDeletion(t *testing.T) {
10181018
// Make sure that successfully created, but outdated prebuilt workspace was scheduled for deletion.
10191019
require.Equal(t, database.WorkspaceTransitionDelete, workspaceBuilds[0].Transition)
10201020
require.Equal(t, database.WorkspaceTransitionStart, workspaceBuilds[1].Transition)
1021+
1022+
// Metric is reset to zero after preset became outdated.
1023+
mf, err = registry.Gather()
1024+
require.NoError(t, err)
1025+
metric = findMetric(mf, prebuilds.MetricPresetHardLimitedGauge, map[string]string{
1026+
"template_name": template.Name,
1027+
"preset_name": preset.Name,
1028+
"org_name": org.Name,
1029+
})
1030+
require.NotNil(t, metric)
1031+
require.NotNil(t, metric.GetGauge())
1032+
require.EqualValues(t, 0, metric.GetGauge().GetValue())
10211033
})
10221034
}
10231035
}

0 commit comments

Comments
 (0)