Skip to content

Commit b0a025e

Browse files
refactor: use specific error for agpl and prebuilds
1 parent df47c30 commit b0a025e

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-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+
ErrAGPLDoesNotSupportPrebuilds = xerrors.New("prebuild-related 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, ErrAGPLDoesNotSupportPrebuilds
3131
}
3232

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

coderd/workspaces.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,11 @@ 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 err != nil &&
654+
!errors.Is(err, prebuilds.ErrNoClaimablePrebuiltWorkspaces) &&
655+
!errors.Is(err, prebuilds.ErrAGPLDoesNotSupportPrebuilds) {
656+
657+
return xerrors.Errorf("failed to claim prebuilt workspace: %w", err)
655658
}
656659
}
657660

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.ErrAGPLDoesNotSupportPrebuilds,
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.ErrAGPLDoesNotSupportPrebuilds)
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)