Skip to content

Commit a78f0fc

Browse files
refactor: use specific error for agpl and prebuilds (coder#17591)
Follow-up PR to coder#17458 Addresses this discussion: coder#17458 (comment)
1 parent 1da27a1 commit a78f0fc

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

coderd/prebuilds/api.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
"github.com/coder/coder/v2/coderd/database"
1010
)
1111

12-
var ErrNoClaimablePrebuiltWorkspaces = xerrors.New("no claimable prebuilt workspaces found")
12+
var (
13+
ErrNoClaimablePrebuiltWorkspaces = xerrors.New("no claimable prebuilt workspaces found")
14+
ErrAGPLDoesNotSupportPrebuiltWorkspaces = xerrors.New("prebuilt workspaces functionality is not supported under the AGPL license")
15+
)
1316

1417
// ReconciliationOrchestrator manages the lifecycle of prebuild reconciliation.
1518
// It runs a continuous loop to check and reconcile prebuild states, and can be stopped gracefully.

coderd/prebuilds/noop.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type NoopClaimer struct{}
2727

2828
func (NoopClaimer) Claim(context.Context, uuid.UUID, string, uuid.UUID) (*uuid.UUID, error) {
2929
// Not entitled to claim prebuilds in AGPL version.
30-
return nil, ErrNoClaimablePrebuiltWorkspaces
30+
return nil, ErrAGPLDoesNotSupportPrebuiltWorkspaces
3131
}
3232

3333
func (NoopClaimer) Initiator() uuid.UUID {

coderd/workspaces.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,28 @@ func createWorkspace(
650650
if req.TemplateVersionPresetID != uuid.Nil {
651651
// Try and claim an eligible prebuild, if available.
652652
claimedWorkspace, err = claimPrebuild(ctx, prebuildsClaimer, db, api.Logger, req, owner)
653-
if err != nil && !errors.Is(err, prebuilds.ErrNoClaimablePrebuiltWorkspaces) {
654-
return xerrors.Errorf("claim prebuild: %w", err)
653+
// If claiming fails with an expected error (no claimable prebuilds or AGPL does not support prebuilds),
654+
// we fall back to creating a new workspace. Otherwise, propagate the unexpected error.
655+
if err != nil {
656+
isExpectedError := errors.Is(err, prebuilds.ErrNoClaimablePrebuiltWorkspaces) ||
657+
errors.Is(err, prebuilds.ErrAGPLDoesNotSupportPrebuiltWorkspaces)
658+
fields := []any{
659+
slog.Error(err),
660+
slog.F("workspace_name", req.Name),
661+
slog.F("template_version_preset_id", req.TemplateVersionPresetID),
662+
}
663+
664+
if !isExpectedError {
665+
// if it's an unexpected error - use error log level
666+
api.Logger.Error(ctx, "failed to claim prebuilt workspace", fields...)
667+
668+
return xerrors.Errorf("failed to claim prebuilt workspace: %w", err)
669+
}
670+
671+
// if it's an expected error - use warn log level
672+
api.Logger.Warn(ctx, "failed to claim prebuilt workspace", fields...)
673+
674+
// fall back to creating a new workspace
655675
}
656676
}
657677

enterprise/coderd/prebuilds/claim_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ func TestClaimPrebuild(t *testing.T) {
111111
markPrebuildsClaimable: true,
112112
claimingErr: agplprebuilds.ErrNoClaimablePrebuiltWorkspaces,
113113
},
114+
"AGPL does not support prebuilds error is returned": {
115+
expectPrebuildClaimed: false,
116+
markPrebuildsClaimable: true,
117+
claimingErr: agplprebuilds.ErrAGPLDoesNotSupportPrebuiltWorkspaces,
118+
},
114119
"unexpected claiming error is returned": {
115120
expectPrebuildClaimed: false,
116121
markPrebuildsClaimable: true,
@@ -224,8 +229,11 @@ func TestClaimPrebuild(t *testing.T) {
224229
TemplateVersionPresetID: presets[0].ID,
225230
})
226231

232+
isNoPrebuiltWorkspaces := errors.Is(tc.claimingErr, agplprebuilds.ErrNoClaimablePrebuiltWorkspaces)
233+
isUnsupported := errors.Is(tc.claimingErr, agplprebuilds.ErrAGPLDoesNotSupportPrebuiltWorkspaces)
234+
227235
switch {
228-
case tc.claimingErr != nil && errors.Is(tc.claimingErr, agplprebuilds.ErrNoClaimablePrebuiltWorkspaces):
236+
case tc.claimingErr != nil && (isNoPrebuiltWorkspaces || isUnsupported):
229237
require.NoError(t, err)
230238
coderdtest.AwaitWorkspaceBuildJobCompleted(t, userClient, userWorkspace.LatestBuild.ID)
231239

0 commit comments

Comments
 (0)