Skip to content

Commit b151e1b

Browse files
committed
Last little bits to make everything work
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 3a9f9c8 commit b151e1b

File tree

4 files changed

+58
-33
lines changed

4 files changed

+58
-33
lines changed

coderd/prebuilds/controller.go

+26-9
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,30 @@ import (
55
"crypto/rand"
66
"encoding/base32"
77
"fmt"
8+
"math"
9+
mrand "math/rand"
10+
"strings"
11+
"time"
12+
13+
"golang.org/x/exp/slices"
14+
815
"github.com/coder/coder/v2/coderd/audit"
916
"github.com/coder/coder/v2/coderd/database/dbtime"
1017
"github.com/coder/coder/v2/coderd/database/provisionerjobs"
1118
"github.com/coder/coder/v2/coderd/database/pubsub"
1219
"github.com/coder/coder/v2/coderd/rbac"
1320
"github.com/coder/coder/v2/coderd/rbac/policy"
1421
"github.com/coder/coder/v2/coderd/wsbuilder"
15-
"golang.org/x/exp/slices"
16-
"math"
17-
mrand "math/rand"
18-
"strings"
19-
"time"
22+
"github.com/coder/coder/v2/codersdk"
2023

2124
"cdr.dev/slog"
2225

23-
"github.com/coder/coder/v2/coderd/database"
24-
"github.com/coder/coder/v2/coderd/database/dbauthz"
2526
"github.com/google/uuid"
2627
"golang.org/x/sync/errgroup"
2728
"golang.org/x/xerrors"
29+
30+
"github.com/coder/coder/v2/coderd/database"
31+
"github.com/coder/coder/v2/coderd/database/dbauthz"
2832
)
2933

3034
type Controller struct {
@@ -395,13 +399,26 @@ func (c Controller) deletePrebuild(ctx context.Context, db database.Store, prebu
395399
}
396400

397401
func (c Controller) provision(ctx context.Context, db database.Store, prebuildID uuid.UUID, template database.Template, transition database.WorkspaceTransition, workspace database.Workspace) error {
402+
tvp, err := db.GetPresetParametersByTemplateVersionID(ctx, template.ActiveVersionID)
403+
if err != nil {
404+
return xerrors.Errorf("fetch preset details: %w", err)
405+
}
406+
407+
var params []codersdk.WorkspaceBuildParameter
408+
for _, param := range tvp {
409+
params = append(params, codersdk.WorkspaceBuildParameter{
410+
Name: param.Name,
411+
Value: param.Value,
412+
})
413+
}
414+
398415
builder := wsbuilder.New(workspace, transition).
399416
Reason(database.BuildReasonInitiator).
400417
Initiator(PrebuildOwnerUUID).
401418
ActiveVersion().
402419
VersionID(template.ActiveVersionID).
403-
MarkPrebuild()
404-
// RichParameterValues(req.RichParameterValues) // TODO: fetch preset's params
420+
MarkPrebuild().
421+
RichParameterValues(params)
405422

406423
_, provisionerJob, _, err := builder.Build(
407424
ctx,

coderd/provisionerdserver/provisionerdserver.go

+19-18
Original file line numberDiff line numberDiff line change
@@ -1713,13 +1713,13 @@ func (s *server) CompleteJob(ctx context.Context, completed *proto.CompletedJob)
17131713

17141714
// If this job was initiated by the prebuilds user and the job is not a prebuild, then it MUST be the claim run.
17151715
// TODO: maybe add some specific metadata to indicate this rather than imputing it.
1716-
if input.PrebuildClaimByUser != uuid.Nil {
1716+
if input.PrebuildClaimedByUser != uuid.Nil {
17171717
channel := agentsdk.PrebuildClaimedChannel(workspace.ID)
17181718
s.Logger.Info(ctx, "workspace prebuild successfully claimed by user",
1719-
slog.F("user", input.PrebuildClaimByUser.String()),
1719+
slog.F("user", input.PrebuildClaimedByUser.String()),
17201720
slog.F("workspace_id", workspace.ID),
17211721
slog.F("channel", channel))
1722-
if err := s.Pubsub.Publish(channel, []byte(input.PrebuildClaimByUser.String())); err != nil {
1722+
if err := s.Pubsub.Publish(channel, []byte(input.PrebuildClaimedByUser.String())); err != nil {
17231723
s.Logger.Error(ctx, "failed to publish message to workspace agent to pull new manifest", slog.Error(err))
17241724
}
17251725
}
@@ -1881,22 +1881,23 @@ func InsertWorkspacePresetAndParameters(ctx context.Context, db database.Store,
18811881
if err != nil {
18821882
return xerrors.Errorf("insert preset parameters: %w", err)
18831883
}
1884+
1885+
if protoPreset.Prebuild != nil {
1886+
_, err := db.InsertPresetPrebuild(ctx, database.InsertPresetPrebuildParams{
1887+
ID: uuid.New(),
1888+
PresetID: dbPreset.ID,
1889+
DesiredInstances: protoPreset.Prebuild.Instances,
1890+
InvalidateAfterSecs: 0, // TODO: implement cache invalidation
1891+
})
1892+
if err != nil {
1893+
return xerrors.Errorf("insert preset prebuild: %w", err)
1894+
}
1895+
}
18841896
return nil
18851897
}, nil)
18861898
if err != nil {
18871899
return xerrors.Errorf("insert preset and parameters: %w", err)
18881900
}
1889-
if protoPreset.Prebuild != nil {
1890-
_, err := db.InsertPresetPrebuild(ctx, database.InsertPresetPrebuildParams{
1891-
ID: uuid.New(),
1892-
PresetID: dbPreset.ID,
1893-
DesiredInstances: protoPreset.Prebuild.Instances,
1894-
InvalidateAfterSecs: 0, // TODO: implement cache invalidation
1895-
})
1896-
if err != nil {
1897-
return xerrors.Errorf("insert preset prebuild: %w", err)
1898-
}
1899-
}
19001901
return nil
19011902
}
19021903

@@ -2383,10 +2384,10 @@ type TemplateVersionImportJob struct {
23832384

23842385
// WorkspaceProvisionJob is the payload for the "workspace_provision" job type.
23852386
type WorkspaceProvisionJob struct {
2386-
WorkspaceBuildID uuid.UUID `json:"workspace_build_id"`
2387-
DryRun bool `json:"dry_run"`
2388-
IsPrebuild bool `json:"is_prebuild,omitempty"`
2389-
PrebuildClaimByUser uuid.UUID `json:"prebuild_claim_by,omitempty"`
2387+
WorkspaceBuildID uuid.UUID `json:"workspace_build_id"`
2388+
DryRun bool `json:"dry_run"`
2389+
IsPrebuild bool `json:"is_prebuild,omitempty"`
2390+
PrebuildClaimedByUser uuid.UUID `json:"prebuild_claimed_by,omitempty"`
23902391
// RunningWorkspaceAgentID is *only* used for prebuilds. We pass it down when we want to rebuild a prebuilt workspace
23912392
// but not generate a new agent token. The provisionerdserver will retrieve this token and push it down to
23922393
// the provisioner (and ultimately to the `coder_agent` resource in the Terraform provider) where it will be

coderd/workspaces.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9-
"github.com/coder/coder/v2/coderd/prebuilds"
109
"net/http"
1110
"slices"
1211
"strconv"
1312
"time"
1413

14+
"github.com/coder/coder/v2/coderd/prebuilds"
15+
1516
"github.com/dustin/go-humanize"
1617
"github.com/go-chi/chi/v5"
1718
"github.com/google/uuid"
1819
"golang.org/x/xerrors"
1920

2021
"cdr.dev/slog"
22+
2123
"github.com/coder/coder/v2/agent/proto"
2224
"github.com/coder/coder/v2/coderd/audit"
2325
"github.com/coder/coder/v2/coderd/database"
@@ -729,7 +731,7 @@ func createWorkspace(
729731
}
730732

731733
if claimedWorkspace != nil {
732-
builder = builder.MarkPrebuildClaimBy(owner.ID)
734+
builder = builder.MarkPrebuildClaimedBy(owner.ID)
733735
}
734736

735737
workspaceBuild, provisionerJob, provisionerDaemons, err = builder.Build(

coderd/wsbuilder/wsbuilder.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type Builder struct {
7474
parameterValues *[]string
7575

7676
prebuild bool
77-
prebuildClaimBy uuid.UUID
77+
prebuildClaimedBy uuid.UUID
7878
runningWorkspaceAgentID uuid.UUID
7979

8080
verifyNoLegacyParametersOnce bool
@@ -178,9 +178,9 @@ func (b Builder) MarkPrebuild() Builder {
178178
return b
179179
}
180180

181-
func (b Builder) MarkPrebuildClaimBy(userID uuid.UUID) Builder {
181+
func (b Builder) MarkPrebuildClaimedBy(userID uuid.UUID) Builder {
182182
// nolint: revive
183-
b.prebuildClaimBy = userID
183+
b.prebuildClaimedBy = userID
184184
return b
185185
}
186186

@@ -319,7 +319,7 @@ func (b *Builder) buildTx(authFunc func(action policy.Action, object rbac.Object
319319
WorkspaceBuildID: workspaceBuildID,
320320
LogLevel: b.logLevel,
321321
IsPrebuild: b.prebuild,
322-
PrebuildClaimByUser: b.prebuildClaimBy,
322+
PrebuildClaimedByUser: b.prebuildClaimedBy,
323323
RunningWorkspaceAgentID: b.runningWorkspaceAgentID,
324324
})
325325
if err != nil {
@@ -613,6 +613,11 @@ func (b *Builder) findNewBuildParameterValue(name string) *codersdk.WorkspaceBui
613613
}
614614

615615
func (b *Builder) getLastBuildParameters() ([]database.WorkspaceBuildParameter, error) {
616+
// TODO: exclude preset params from this list instead of returning nothing?
617+
if b.prebuildClaimedBy != uuid.Nil {
618+
return nil, nil
619+
}
620+
616621
if b.lastBuildParameters != nil {
617622
return *b.lastBuildParameters, nil
618623
}

0 commit comments

Comments
 (0)