Skip to content

feat(agent/agentcontainers): auto detect dev containers #18950

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
// return existing devcontainers but actual container detection
// and creation will be deferred.
a.containerAPI.Init(
agentcontainers.WithManifestInfo(manifest.OwnerName, manifest.WorkspaceName, manifest.AgentName),
agentcontainers.WithManifestInfo(manifest.OwnerName, manifest.WorkspaceName, manifest.AgentName, manifest.Directory),
agentcontainers.WithDevcontainers(manifest.Devcontainers, manifest.Scripts),
agentcontainers.WithSubAgentClient(agentcontainers.NewSubAgentClientFromAPI(a.logger, aAPI)),
)
Expand Down
143 changes: 139 additions & 4 deletions agent/agentcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"maps"
"net/http"
"os"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/fsnotify/fsnotify"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/spf13/afero"
"golang.org/x/xerrors"

"cdr.dev/slog"
Expand Down Expand Up @@ -56,10 +58,12 @@ type API struct {
cancel context.CancelFunc
watcherDone chan struct{}
updaterDone chan struct{}
discoverDone chan struct{}
updateTrigger chan chan error // Channel to trigger manual refresh.
updateInterval time.Duration // Interval for periodic container updates.
logger slog.Logger
watcher watcher.Watcher
fs afero.Fs
execer agentexec.Execer
commandEnv CommandEnv
ccli ContainerCLI
Expand All @@ -71,9 +75,12 @@ type API struct {
subAgentURL string
subAgentEnv []string

ownerName string
workspaceName string
parentAgent string
projectDiscovery bool // If we should perform project discovery or not.

ownerName string
workspaceName string
parentAgent string
agentDirectory string

mu sync.RWMutex // Protects the following fields.
initDone chan struct{} // Closed by Init.
Expand Down Expand Up @@ -192,11 +199,12 @@ func WithSubAgentEnv(env ...string) Option {

// WithManifestInfo sets the owner name, and workspace name
// for the sub-agent.
func WithManifestInfo(owner, workspace, parentAgent string) Option {
func WithManifestInfo(owner, workspace, parentAgent, agentDirectory string) Option {
return func(api *API) {
api.ownerName = owner
api.workspaceName = workspace
api.parentAgent = parentAgent
api.agentDirectory = agentDirectory
}
}

Expand Down Expand Up @@ -261,6 +269,21 @@ func WithWatcher(w watcher.Watcher) Option {
}
}

// WithFileSystem sets the file system used for discovering projects.
func WithFileSystem(fileSystem afero.Fs) Option {
return func(api *API) {
api.fs = fileSystem
}
}

// WithProjectDiscovery sets if the API should attempt to discover
// projects on the filesystem.
func WithProjectDiscovery(projectDiscovery bool) Option {
return func(api *API) {
api.projectDiscovery = projectDiscovery
}
}

// ScriptLogger is an interface for sending devcontainer logs to the
// controlplane.
type ScriptLogger interface {
Expand Down Expand Up @@ -331,6 +354,9 @@ func NewAPI(logger slog.Logger, options ...Option) *API {
api.watcher = watcher.NewNoop()
}
}
if api.fs == nil {
api.fs = afero.NewOsFs()
}
if api.subAgentClient.Load() == nil {
var c SubAgentClient = noopSubAgentClient{}
api.subAgentClient.Store(&c)
Expand Down Expand Up @@ -372,13 +398,119 @@ func (api *API) Start() {
return
}

if api.projectDiscovery && api.agentDirectory != "" {
api.discoverDone = make(chan struct{})

go api.discover()
}

api.watcherDone = make(chan struct{})
api.updaterDone = make(chan struct{})

go api.watcherLoop()
go api.updaterLoop()
}

func (api *API) discover() {
defer close(api.discoverDone)
defer api.logger.Debug(api.ctx, "project discovery finished")
api.logger.Debug(api.ctx, "project discovery started")

if err := api.discoverDevcontainerProjects(); err != nil {
api.logger.Error(api.ctx, "discovering dev container projects", slog.Error(err))
}

if err := api.RefreshContainers(api.ctx); err != nil {
api.logger.Error(api.ctx, "refreshing containers after discovery", slog.Error(err))
}
}

func (api *API) discoverDevcontainerProjects() error {
isGitProject, err := afero.DirExists(api.fs, filepath.Join(api.agentDirectory, ".git"))
if err != nil {
return xerrors.Errorf(".git dir exists: %w", err)
}

// If the agent directory is a git project, we'll search
// the project for any `.devcontainer/devcontainer.json`
// files.
if isGitProject {
return api.discoverDevcontainersInProject(api.agentDirectory)
}

// The agent directory is _not_ a git project, so we'll
// search the top level of the agent directory for any
// git projects, and search those.
entries, err := afero.ReadDir(api.fs, api.agentDirectory)
if err != nil {
return xerrors.Errorf("read agent directory: %w", err)
}

for _, entry := range entries {
if !entry.IsDir() {
continue
}

isGitProject, err = afero.DirExists(api.fs, filepath.Join(api.agentDirectory, entry.Name(), ".git"))
if err != nil {
return xerrors.Errorf(".git dir exists: %w", err)
}

// If this directory is a git project, we'll search
// it for any `.devcontainer/devcontainer.json` files.
if isGitProject {
if err := api.discoverDevcontainersInProject(filepath.Join(api.agentDirectory, entry.Name())); err != nil {
return err
}
}
}

return nil
}

func (api *API) discoverDevcontainersInProject(projectPath string) error {
devcontainerConfigPaths := []string{
"/.devcontainer/devcontainer.json",
"/.devcontainer.json",
}

return afero.Walk(api.fs, projectPath, func(path string, info fs.FileInfo, _ error) error {
if info.IsDir() {
return nil
}

for _, relativeConfigPath := range devcontainerConfigPaths {
if !strings.HasSuffix(path, relativeConfigPath) {
continue
}

workspaceFolder := strings.TrimSuffix(path, relativeConfigPath)

api.logger.Debug(api.ctx, "discovered dev container project", slog.F("workspace_folder", workspaceFolder))

api.mu.Lock()
if _, found := api.knownDevcontainers[workspaceFolder]; !found {
api.logger.Debug(api.ctx, "adding dev container project", slog.F("workspace_folder", workspaceFolder))

dc := codersdk.WorkspaceAgentDevcontainer{
ID: uuid.New(),
Name: "", // Updated later based on container state.
WorkspaceFolder: workspaceFolder,
ConfigPath: path,
Status: "", // Updated later based on container state.
Dirty: false, // Updated later based on config file changes.
Container: nil,
}

api.knownDevcontainers[workspaceFolder] = dc
}
api.mu.Unlock()
}

return nil
})
}

func (api *API) watcherLoop() {
defer close(api.watcherDone)
defer api.logger.Debug(api.ctx, "watcher loop stopped")
Expand Down Expand Up @@ -1808,6 +1940,9 @@ func (api *API) Close() error {
if api.updaterDone != nil {
<-api.updaterDone
}
if api.discoverDone != nil {
<-api.discoverDone
}

// Wait for all async tasks to complete.
api.asyncWg.Wait()
Expand Down
Loading
Loading