- Notifications
You must be signed in to change notification settings - Fork1.1k
feat(agent/agentcontainers): support apps for dev container agents#18346
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from7 commits
eb8a4690dc5bb2bcd668991fd4e7c273b2386981008ec61b6ab2e82e4db77afd3a258320e832a336997b0ae2616e17b228d1da3a1c6652a1File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -64,6 +64,9 @@ type API struct { | ||||||
| subAgentURL string | ||||||
| subAgentEnv []string | ||||||
| userName string | ||||||
| workspaceName string | ||||||
| mu sync.RWMutex | ||||||
| closed bool | ||||||
| containers codersdk.WorkspaceAgentListContainersResponse // Output from the last list operation. | ||||||
| @@ -153,6 +156,20 @@ func WithSubAgentEnv(env ...string) Option { | ||||||
| } | ||||||
| } | ||||||
| // WithWorkspaceName sets the workspace name for the sub-agent. | ||||||
| func WithWorkspaceName(name string) Option { | ||||||
| return func(api *API) { | ||||||
| api.workspaceName = name | ||||||
| } | ||||||
| } | ||||||
| // WithUserName sets the user name for the sub-agent. | ||||||
| func WithUserName(name string) Option { | ||||||
| return func(api *API) { | ||||||
| api.userName = name | ||||||
| } | ||||||
| } | ||||||
| // WithDevcontainers sets the known devcontainers for the API. This | ||||||
| // allows the API to be aware of devcontainers defined in the workspace | ||||||
| // agent manifest. | ||||||
| @@ -1127,7 +1144,16 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c | ||||||
| codersdk.DisplayAppPortForward: true, | ||||||
| } | ||||||
| var apps []SubAgentApp | ||||||
| if config, err := api.dccli.ReadConfig(ctx, dc.WorkspaceFolder, dc.ConfigPath, | ||||||
| []string{ | ||||||
| fmt.Sprintf("CODER_WORKSPACE_AGENT_NAME=%s", dc.Name), | ||||||
| fmt.Sprintf("CODER_WORKSPACE_OWNER_NAME=%s", api.userName), | ||||||
| fmt.Sprintf("CODER_WORKSPACE_NAME=%s", api.workspaceName), | ||||||
| fmt.Sprintf("CODER_DEPLOYMENT_URL=%s", api.subAgentURL), | ||||||
| ||||||
| fmt.Sprintf("CODER_DEPLOYMENT_URL=%s",api.subAgentURL), | |
| fmt.Sprintf("CODER_AGENT_URL=%s",api.subAgentURL), |
The naming is slightly unfortunate but we currently have bothCODER_URL andCODER_AGENT_URL in the product, and a third one seems like too much.
IMOCODER_URL would be more appropriate, however, it would be nice if these environment variables werealso set on the parent workspace/agent (withCODER_AGENT_URL we're at 3/4). The reason is that if a user runsdevcontainer up manually, these values are then serializable into the docker container label.
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.
I've replaced it withCODER_URL. If you'd ratherCODER_AGENT_URL I'm happy to update it again.
I don't think we can rely on anything serialized at the point ofdevcontainer up, as theCODER_WORKSPACE_AGENT_NAME will be the parents agent name, not the child. So I'm not sure there is too much benefit there.
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.
Yeah, that's a good point about the conflicting env. Let's go withCODER_URL for now 👍🏻
DanielleMaywood marked this conversation as resolved. OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
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.
We should log app creation errors from the response after agent creation too.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,6 +7,7 @@ import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "io" | ||
| "os" | ||
| "golang.org/x/xerrors" | ||
| @@ -32,13 +33,14 @@ type DevcontainerCustomizations struct { | ||
| type CoderCustomization struct { | ||
| DisplayApps map[codersdk.DisplayApp]bool `json:"displayApps,omitempty"` | ||
| Apps []SubAgentApp `json:"apps,omitempty"` | ||
| } | ||
| // DevcontainerCLI is an interface for the devcontainer CLI. | ||
| type DevcontainerCLI interface { | ||
| Up(ctx context.Context, workspaceFolder, configPath string, opts ...DevcontainerCLIUpOptions) (id string, err error) | ||
| Exec(ctx context.Context, workspaceFolder, configPath string, cmd string, cmdArgs []string, opts ...DevcontainerCLIExecOptions) error | ||
| ReadConfig(ctx context.Context, workspaceFolder, configPath string,env []string,opts ...DevcontainerCLIReadConfigOptions) (DevcontainerConfig, error) | ||
| } | ||
| // DevcontainerCLIUpOptions are options for the devcontainer CLI Up | ||
| @@ -113,8 +115,8 @@ type devcontainerCLIReadConfigConfig struct { | ||
| stderr io.Writer | ||
| } | ||
| //WithReadConfigOutput sets additional stdout and stderr writers for logs | ||
| // duringReadConfig operations. | ||
| func WithReadConfigOutput(stdout, stderr io.Writer) DevcontainerCLIReadConfigOptions { | ||
| return func(o *devcontainerCLIReadConfigConfig) { | ||
| o.stdout = stdout | ||
| @@ -250,7 +252,7 @@ func (d *devcontainerCLI) Exec(ctx context.Context, workspaceFolder, configPath | ||
| return nil | ||
| } | ||
| func (d *devcontainerCLI) ReadConfig(ctx context.Context, workspaceFolder, configPath string,env []string,opts ...DevcontainerCLIReadConfigOptions) (DevcontainerConfig, error) { | ||
| conf := applyDevcontainerCLIReadConfigOptions(opts) | ||
| logger := d.logger.With(slog.F("workspace_folder", workspaceFolder), slog.F("config_path", configPath)) | ||
| @@ -263,6 +265,8 @@ func (d *devcontainerCLI) ReadConfig(ctx context.Context, workspaceFolder, confi | ||
| } | ||
| c := d.execer.CommandContext(ctx, "devcontainer", args...) | ||
| c.Env = append(c.Env, os.Environ()...) | ||
DanielleMaywood marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| c.Env = append(c.Env, env...) | ||
| var stdoutBuf bytes.Buffer | ||
| stdoutWriters := []io.Writer{&stdoutBuf, &devcontainerCLILogWriter{ctx: ctx, logger: logger.With(slog.F("stdout", true))}} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -316,7 +316,7 @@ func TestDevcontainerCLI_ArgsAndParsing(t *testing.T) { | ||
| } | ||
| dccli := agentcontainers.NewDevcontainerCLI(logger, testExecer) | ||
| config, err := dccli.ReadConfig(ctx, tt.workspaceFolder, tt.configPath,[]string{},tt.opts...) | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if tt.wantError { | ||
| assert.Error(t, err, "want error") | ||
| assert.Equal(t, agentcontainers.DevcontainerConfig{}, config, "expected empty config on error") | ||
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.