Skip to content

Commit 0a94405

Browse files
committed
Move prebuilds code to enterprise top-level package, refactor into agpl pointers
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 7498980 commit 0a94405

File tree

9 files changed

+73
-20
lines changed

9 files changed

+73
-20
lines changed

coderd/coderd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"sync/atomic"
2020
"time"
2121

22+
"github.com/coder/coder/v2/coderd/prebuilds"
23+
2224
"github.com/andybalholm/brotli"
2325
"github.com/go-chi/chi/v5"
2426
"github.com/go-chi/chi/v5/middleware"
@@ -1476,6 +1478,7 @@ type API struct {
14761478
// passed to dbauthz.
14771479
AccessControlStore *atomic.Pointer[dbauthz.AccessControlStore]
14781480
PortSharer atomic.Pointer[portsharing.PortSharer]
1481+
PrebuildsClaimer atomic.Pointer[prebuilds.Claimer]
14791482

14801483
UpdatesProvider tailnet.WorkspaceUpdatesProvider
14811484

coderd/prebuilds/api.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package prebuilds
2+
3+
import (
4+
"context"
5+
6+
"github.com/google/uuid"
7+
"golang.org/x/xerrors"
8+
9+
"github.com/coder/coder/v2/coderd/database"
10+
)
11+
12+
type Claimer interface {
13+
Claim(ctx context.Context, store database.Store, userID uuid.UUID, name string, presetID uuid.UUID) (*uuid.UUID, error)
14+
Initiator() uuid.UUID
15+
}
16+
17+
type AGPLPrebuildClaimer struct{}
18+
19+
func (c AGPLPrebuildClaimer) Claim(context.Context, database.Store, uuid.UUID, string, uuid.UUID) (*uuid.UUID, error) {
20+
return nil, xerrors.Errorf("not entitled to claim prebuilds")
21+
}
22+
23+
func (c AGPLPrebuildClaimer) Initiator() uuid.UUID {
24+
return uuid.Nil
25+
}
26+
27+
var DefaultClaimer Claimer = AGPLPrebuildClaimer{}

coderd/prebuilds/id.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

coderd/workspaces.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"strconv"
1212
"time"
1313

14-
"github.com/coder/coder/v2/coderd/prebuilds"
15-
1614
"github.com/dustin/go-humanize"
1715
"github.com/go-chi/chi/v5"
1816
"github.com/google/uuid"
@@ -30,6 +28,7 @@ import (
3028
"github.com/coder/coder/v2/coderd/httpapi"
3129
"github.com/coder/coder/v2/coderd/httpmw"
3230
"github.com/coder/coder/v2/coderd/notifications"
31+
"github.com/coder/coder/v2/coderd/prebuilds"
3332
"github.com/coder/coder/v2/coderd/rbac"
3433
"github.com/coder/coder/v2/coderd/rbac/policy"
3534
"github.com/coder/coder/v2/coderd/schedule"
@@ -632,6 +631,9 @@ func createWorkspace(
632631

633632
runningWorkspaceAgentID uuid.UUID
634633
)
634+
635+
prebuilds := (*api.PrebuildsClaimer.Load()).(prebuilds.Claimer)
636+
635637
err = api.Database.InTx(func(db database.Store) error {
636638
var (
637639
workspaceID uuid.UUID
@@ -641,7 +643,7 @@ func createWorkspace(
641643
// If a template preset was chosen, try claim a prebuild.
642644
if req.TemplateVersionPresetID != uuid.Nil {
643645
// Try and claim an eligible prebuild, if available.
644-
claimedWorkspace, err = claimPrebuild(ctx, db, api.Logger, req, owner)
646+
claimedWorkspace, err = claimPrebuild(ctx, prebuilds, db, api.Logger, req, owner)
645647
if err != nil {
646648
return xerrors.Errorf("claim prebuild: %w", err)
647649
}
@@ -674,8 +676,7 @@ func createWorkspace(
674676
} else {
675677
// Prebuild found!
676678
workspaceID = claimedWorkspace.ID
677-
initiatorID = prebuilds.PrebuildOwnerUUID
678-
679+
initiatorID = prebuilds.Initiator()
679680
agents, err := api.Database.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx, claimedWorkspace.ID)
680681
if err != nil {
681682
api.Logger.Error(ctx, "failed to retrieve running agents of claimed prebuilt workspace",
@@ -806,9 +807,9 @@ func createWorkspace(
806807
httpapi.Write(ctx, rw, http.StatusCreated, w)
807808
}
808809

809-
func claimPrebuild(ctx context.Context, db database.Store, logger slog.Logger, req codersdk.CreateWorkspaceRequest, owner workspaceOwner) (*database.Workspace, error) {
810+
func claimPrebuild(ctx context.Context, claimer prebuilds.Claimer, db database.Store, logger slog.Logger, req codersdk.CreateWorkspaceRequest, owner workspaceOwner) (*database.Workspace, error) {
810811
// TODO: authz // Can't use existing profiles (i.e. AsSystemRestricted) because of dbauthz rules
811-
var ownerCtx = dbauthz.As(ctx, rbac.Subject{
812+
ownerCtx := dbauthz.As(ctx, rbac.Subject{
812813
ID: "owner",
813814
Roles: rbac.RoleIdentifiers{rbac.RoleOwner()},
814815
Groups: []string{},
@@ -819,7 +820,7 @@ func claimPrebuild(ctx context.Context, db database.Store, logger slog.Logger, r
819820
claimCtx, cancel := context.WithTimeout(ownerCtx, time.Second*10) // TODO: don't use elevated authz context
820821
defer cancel()
821822

822-
claimedID, err := prebuilds.Claim(claimCtx, db, owner.ID, req.Name, req.TemplateVersionPresetID)
823+
claimedID, err := claimer.Claim(claimCtx, db, owner.ID, req.Name, req.TemplateVersionPresetID)
823824
if err != nil {
824825
// TODO: enhance this by clarifying whether this *specific* prebuild failed or whether there are none to claim.
825826
return nil, xerrors.Errorf("claim prebuild: %w", err)

enterprise/coderd/coderd.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"crypto/ed25519"
66
"fmt"
7-
"github.com/coder/coder/v2/coderd/prebuilds"
87
"math"
98
"net/http"
109
"net/url"
@@ -19,6 +18,7 @@ import (
1918
"github.com/coder/coder/v2/coderd/entitlements"
2019
"github.com/coder/coder/v2/coderd/idpsync"
2120
agplportsharing "github.com/coder/coder/v2/coderd/portsharing"
21+
agplprebuilds "github.com/coder/coder/v2/coderd/prebuilds"
2222
"github.com/coder/coder/v2/coderd/rbac/policy"
2323
"github.com/coder/coder/v2/enterprise/coderd/enidpsync"
2424
"github.com/coder/coder/v2/enterprise/coderd/portsharing"
@@ -44,6 +44,7 @@ import (
4444
"github.com/coder/coder/v2/codersdk"
4545
"github.com/coder/coder/v2/enterprise/coderd/dbauthz"
4646
"github.com/coder/coder/v2/enterprise/coderd/license"
47+
"github.com/coder/coder/v2/enterprise/coderd/prebuilds"
4748
"github.com/coder/coder/v2/enterprise/coderd/proxyhealth"
4849
"github.com/coder/coder/v2/enterprise/coderd/schedule"
4950
"github.com/coder/coder/v2/enterprise/dbcrypt"
@@ -583,6 +584,7 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
583584
go api.runEntitlementsLoop(ctx)
584585

585586
if api.AGPL.Experiments.Enabled(codersdk.ExperimentWorkspacePrebuilds) {
587+
// TODO: future enhancement, start this up without restarting coderd when entitlement is updated.
586588
if !api.Entitlements.Enabled(codersdk.FeatureWorkspacePrebuilds) {
587589
options.Logger.Warn(ctx, "prebuilds experiment enabled but not entitled to use")
588590
} else {
@@ -883,6 +885,14 @@ func (api *API) updateEntitlements(ctx context.Context) error {
883885
api.AGPL.PortSharer.Store(&ps)
884886
}
885887

888+
if initial, changed, enabled := featureChanged(codersdk.FeatureWorkspacePrebuilds); shouldUpdate(initial, changed, enabled) {
889+
c := agplprebuilds.DefaultClaimer
890+
if enabled {
891+
c = prebuilds.EnterpriseClaimer{}
892+
}
893+
api.AGPL.PrebuildsClaimer.Store(&c)
894+
}
895+
886896
// External token encryption is soft-enforced
887897
featureExternalTokenEncryption := reloadedEntitlements.Features[codersdk.FeatureExternalTokenEncryption]
888898
featureExternalTokenEncryption.Enabled = len(api.ExternalTokenEncryption) > 0

coderd/prebuilds/claim.go renamed to enterprise/coderd/prebuilds/claim.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ import (
44
"context"
55
"database/sql"
66
"errors"
7-
"github.com/coder/coder/v2/coderd/database"
7+
8+
"github.com/coder/coder/v2/coderd/prebuilds"
9+
810
"github.com/google/uuid"
911
"golang.org/x/xerrors"
12+
13+
"github.com/coder/coder/v2/coderd/database"
1014
)
1115

12-
func Claim(ctx context.Context, store database.Store, userID uuid.UUID, name string, presetID uuid.UUID) (*uuid.UUID, error) {
16+
type EnterpriseClaimer struct{}
17+
18+
func (e EnterpriseClaimer) Claim(ctx context.Context, store database.Store, userID uuid.UUID, name string, presetID uuid.UUID) (*uuid.UUID, error) {
1319
var prebuildID *uuid.UUID
1420
err := store.InTx(func(db database.Store) error {
1521
// TODO: do we need this?
@@ -44,3 +50,9 @@ func Claim(ctx context.Context, store database.Store, userID uuid.UUID, name str
4450

4551
return prebuildID, err
4652
}
53+
54+
func (e EnterpriseClaimer) Initiator() uuid.UUID {
55+
return ownerID
56+
}
57+
58+
var _ prebuilds.Claimer = &EnterpriseClaimer{}

coderd/prebuilds/controller.go renamed to enterprise/coderd/prebuilds/controller.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func (c *Controller) reconcileTemplate(ctx context.Context, template database.Te
312312
}
313313

314314
// TODO: authz // Can't use existing profiles (i.e. AsSystemRestricted) because of dbauthz rules
315-
var ownerCtx = dbauthz.As(ctx, rbac.Subject{
315+
ownerCtx := dbauthz.As(ctx, rbac.Subject{
316316
ID: "owner",
317317
Roles: rbac.RoleIdentifiers{rbac.RoleOwner()},
318318
Groups: []string{},
@@ -375,7 +375,7 @@ func (c *Controller) createPrebuild(ctx context.Context, db database.Store, preb
375375
ID: prebuildID,
376376
CreatedAt: now,
377377
UpdatedAt: now,
378-
OwnerID: PrebuildOwnerUUID,
378+
OwnerID: ownerID,
379379
OrganizationID: template.OrganizationID,
380380
TemplateID: template.ID,
381381
Name: name,
@@ -397,6 +397,7 @@ func (c *Controller) createPrebuild(ctx context.Context, db database.Store, preb
397397

398398
return c.provision(ctx, db, prebuildID, template, presetID, database.WorkspaceTransitionStart, workspace)
399399
}
400+
400401
func (c *Controller) deletePrebuild(ctx context.Context, db database.Store, prebuildID uuid.UUID, template database.Template, presetID uuid.UUID) error {
401402
workspace, err := db.GetWorkspaceByID(ctx, prebuildID)
402403
if err != nil {
@@ -430,7 +431,7 @@ func (c *Controller) provision(ctx context.Context, db database.Store, prebuildI
430431

431432
builder := wsbuilder.New(workspace, transition).
432433
Reason(database.BuildReasonInitiator).
433-
Initiator(PrebuildOwnerUUID).
434+
Initiator(ownerID).
434435
ActiveVersion().
435436
VersionID(template.ActiveVersionID).
436437
MarkPrebuild().

enterprise/coderd/prebuilds/id.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package prebuilds
2+
3+
import "github.com/google/uuid"
4+
5+
var ownerID = uuid.MustParse("c42fdf75-3097-471c-8c33-fb52454d81c0")

provisioner/terraform/executor.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"context"
77
"encoding/json"
88
"fmt"
9-
"github.com/coder/terraform-provider-coder/provider"
109
"io"
1110
"os"
1211
"os/exec"

0 commit comments

Comments
 (0)