-
Notifications
You must be signed in to change notification settings - Fork 955
feat(agent/agentcontainers): allow auto start for discovered containers #19040
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,7 +143,8 @@ func WithCommandEnv(ce CommandEnv) Option { | |
strings.HasPrefix(s, "CODER_WORKSPACE_AGENT_URL=") || | ||
strings.HasPrefix(s, "CODER_AGENT_TOKEN=") || | ||
strings.HasPrefix(s, "CODER_AGENT_AUTH=") || | ||
strings.HasPrefix(s, "CODER_AGENT_DEVCONTAINERS_ENABLE=") | ||
strings.HasPrefix(s, "CODER_AGENT_DEVCONTAINERS_ENABLE=") || | ||
strings.HasPrefix(s, "CODER_AGENT_DEVCONTAINERS_PROJECT_DISCOVERY_ENABLE=") | ||
}) | ||
return shell, dir, env, nil | ||
} | ||
|
@@ -524,23 +525,41 @@ func (api *API) discoverDevcontainersInProject(projectPath string) error { | |
|
||
workspaceFolder := strings.TrimSuffix(path, relativeConfigPath) | ||
|
||
logger.Debug(api.ctx, "discovered dev container project", slog.F("workspace_folder", workspaceFolder)) | ||
logger := logger.With(slog.F("workspace_folder", workspaceFolder)) | ||
logger.Debug(api.ctx, "discovered dev container project") | ||
|
||
api.mu.Lock() | ||
if _, found := api.knownDevcontainers[workspaceFolder]; !found { | ||
logger.Debug(api.ctx, "adding dev container project", slog.F("workspace_folder", workspaceFolder)) | ||
logger.Debug(api.ctx, "adding dev container project") | ||
|
||
dc := codersdk.WorkspaceAgentDevcontainer{ | ||
ID: uuid.New(), | ||
Name: "", // Updated later based on container state. | ||
WorkspaceFolder: workspaceFolder, | ||
ConfigPath: path, | ||
Status: "", // Updated later based on container state. | ||
Status: codersdk.WorkspaceAgentDevcontainerStatusStopped, | ||
Dirty: false, // Updated later based on config file changes. | ||
Container: nil, | ||
} | ||
|
||
config, err := api.dccli.ReadConfig(api.ctx, workspaceFolder, path, []string{}) | ||
if err != nil { | ||
logger.Error(api.ctx, "read project configuration", slog.Error(err)) | ||
} else if config.Configuration.Customizations.Coder.AutoStart { | ||
dc.Status = codersdk.WorkspaceAgentDevcontainerStatusStarting | ||
} | ||
|
||
DanielleMaywood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
api.knownDevcontainers[workspaceFolder] = dc | ||
api.broadcastUpdatesLocked() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll now broadcast updates as we find the dev containers. This makes it feel a little quicker when there is a lot of files being searched through. |
||
|
||
if dc.Status == codersdk.WorkspaceAgentDevcontainerStatusStarting { | ||
api.asyncWg.Add(1) | ||
go func() { | ||
defer api.asyncWg.Done() | ||
|
||
_ = api.CreateDevcontainer(dc.WorkspaceFolder, dc.ConfigPath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I suppose it returns an error, should we log it somewhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}() | ||
} | ||
} | ||
api.mu.Unlock() | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3568,4 +3568,250 @@ func TestDevcontainerDiscovery(t *testing.T) { | |
// This is implicitly handled by `testutil.Logger` failing when it | ||
// detects an error has been logged. | ||
}) | ||
|
||
t.Run("AutoStart", func(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice tests! |
||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
agentDir string | ||
fs map[string]string | ||
expectDevcontainerCount int | ||
setupMocks func(mDCCLI *acmock.MockDevcontainerCLI) | ||
}{ | ||
{ | ||
name: "SingleEnabled", | ||
agentDir: "/home/coder", | ||
expectDevcontainerCount: 1, | ||
fs: map[string]string{ | ||
"/home/coder/.git/HEAD": "", | ||
"/home/coder/.devcontainer/devcontainer.json": "", | ||
}, | ||
setupMocks: func(mDCCLI *acmock.MockDevcontainerCLI) { | ||
gomock.InOrder( | ||
// Given: This dev container has auto start enabled. | ||
mDCCLI.EXPECT().ReadConfig(gomock.Any(), | ||
"/home/coder", | ||
"/home/coder/.devcontainer/devcontainer.json", | ||
[]string{}, | ||
).Return(agentcontainers.DevcontainerConfig{ | ||
Configuration: agentcontainers.DevcontainerConfiguration{ | ||
Customizations: agentcontainers.DevcontainerCustomizations{ | ||
Coder: agentcontainers.CoderCustomization{ | ||
AutoStart: true, | ||
}, | ||
}, | ||
}, | ||
}, nil), | ||
|
||
// Then: We expect it to be started. | ||
mDCCLI.EXPECT().Up(gomock.Any(), | ||
"/home/coder", | ||
"/home/coder/.devcontainer/devcontainer.json", | ||
gomock.Any(), | ||
).Return("", nil), | ||
) | ||
}, | ||
}, | ||
{ | ||
name: "SingleDisabled", | ||
agentDir: "/home/coder", | ||
expectDevcontainerCount: 1, | ||
fs: map[string]string{ | ||
"/home/coder/.git/HEAD": "", | ||
"/home/coder/.devcontainer/devcontainer.json": "", | ||
}, | ||
setupMocks: func(mDCCLI *acmock.MockDevcontainerCLI) { | ||
gomock.InOrder( | ||
// Given: This dev container has auto start disabled. | ||
mDCCLI.EXPECT().ReadConfig(gomock.Any(), | ||
"/home/coder", | ||
"/home/coder/.devcontainer/devcontainer.json", | ||
[]string{}, | ||
).Return(agentcontainers.DevcontainerConfig{ | ||
Configuration: agentcontainers.DevcontainerConfiguration{ | ||
Customizations: agentcontainers.DevcontainerCustomizations{ | ||
Coder: agentcontainers.CoderCustomization{ | ||
AutoStart: false, | ||
}, | ||
}, | ||
}, | ||
}, nil), | ||
|
||
// Then: We expect it to _not_ be started. | ||
mDCCLI.EXPECT().Up(gomock.Any(), | ||
"/home/coder", | ||
"/home/coder/.devcontainer/devcontainer.json", | ||
gomock.Any(), | ||
).Return("", nil).Times(0), | ||
) | ||
}, | ||
}, | ||
{ | ||
name: "OneEnabledOneDisabled", | ||
agentDir: "/home/coder", | ||
expectDevcontainerCount: 2, | ||
fs: map[string]string{ | ||
"/home/coder/.git/HEAD": "", | ||
"/home/coder/.devcontainer/devcontainer.json": "", | ||
"/home/coder/project/.devcontainer.json": "", | ||
}, | ||
setupMocks: func(mDCCLI *acmock.MockDevcontainerCLI) { | ||
gomock.InOrder( | ||
// Given: This dev container has auto start enabled. | ||
mDCCLI.EXPECT().ReadConfig(gomock.Any(), | ||
"/home/coder", | ||
"/home/coder/.devcontainer/devcontainer.json", | ||
[]string{}, | ||
).Return(agentcontainers.DevcontainerConfig{ | ||
Configuration: agentcontainers.DevcontainerConfiguration{ | ||
Customizations: agentcontainers.DevcontainerCustomizations{ | ||
Coder: agentcontainers.CoderCustomization{ | ||
AutoStart: true, | ||
}, | ||
}, | ||
}, | ||
}, nil), | ||
|
||
// Then: We expect it to be started. | ||
mDCCLI.EXPECT().Up(gomock.Any(), | ||
"/home/coder", | ||
"/home/coder/.devcontainer/devcontainer.json", | ||
gomock.Any(), | ||
).Return("", nil), | ||
) | ||
|
||
gomock.InOrder( | ||
// Given: This dev container has auto start disabled. | ||
mDCCLI.EXPECT().ReadConfig(gomock.Any(), | ||
"/home/coder/project", | ||
"/home/coder/project/.devcontainer.json", | ||
[]string{}, | ||
).Return(agentcontainers.DevcontainerConfig{ | ||
Configuration: agentcontainers.DevcontainerConfiguration{ | ||
Customizations: agentcontainers.DevcontainerCustomizations{ | ||
Coder: agentcontainers.CoderCustomization{ | ||
AutoStart: false, | ||
}, | ||
}, | ||
}, | ||
}, nil), | ||
|
||
// Then: We expect it to _not_ be started. | ||
mDCCLI.EXPECT().Up(gomock.Any(), | ||
"/home/coder/project", | ||
"/home/coder/project/.devcontainer.json", | ||
gomock.Any(), | ||
).Return("", nil).Times(0), | ||
) | ||
}, | ||
}, | ||
{ | ||
name: "MultipleEnabled", | ||
agentDir: "/home/coder", | ||
expectDevcontainerCount: 2, | ||
fs: map[string]string{ | ||
"/home/coder/.git/HEAD": "", | ||
"/home/coder/.devcontainer/devcontainer.json": "", | ||
"/home/coder/project/.devcontainer.json": "", | ||
}, | ||
setupMocks: func(mDCCLI *acmock.MockDevcontainerCLI) { | ||
gomock.InOrder( | ||
// Given: This dev container has auto start enabled. | ||
mDCCLI.EXPECT().ReadConfig(gomock.Any(), | ||
"/home/coder", | ||
"/home/coder/.devcontainer/devcontainer.json", | ||
[]string{}, | ||
).Return(agentcontainers.DevcontainerConfig{ | ||
Configuration: agentcontainers.DevcontainerConfiguration{ | ||
Customizations: agentcontainers.DevcontainerCustomizations{ | ||
Coder: agentcontainers.CoderCustomization{ | ||
AutoStart: true, | ||
}, | ||
}, | ||
}, | ||
}, nil), | ||
|
||
// Then: We expect it to be started. | ||
mDCCLI.EXPECT().Up(gomock.Any(), | ||
"/home/coder", | ||
"/home/coder/.devcontainer/devcontainer.json", | ||
gomock.Any(), | ||
).Return("", nil), | ||
) | ||
|
||
gomock.InOrder( | ||
// Given: This dev container has auto start enabled. | ||
mDCCLI.EXPECT().ReadConfig(gomock.Any(), | ||
"/home/coder/project", | ||
"/home/coder/project/.devcontainer.json", | ||
[]string{}, | ||
).Return(agentcontainers.DevcontainerConfig{ | ||
Configuration: agentcontainers.DevcontainerConfiguration{ | ||
Customizations: agentcontainers.DevcontainerCustomizations{ | ||
Coder: agentcontainers.CoderCustomization{ | ||
AutoStart: true, | ||
}, | ||
}, | ||
}, | ||
}, nil), | ||
|
||
// Then: We expect it to be started. | ||
mDCCLI.EXPECT().Up(gomock.Any(), | ||
"/home/coder/project", | ||
"/home/coder/project/.devcontainer.json", | ||
gomock.Any(), | ||
).Return("", nil), | ||
) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ( | ||
ctx = testutil.Context(t, testutil.WaitShort) | ||
logger = testutil.Logger(t) | ||
mClock = quartz.NewMock(t) | ||
mDCCLI = acmock.NewMockDevcontainerCLI(gomock.NewController(t)) | ||
|
||
r = chi.NewRouter() | ||
) | ||
|
||
// Given: We setup our mocks. These mocks handle our expectations for these | ||
// tests. If there are missing/unexpected mock calls, the test will fail. | ||
tt.setupMocks(mDCCLI) | ||
|
||
api := agentcontainers.NewAPI(logger, | ||
agentcontainers.WithClock(mClock), | ||
agentcontainers.WithWatcher(watcher.NewNoop()), | ||
agentcontainers.WithFileSystem(initFS(t, tt.fs)), | ||
agentcontainers.WithManifestInfo("owner", "workspace", "parent-agent", "/home/coder"), | ||
agentcontainers.WithContainerCLI(&fakeContainerCLI{}), | ||
agentcontainers.WithDevcontainerCLI(mDCCLI), | ||
agentcontainers.WithProjectDiscovery(true), | ||
) | ||
api.Start() | ||
defer api.Close() | ||
r.Mount("/", api.Routes()) | ||
|
||
// When: All expected dev containers have been found. | ||
require.Eventuallyf(t, func() bool { | ||
req := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx) | ||
rec := httptest.NewRecorder() | ||
r.ServeHTTP(rec, req) | ||
|
||
got := codersdk.WorkspaceAgentListContainersResponse{} | ||
err := json.NewDecoder(rec.Body).Decode(&got) | ||
require.NoError(t, err) | ||
|
||
return len(got.Devcontainers) >= tt.expectDevcontainerCount | ||
}, testutil.WaitShort, testutil.IntervalFast, "dev containers never found") | ||
|
||
// Then: We expect the mock infra to not fail. | ||
}) | ||
} | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed this in a previous PR #19016