8000 fix(agent): start devcontainers through agentcontainers package by DanielleMaywood · Pull Request #18471 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

fix(agent): start devcontainers through agentcontainers package #18471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
978c871
fix(agent): start devcontainers through agentcontainers package
DanielleMaywood Jun 20, 2025
fe99bd6
chore: appease formatter
DanielleMaywood Jun 20, 2025
aeae6e2
chore: fix test, appease linter
DanielleMaywood Jun 20, 2025
916f7e8
chore: feedback
DanielleMaywood Jun 24, 2025
53e256b
Merge branch 'main' into dm-devcontainer-log-spam
DanielleMaywood Jun 24, 2025
91bb43a
chore: re-add script timings
DanielleMaywood Jun 24, 2025
81fe11d
fix: change how containerAPI is stored
DanielleMaywood Jun 24, 2025
5c70a8c
Merge branch 'main' into dm-devcontainer-log-spam
DanielleMaywood Jun 24, 2025
8437ca4
chore: appease linter
DanielleMaywood Jun 24, 2025
2c6a2b1
chore: ensure the last log line is printed
DanielleMaywood Jun 24, 2025
a512ad4
chore: fix typo
DanielleMaywood Jun 24, 2025
c50dc6e
chore: OOPS
DanielleMaywood Jun 24, 2025
4d40ef2
chore: 1 -> 2
DanielleMaywood Jun 24, 2025
ce32e2e
chore: add a status to the timings
DanielleMaywood Jun 24, 2025
32ac48a
chore: initialize containerapi even earlier
DanielleMaywood Jun 24, 2025
738b755
chore: only enable when devcontainers are enabled
DanielleMaywood Jun 24, 2025
3714fec
chore: simplify things a little
DanielleMaywood Jun 24, 2025
996d440
chore: recreate -> create with argument
DanielleMaywood Jun 24, 2025
ae5dd1e
chore: ensure we close and init
DanielleMaywood Jun 24, 2025
9d76cf6
chore: appease linter
DanielleMaywood Jun 24, 2025
7285c39
chore: mock ReadConfig any time
DanielleMaywood Jun 24, 2025
3fce51c
chore: feedback
DanielleMaywood Jun 25, 2025
54aa84a
chore: feedback
DanielleMaywood Jun 25, 2025
3d08d0e
chore: only set status if not set, and run create in test
DanielleMaywood Jun 25, 2025
fa479fc
chore: feedback on poor error message
DanielleMaywood Jun 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10000
Prev Previous commit
Next Next commit
chore: re-add script timings
  • Loading branch information
DanielleMaywood committed Jun 24, 2025
commit 91bb43af35d52a9052f0bb08dedcfe1772377b8c
37 changes: 34 additions & 3 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
ServiceBannerRefreshInterval time.Duration
BlockFileTransfer bool
Execer agentexec.Execer
Clock quartz.Clock

ExperimentalDevcontainersEnabled bool
ContainerAPIOptions []agentcontainers.Option // Enable ExperimentalDevcontainersEnabled for these to be effective.
Expand Down Expand Up @@ -145,6 +146,9 @@
if options.PortCacheDuration == 0 {
options.PortCacheDuration = 1 * time.Second
}
if options.Clock == nil {
options.Clock = quartz.NewReal()
}

prometheusRegistry := options.PrometheusRegistry
if prometheusRegistry == nil {
Expand All @@ -158,6 +162,7 @@
hardCtx, hardCancel := context.WithCancel(context.Background())
gracefulCtx, gracefulCancel := context.WithCancel(hardCtx)
a := &agent{
clock: options.Clock,
tailnetListenPort: options.TailnetListenPort,
reconnectingPTYTimeout: options.ReconnectingPTYTimeout,
logger: options.Logger,
Expand Down Expand Up @@ -205,6 +210,7 @@
}

type agent struct {
clock quartz.Clock
logger 8000 slog.Logger
client Client
exchangeToken func(ctx context.Context) (string, error)
Expand Down Expand Up @@ -1124,7 +1130,7 @@
sentResult = true

// The startup script should only execute on the first run!
if oldManifest == nil {

Check failure on line 1133 in agent/agent.go

View workflow job for this annotation

GitHub Actions / lint

`if oldManifest == nil` has complex nested blocks (complexity: 20) (nestif)
a.setLifecycle(codersdk.WorkspaceAgentLifecycleStarting)

// Perform overrides early so that Git auth can work even if users
Expand All @@ -1142,9 +1148,13 @@
}

var (
scripts = manifest.Scripts
scriptRunnerOpts []agentscripts.InitOption
scripts = manifest.Scripts
scriptRunnerOpts []agentscripts.InitOption
devcontainerScripts map[uuid.UUID]codersdk.WorkspaceAgentScript
)
if a.experimentalDevcontainersEnabled {
scripts, devcontainerScripts = agentcontainers.ExtractDevcontainerScripts(manifest.Devcontainers, scripts)
}
err = a.scriptRunner.Init(scripts, aAPI.ScriptCompleted, scriptRunnerOpts...)
if err != nil {
return xerrors.Errorf("init script runner: %w", err)
Expand All @@ -1165,7 +1175,28 @@

if cAPI := a.containerAPI.Load(); cAPI != nil {
for _, dc := range manifest.Devcontainers {
err = errors.Join(err, cAPI.CreateDevcontainer(dc))
var (
script = devcontainerScripts[dc.ID]
exitCode = int32(0)
startTime = a.clock.Now()
)
if cErr := cAPI.CreateDevcontainer(dc); cErr != nil {
exitCode = 1
err = errors.Join(err, cErr)
}
endTime := a.clock.Now()

if _, scriptErr := aAPI.ScriptCompleted(ctx, &proto.WorkspaceAgentScriptCompletedRequest{
Timing: &proto.Timing{
ScriptId: script.ID[:],
Start: timestamppb.New(startTime),
End: timestamppb.New(endTime),
ExitCode: exitCode,
Stage: proto.Timing_START,
},
}); scriptErr != nil {
a.logger.Warn(ctx, "reporting script completed failed", slog.Error(scriptErr))
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions agent/agentcontainers/devcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"cdr.dev/slog"
"github.com/coder/coder/v2/codersdk"
"github.com/google/uuid"
)

const (
Expand All @@ -20,6 +21,28 @@ const (
DevcontainerDefaultContainerWorkspaceFolder = "/workspaces"
)

func ExtractDevcontainerScripts(
devcontainers []codersdk.WorkspaceAgentDevcontainer,
scripts []codersdk.WorkspaceAgentScript,
) (filteredScripts []codersdk.WorkspaceAgentScript, devcontainerScripts map[uuid.UUID]codersdk.WorkspaceAgentScript) {
devcontainerScripts = make(map[uuid.UUID]codersdk.WorkspaceAgentScript)
ScriptLoop:
for _, script := range scripts {
for _, dc := range devcontainers {
// The devcontainer scripts match the devcontainer ID for
// identification.
if script.ID == dc.ID {
devcontainerScripts[dc.ID] = script
continue ScriptLoop
}
}

filteredScripts = append(filteredScripts, script)
}

return filteredScripts, devcontainerScripts
}

// ExpandAllDevcontainerPaths expands all devcontainer paths in the given
// devcontainers. This is required by the devcontainer CLI, which requires
// absolute paths for the workspace folder and config path.
Expand Down
Loading
0