Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3590102

Browse files
authored
feat(agent): add CODER_AGENT_DEVCONTAINERS_ENABLE option (coder#16525)
1 parent 34b46f9 commit 3590102

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

agent/agentcontainers/containers.go

+7
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,10 @@ type Lister interface {
140140
// This should include running and stopped containers.
141141
List(ctx context.Context) (codersdk.WorkspaceAgentListContainersResponse, error)
142142
}
143+
144+
// NoopLister is a Lister interface that never returns any containers.
145+
type NoopLister struct{}
146+
147+
func (NoopLister) List(_ context.Context) (codersdk.WorkspaceAgentListContainersResponse, error) {
148+
return codersdk.WorkspaceAgentListContainersResponse{}, nil
149+
}

cli/agent.go

+34-15
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"cdr.dev/slog/sloggers/slogjson"
2626
"cdr.dev/slog/sloggers/slogstackdriver"
2727
"github.com/coder/coder/v2/agent"
28+
"github.com/coder/coder/v2/agent/agentcontainers"
2829
"github.com/coder/coder/v2/agent/agentexec"
2930
"github.com/coder/coder/v2/agent/agentssh"
3031
"github.com/coder/coder/v2/agent/reaper"
@@ -37,21 +38,22 @@ import (
3738

3839
func (r *RootCmd) workspaceAgent() *serpent.Command {
3940
var (
40-
auth string
41-
logDir string
42-
scriptDataDir string
43-
pprofAddress string
44-
noReap bool
45-
sshMaxTimeout time.Duration
46-
tailnetListenPort int64
47-
prometheusAddress string
48-
debugAddress string
49-
slogHumanPath string
50-
slogJSONPath string
51-
slogStackdriverPath string
52-
blockFileTransfer bool
53-
agentHeaderCommand string
54-
agentHeader []string
41+
auth string
42+
logDir string
43+
scriptDataDir string
44+
pprofAddress string
45+
noReap bool
46+
sshMaxTimeout time.Duration
47+
tailnetListenPort int64
48+
prometheusAddress string
49+
debugAddress string
50+
slogHumanPath string
51+
slogJSONPath string
52+
slogStackdriverPath string
53+
blockFileTransfer bool
54+
agentHeaderCommand string
55+
agentHeader []string
56+
devcontainersEnabled bool
5557
)
5658
cmd := &serpent.Command{
5759
Use: "agent",
@@ -314,6 +316,15 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
314316
return xerrors.Errorf("create agent execer: %w", err)
315317
}
316318

319+
var containerLister agentcontainers.Lister
320+
if !devcontainersEnabled {
321+
logger.Info(ctx, "agent devcontainer detection not enabled")
322+
containerLister = &agentcontainers.NoopLister{}
323+
} else {
324+
logger.Info(ctx, "agent devcontainer detection enabled")
325+
containerLister = agentcontainers.NewDocker(execer)
326+
}
327+
317328
agnt := agent.New(agent.Options{
318329
Client: client,
319330
Logger: logger,
@@ -339,6 +350,7 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
339350
PrometheusRegistry: prometheusRegistry,
340351
BlockFileTransfer: blockFileTransfer,
341352
Execer: execer,
353+
ContainerLister: containerLister,
342354
})
343355

344356
promHandler := agent.PrometheusMetricsHandler(prometheusRegistry, logger)
@@ -461,6 +473,13 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
461473
Description: fmt.Sprintf("Block file transfer using known applications: %s.", strings.Join(agentssh.BlockedFileTransferCommands, ",")),
462474
Value: serpent.BoolOf(&blockFileTransfer),
463475
},
476+
{
477+
Flag: "devcontainers-enable",
478+
Default: "true",
479+
Env: "CODER_AGENT_DEVCONTAINERS_ENABLE",
480+
Description: "Allow the agent to automatically detect running devcontainers.",
481+
Value: serpent.BoolOf(&devcontainersEnabled),
482+
},
464483
}
465484

466485
return cmd

cli/testdata/coder_agent_--help.golden

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ OPTIONS:
3333
--debug-address string, $CODER_AGENT_DEBUG_ADDRESS (default: 127.0.0.1:2113)
3434
The bind address to serve a debug HTTP server.
3535

36+
--devcontainers-enable bool, $CODER_AGENT_DEVCONTAINERS_ENABLE (default: true)
37+
Allow the agent to automatically detect running devcontainers.
38+
3639
--log-dir string, $CODER_AGENT_LOG_DIR (default: /tmp)
3740
Specify the location for the agent log files.
3841

0 commit comments

Comments
 (0)