Skip to content

Commit 9e394dd

Browse files
committed
Merge branch 'main' into template-settings-9541
2 parents f5e7206 + ceb52ac commit 9e394dd

File tree

82 files changed

+3240
-1718
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+3240
-1718
lines changed

.github/workflows/pr-deploy.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ jobs:
110110
set -euo pipefail
111111
mkdir -p ~/.kube
112112
echo "${{ secrets.PR_DEPLOYMENTS_KUBECONFIG }}" > ~/.kube/config
113+
chmod 644 ~/.kube/config
113114
export KUBECONFIG=~/.kube/config
114115
115116
- name: Check if the helm deployment already exists
@@ -257,6 +258,7 @@ jobs:
257258
set -euo pipefail
258259
mkdir -p ~/.kube
259260
echo "${{ secrets.PR_DEPLOYMENTS_KUBECONFIG }}" > ~/.kube/config
261+
chmod 644 ~/.kube/config
260262
export KUBECONFIG=~/.kube/config
261263
262264
- name: Check if image exists

cli/server.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,8 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
10121012

10131013
autobuildTicker := time.NewTicker(vals.AutobuildPollInterval.Value())
10141014
defer autobuildTicker.Stop()
1015-
autobuildExecutor := autobuild.NewExecutor(ctx, options.Database, coderAPI.TemplateScheduleStore, logger, autobuildTicker.C)
1015+
autobuildExecutor := autobuild.NewExecutor(
1016+
ctx, options.Database, options.Pubsub, coderAPI.TemplateScheduleStore, logger, autobuildTicker.C)
10161017
autobuildExecutor.Run()
10171018

10181019
hangDetectorTicker := time.NewTicker(vals.JobHangDetectorInterval.Value())
@@ -1378,16 +1379,12 @@ func newProvisionerDaemon(
13781379
connector[string(database.ProvisionerTypeTerraform)] = sdkproto.NewDRPCProvisionerClient(terraformClient)
13791380
}
13801381

1381-
debounce := time.Second
13821382
return provisionerd.New(func(ctx context.Context) (proto.DRPCProvisionerDaemonClient, error) {
13831383
// This debounces calls to listen every second. Read the comment
13841384
// in provisionerdserver.go to learn more!
1385-
return coderAPI.CreateInMemoryProvisionerDaemon(ctx, debounce)
1385+
return coderAPI.CreateInMemoryProvisionerDaemon(ctx)
13861386
}, &provisionerd.Options{
13871387
Logger: logger.Named("provisionerd"),
1388-
JobPollInterval: cfg.Provisioner.DaemonPollInterval.Value(),
1389-
JobPollJitter: cfg.Provisioner.DaemonPollJitter.Value(),
1390-
JobPollDebounce: debounce,
13911388
UpdateInterval: time.Second,
13921389
ForceCancelInterval: cfg.Provisioner.ForceCancelInterval.Value(),
13931390
Connector: connector,

cli/testdata/coder_server_--help.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,10 @@ updating, and deleting workspace resources.
393393
Time to force cancel provisioning tasks that are stuck.
394394

395395
--provisioner-daemon-poll-interval duration, $CODER_PROVISIONER_DAEMON_POLL_INTERVAL (default: 1s)
396-
Time to wait before polling for a new job.
396+
Deprecated and ignored.
397397

398398
--provisioner-daemon-poll-jitter duration, $CODER_PROVISIONER_DAEMON_POLL_JITTER (default: 100ms)
399-
Random jitter added to the poll interval.
399+
Deprecated and ignored.
400400

401401
--provisioner-daemon-psk string, $CODER_PROVISIONER_DAEMON_PSK
402402
Pre-shared key to authenticate external provisioner daemons to Coder

cli/testdata/server-config.yaml.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,10 @@ provisioning:
348348
# tests.
349349
# (default: false, type: bool)
350350
daemonsEcho: false
351-
# Time to wait before polling for a new job.
351+
# Deprecated and ignored.
352352
# (default: 1s, type: duration)
353353
daemonPollInterval: 1s
354-
# Random jitter added to the poll interval.
354+
# Deprecated and ignored.
355355
# (default: 100ms, type: duration)
356356
daemonPollJitter: 100ms
357357
# Time to force cancel provisioning tasks that are stuck.

cli/update_test.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -578,13 +578,15 @@ func TestUpdateValidateRichParameters(t *testing.T) {
578578
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
579579
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
580580

581+
// Create new workspace
581582
inv, root := clitest.New(t, "create", "my-workspace", "--yes", "--template", template.Name, "--parameter", fmt.Sprintf("%s=%s", stringParameterName, "2nd"))
582583
clitest.SetupConfig(t, client, root)
583584
err := inv.Run()
584585
require.NoError(t, err)
585586

586587
// Update template
587588
updatedTemplateParameters := []*proto.RichParameter{
589+
// The order of rich parameter options must be maintained because `cliui.Select` automatically selects the first option during tests.
588590
{Name: stringParameterName, Type: "string", Mutable: true, Required: true, Options: []*proto.RichParameterOption{
589591
{Name: "first_option", Description: "This is first option", Value: "1"},
590592
{Name: "second_option", Description: "This is second option", Value: "2"},
@@ -602,12 +604,16 @@ func TestUpdateValidateRichParameters(t *testing.T) {
602604
// Update the workspace
603605
inv, root = clitest.New(t, "update", "my-workspace")
604606
clitest.SetupConfig(t, client, root)
605-
607+
doneChan := make(chan struct{})
606608
pty := ptytest.New(t).Attach(inv)
607-
clitest.Start(t, inv)
609+
go func() {
610+
defer close(doneChan)
611+
err := inv.Run()
612+
assert.NoError(t, err)
613+
}()
608614

609615
matches := []string{
610-
stringParameterName, "second_option",
616+
// `cliui.Select` will automatically pick the first option
611617
"Planning workspace...", "",
612618
}
613619
for i := 0; i < len(matches); i += 2 {
@@ -619,6 +625,8 @@ func TestUpdateValidateRichParameters(t *testing.T) {
619625
pty.WriteLine(value)
620626
}
621627
}
628+
629+
<-doneChan
622630
})
623631

624632
t.Run("ParameterOptionDisappeared", func(t *testing.T) {
@@ -639,16 +647,19 @@ func TestUpdateValidateRichParameters(t *testing.T) {
639647
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
640648
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
641649

650+
// Create new workspace
642651
inv, root := clitest.New(t, "create", "my-workspace", "--yes", "--template", template.Name, "--parameter", fmt.Sprintf("%s=%s", stringParameterName, "2nd"))
643652
clitest.SetupConfig(t, client, root)
653+
ptytest.New(t).Attach(inv)
644654
err := inv.Run()
645655
require.NoError(t, err)
646656

647657
// Update template - 2nd option disappeared, 4th option added
648658
updatedTemplateParameters := []*proto.RichParameter{
659+
// The order of rich parameter options must be maintained because `cliui.Select` automatically selects the first option during tests.
649660
{Name: stringParameterName, Type: "string", Mutable: true, Required: true, Options: []*proto.RichParameterOption{
650-
{Name: "First option", Description: "This is first option", Value: "1st"},
651661
{Name: "Third option", Description: "This is third option", Value: "3rd"},
662+
{Name: "First option", Description: "This is first option", Value: "1st"},
652663
{Name: "Fourth option", Description: "This is fourth option", Value: "4th"},
653664
}},
654665
}
@@ -663,11 +674,16 @@ func TestUpdateValidateRichParameters(t *testing.T) {
663674
// Update the workspace
664675
inv, root = clitest.New(t, "update", "my-workspace")
665676
clitest.SetupConfig(t, client, root)
677+
doneChan := make(chan struct{})
666678
pty := ptytest.New(t).Attach(inv)
667-
clitest.Start(t, inv)
679+
go func() {
680+
defer close(doneChan)
681+
err := inv.Run()
682+
assert.NoError(t, err)
683+
}()
668684

669685
matches := []string{
670-
stringParameterName, "Third option",
686+
// `cliui.Select` will automatically pick the first option
671687
"Planning workspace...", "",
672688
}
673689
for i := 0; i < len(matches); i += 2 {
@@ -679,6 +695,8 @@ func TestUpdateValidateRichParameters(t *testing.T) {
679695
pty.WriteLine(value)
680696
}
681697
}
698+
699+
<-doneChan
682700
})
683701

684702
t.Run("ImmutableRequiredParameterExists_MutableRequiredParameterAdded", func(t *testing.T) {
@@ -742,6 +760,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
742760
pty.WriteLine(value)
743761
}
744762
}
763+
745764
<-doneChan
746765
})
747766

@@ -771,10 +790,11 @@ func TestUpdateValidateRichParameters(t *testing.T) {
771790
// Update template: add required, immutable parameter
772791
updatedTemplateParameters := []*proto.RichParameter{
773792
templateParameters[0],
793+
// The order of rich parameter options must be maintained because `cliui.Select` automatically selects the first option during tests.
774794
{Name: immutableParameterName, Type: "string", Mutable: false, Required: true, Options: []*proto.RichParameterOption{
795+
{Name: "thi", Description: "This is third option for immutable parameter", Value: "III"},
775796
{Name: "fir", Description: "This is first option for immutable parameter", Value: "I"},
776797
{Name: "sec", Description: "This is second option for immutable parameter", Value: "II"},
777-
{Name: "thi", Description: "This is third option for immutable parameter", Value: "III"},
778798
}},
779799
}
780800

@@ -797,7 +817,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
797817
}()
798818

799819
matches := []string{
800-
immutableParameterName, "thi",
820+
// `cliui.Select` will automatically pick the first option
801821
"Planning workspace...", "",
802822
}
803823
for i := 0; i < len(matches); i += 2 {
@@ -809,6 +829,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
809829
pty.WriteLine(value)
810830
}
811831
}
832+
812833
<-doneChan
813834
})
814835
}

coderd/activitybump_internal_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func Test_ActivityBumpWorkspace(t *testing.T) {
134134
TemplateID: template.ID,
135135
Ttl: sql.NullInt64{Valid: true, Int64: int64(tt.workspaceTTL)},
136136
})
137-
job = dbgen.ProvisionerJob(t, db, database.ProvisionerJob{
137+
job = dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{
138138
OrganizationID: org.ID,
139139
CompletedAt: tt.jobCompletedAt,
140140
})
@@ -225,7 +225,7 @@ func Test_ActivityBumpWorkspace(t *testing.T) {
225225
func insertPrevWorkspaceBuild(t *testing.T, db database.Store, orgID, tvID, workspaceID uuid.UUID, transition database.WorkspaceTransition, buildNumber int32) {
226226
t.Helper()
227227

228-
job := dbgen.ProvisionerJob(t, db, database.ProvisionerJob{
228+
job := dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{
229229
OrganizationID: orgID,
230230
})
231231
_ = dbgen.WorkspaceResource(t, db, database.WorkspaceResource{

coderd/apidoc/docs.go

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 11 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/autobuild/lifecycle_executor.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/coder/coder/v2/coderd/database/db2sdk"
1717
"github.com/coder/coder/v2/coderd/database/dbauthz"
1818
"github.com/coder/coder/v2/coderd/database/dbtime"
19+
"github.com/coder/coder/v2/coderd/database/provisionerjobs"
20+
"github.com/coder/coder/v2/coderd/database/pubsub"
1921
"github.com/coder/coder/v2/coderd/schedule"
2022
"github.com/coder/coder/v2/coderd/schedule/cron"
2123
"github.com/coder/coder/v2/coderd/wsbuilder"
@@ -26,6 +28,7 @@ import (
2628
type Executor struct {
2729
ctx context.Context
2830
db database.Store
31+
ps pubsub.Pubsub
2932
templateScheduleStore *atomic.Pointer[schedule.TemplateScheduleStore]
3033
log slog.Logger
3134
tick <-chan time.Time
@@ -40,11 +43,12 @@ type Stats struct {
4043
}
4144

4245
// New returns a new wsactions executor.
43-
func NewExecutor(ctx context.Context, db database.Store, tss *atomic.Pointer[schedule.TemplateScheduleStore], log slog.Logger, tick <-chan time.Time) *Executor {
46+
func NewExecutor(ctx context.Context, db database.Store, ps pubsub.Pubsub, tss *atomic.Pointer[schedule.TemplateScheduleStore], log slog.Logger, tick <-chan time.Time) *Executor {
4447
le := &Executor{
4548
//nolint:gocritic // Autostart has a limited set of permissions.
4649
ctx: dbauthz.AsAutostart(ctx),
4750
db: db,
51+
ps: ps,
4852
templateScheduleStore: tss,
4953
tick: tick,
5054
log: log.Named("autobuild"),
@@ -129,6 +133,7 @@ func (e *Executor) runOnce(t time.Time) Stats {
129133
log := e.log.With(slog.F("workspace_id", wsID))
130134

131135
eg.Go(func() error {
136+
var job *database.ProvisionerJob
132137
err := e.db.InTx(func(tx database.Store) error {
133138
// Re-check eligibility since the first check was outside the
134139
// transaction and the workspace settings may have changed.
@@ -168,7 +173,8 @@ func (e *Executor) runOnce(t time.Time) Stats {
168173
SetLastWorkspaceBuildJobInTx(&latestJob).
169174
Reason(reason)
170175

171-
if _, _, err := builder.Build(e.ctx, tx, nil); err != nil {
176+
_, job, err = builder.Build(e.ctx, tx, nil)
177+
if err != nil {
172178
log.Error(e.ctx, "unable to transition workspace",
173179
slog.F("transition", nextTransition),
174180
slog.Error(err),
@@ -230,6 +236,17 @@ func (e *Executor) runOnce(t time.Time) Stats {
230236
if err != nil {
231237
log.Error(e.ctx, "workspace scheduling failed", slog.Error(err))
232238
}
239+
if job != nil && err == nil {
240+
// Note that we can't refactor such that posting the job happens inside wsbuilder because it's called
241+
// with an outer transaction like this, and we need to make sure the outer transaction commits before
242+
// posting the job. If we post before the transaction commits, provisionerd might try to acquire the
243+
// job, fail, and then sit idle instead of picking up the job.
244+
err = provisionerjobs.PostJob(e.ps, *job)
245+
if err != nil {
246+
// Client probably doesn't care about this error, so just log it.
247+
log.Error(e.ctx, "failed to post provisioner job to pubsub", slog.Error(err))
248+
}
249+
}
233250
return nil
234251
})
235252
}

coderd/batchstats/batcher.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ func (b *Batcher) flush(ctx context.Context, forced bool, reason string) {
243243
err = b.store.InsertWorkspaceAgentStats(ctx, *b.buf)
244244
elapsed := time.Since(start)
245245
if err != nil {
246+
if xerrors.Is(err, context.Canceled) {
247+
b.log.Debug(ctx, "context canceled, skipping insert of workspace agent stats", slog.F("elapsed", elapsed))
248+
return
249+
}
246250
b.log.Error(ctx, "error inserting workspace agent stats", slog.Error(err), slog.F("elapsed", elapsed))
247251
return
248252
}

coderd/batchstats/batcher_internal_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/coder/coder/v2/coderd/database/dbgen"
1515
"github.com/coder/coder/v2/coderd/database/dbtestutil"
1616
"github.com/coder/coder/v2/coderd/database/dbtime"
17+
"github.com/coder/coder/v2/coderd/database/pubsub"
1718
"github.com/coder/coder/v2/coderd/rbac"
1819
"github.com/coder/coder/v2/codersdk/agentsdk"
1920
"github.com/coder/coder/v2/cryptorand"
@@ -26,11 +27,11 @@ func TestBatchStats(t *testing.T) {
2627
ctx, cancel := context.WithCancel(context.Background())
2728
t.Cleanup(cancel)
2829
log := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
29-
store, _ := dbtestutil.NewDB(t)
30+
store, ps := dbtestutil.NewDB(t)
3031

3132
// Set up some test dependencies.
32-
deps1 := setupDeps(t, store)
33-
deps2 := setupDeps(t, store)
33+
deps1 := setupDeps(t, store, ps)
34+
deps2 := setupDeps(t, store, ps)
3435
tick := make(chan time.Time)
3536
flushed := make(chan int, 1)
3637

@@ -168,7 +169,7 @@ type deps struct {
168169
// It creates an organization, user, template, workspace, and agent
169170
// along with all the other miscellaneous plumbing required to link
170171
// them together.
171-
func setupDeps(t *testing.T, store database.Store) deps {
172+
func setupDeps(t *testing.T, store database.Store, ps pubsub.Pubsub) deps {
172173
t.Helper()
173174

174175
org := dbgen.Organization(t, store, database.Organization{})
@@ -194,7 +195,7 @@ func setupDeps(t *testing.T, store database.Store) deps {
194195
OrganizationID: org.ID,
195196
LastUsedAt: time.Now().Add(-time.Hour),
196197
})
197-
pj := dbgen.ProvisionerJob(t, store, database.ProvisionerJob{
198+
pj := dbgen.ProvisionerJob(t, store, ps, database.ProvisionerJob{
198199
InitiatorID: user.ID,
199200
OrganizationID: org.ID,
200201
})

0 commit comments

Comments
 (0)