feat(agent): add CODER_AGENT_DEVCONTAINERS_ENABLE option (#16525)

This commit is contained in:
Cian Johnston
2025-02-11 15:29:59 +00:00
committed by GitHub
parent 34b46f9205
commit 35901028d2
3 changed files with 44 additions and 15 deletions

View File

@ -140,3 +140,10 @@ type Lister interface {
// This should include running and stopped containers. // This should include running and stopped containers.
List(ctx context.Context) (codersdk.WorkspaceAgentListContainersResponse, error) List(ctx context.Context) (codersdk.WorkspaceAgentListContainersResponse, error)
} }
// NoopLister is a Lister interface that never returns any containers.
type NoopLister struct{}
func (NoopLister) List(_ context.Context) (codersdk.WorkspaceAgentListContainersResponse, error) {
return codersdk.WorkspaceAgentListContainersResponse{}, nil
}

View File

@ -25,6 +25,7 @@ import (
"cdr.dev/slog/sloggers/slogjson" "cdr.dev/slog/sloggers/slogjson"
"cdr.dev/slog/sloggers/slogstackdriver" "cdr.dev/slog/sloggers/slogstackdriver"
"github.com/coder/coder/v2/agent" "github.com/coder/coder/v2/agent"
"github.com/coder/coder/v2/agent/agentcontainers"
"github.com/coder/coder/v2/agent/agentexec" "github.com/coder/coder/v2/agent/agentexec"
"github.com/coder/coder/v2/agent/agentssh" "github.com/coder/coder/v2/agent/agentssh"
"github.com/coder/coder/v2/agent/reaper" "github.com/coder/coder/v2/agent/reaper"
@ -37,21 +38,22 @@ import (
func (r *RootCmd) workspaceAgent() *serpent.Command { func (r *RootCmd) workspaceAgent() *serpent.Command {
var ( var (
auth string auth string
logDir string logDir string
scriptDataDir string scriptDataDir string
pprofAddress string pprofAddress string
noReap bool noReap bool
sshMaxTimeout time.Duration sshMaxTimeout time.Duration
tailnetListenPort int64 tailnetListenPort int64
prometheusAddress string prometheusAddress string
debugAddress string debugAddress string
slogHumanPath string slogHumanPath string
slogJSONPath string slogJSONPath string
slogStackdriverPath string slogStackdriverPath string
blockFileTransfer bool blockFileTransfer bool
agentHeaderCommand string agentHeaderCommand string
agentHeader []string agentHeader []string
devcontainersEnabled bool
) )
cmd := &serpent.Command{ cmd := &serpent.Command{
Use: "agent", Use: "agent",
@ -314,6 +316,15 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
return xerrors.Errorf("create agent execer: %w", err) return xerrors.Errorf("create agent execer: %w", err)
} }
var containerLister agentcontainers.Lister
if !devcontainersEnabled {
logger.Info(ctx, "agent devcontainer detection not enabled")
containerLister = &agentcontainers.NoopLister{}
} else {
logger.Info(ctx, "agent devcontainer detection enabled")
containerLister = agentcontainers.NewDocker(execer)
}
agnt := agent.New(agent.Options{ agnt := agent.New(agent.Options{
Client: client, Client: client,
Logger: logger, Logger: logger,
@ -339,6 +350,7 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
PrometheusRegistry: prometheusRegistry, PrometheusRegistry: prometheusRegistry,
BlockFileTransfer: blockFileTransfer, BlockFileTransfer: blockFileTransfer,
Execer: execer, Execer: execer,
ContainerLister: containerLister,
}) })
promHandler := agent.PrometheusMetricsHandler(prometheusRegistry, logger) promHandler := agent.PrometheusMetricsHandler(prometheusRegistry, logger)
@ -461,6 +473,13 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
Description: fmt.Sprintf("Block file transfer using known applications: %s.", strings.Join(agentssh.BlockedFileTransferCommands, ",")), Description: fmt.Sprintf("Block file transfer using known applications: %s.", strings.Join(agentssh.BlockedFileTransferCommands, ",")),
Value: serpent.BoolOf(&blockFileTransfer), Value: serpent.BoolOf(&blockFileTransfer),
}, },
{
Flag: "devcontainers-enable",
Default: "true",
Env: "CODER_AGENT_DEVCONTAINERS_ENABLE",
Description: "Allow the agent to automatically detect running devcontainers.",
Value: serpent.BoolOf(&devcontainersEnabled),
},
} }
return cmd return cmd

View File

@ -33,6 +33,9 @@ OPTIONS:
--debug-address string, $CODER_AGENT_DEBUG_ADDRESS (default: 127.0.0.1:2113) --debug-address string, $CODER_AGENT_DEBUG_ADDRESS (default: 127.0.0.1:2113)
The bind address to serve a debug HTTP server. The bind address to serve a debug HTTP server.
--devcontainers-enable bool, $CODER_AGENT_DEVCONTAINERS_ENABLE (default: true)
Allow the agent to automatically detect running devcontainers.
--log-dir string, $CODER_AGENT_LOG_DIR (default: /tmp) --log-dir string, $CODER_AGENT_LOG_DIR (default: /tmp)
Specify the location for the agent log files. Specify the location for the agent log files.