-
Notifications
You must be signed in to change notification settings - Fork 925
fix(agent): start devcontainers through agentcontainers package #18471
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
base: main
Are you sure you want to change the base?
Conversation
Fixes coder/internal#706 Context for the implementation here coder/internal#706 (comment) Synchronously starts dev containers defined in terraform with our `DevcontainerCLI` abstraction, instead of piggybacking off of our `agentscripts` package. This gives us more control over logs, instead of being reliant on packages which may or may not exist in the user-provided image.
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.
Pull Request Overview
This PR refactors how devcontainers are started by splitting out the devcontainer CLI functionality from the agentscripts package. Key changes include removing post start scripts from agentscripts, updating tests to reference new expected outputs and behavior, and updating the API to synchronously trigger devcontainer creation via a new CreateDevcontainer method.
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
agent/agentscripts/agentscripts_test.go | Removed tests for post-start scripts and updated expectations. |
agent/agentscripts/agentscripts.go | Refactored to remove runnerScript and post start script logic. |
agent/agentcontainers/testdata/devcontainercli/parse/up.expected | Updated expected log output for devcontainer CLI. |
agent/agentcontainers/devcontainercli_test.go | Updated test file references to expected log output file. |
agent/agentcontainers/devcontainercli.go | Updated option defaults and logging writer behavior. |
agent/agentcontainers/devcontainer_test.go | Entire file removed, reflecting changes in devcontainer handling. |
agent/agentcontainers/devcontainer.go | Removed devcontainer script extraction helper functions. |
agent/agentcontainers/api.go | Modified recreateDevcontainer to return errors and added CreateDevcontainer. |
agent/agent.go | Removed legacy handling of experimental devcontainers and added calls to containerAPI.CreateDevcontainer. |
Comments suppressed due to low confidence (1)
@@ -401,6 +408,7 @@ func (l *devcontainerCLILogWriter) Write(p []byte) (n int, err error) { | |||
} | |||
if logLine.Level >= 3 { | |||
l.logger.Info(l.ctx, "@devcontainer/cli", slog.F("line", string(line))) | |||
_, _ = l.writer.Write([]byte(logLine.Text + "\n")) |
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.
It might be beneficial to check the error returned by l.writer.Write rather than ignoring it. Handling any potential write failure could improve the reliability of the logging.
_, _ = l.writer.Write([]byte(logLine.Text + "\n")) | |
if _, err := l.writer.Write([]byte(logLine.Text + "\n")); err != nil { | |
l.logger.Error(l.ctx, "failed to write to log writer", slog.Error(err), slog.F("text", logLine.Text)) | |
} |
Copilot uses AI. Check for mistakes.
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.
Cursory glance looks good! 👍🏻
Couple of notes (in addition to inline comments):
- Testing, this could be done in
agent_test.go
for e.g. autostart integration test - A
api_test.go
to testCreateDevcontainer
behavior - (Re-introduce agent script timing for autostart of devcontainer, not critical for GA, but perhaps track via ticket or if simple a follow-up PR.)
agent/agent.go
Outdated
|
||
if cAPI := a.containerAPI.Load(); cAPI != nil { | ||
for _, dc := range manifest.Devcontainers { | ||
err = errors.Join(err, cAPI.CreateDevcontainer(dc)) |
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 think it's a good idea to do these sync so we avoid potential Docker concurrency races, it'd be nice if it was asynchronous but let's go with this!
api.asyncWg.Add(1) | ||
api.mu.Unlock() | ||
|
||
return api.recreateDevcontainer(dc, dc.ConfigPath) |
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.
How about we rename this createDevcontainer
and make recreate
an argument? I'm thinking that iff users use a docker volume for Docker, it might be nice if we didn't delete/recreate the existing container that was persisted across workspace restart. 🤔
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'd like to bump this idea, not critical to get this merged though.
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'm not entirely sure I understand what the difference would be? What would the recreate
argument make it do differently?
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.
It would decide whether or not to pass WithRemoveExistingContainer
to the up command. If not given and the container exists but is stopped, it would simply be started rather than removed and a new created in its place. May allow users to persist fs changes in their devcontainer across workspace restarts.
// have not been enabled, a warning will be surfaced in the agent logs. | ||
script.RunOnStart = false | ||
return script | ||
} |
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.
Yay!!!
}) | ||
} | ||
} | ||
} |
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.
Good bye my beautiful hack! 👋🏻
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.
Largely looks good, but I still have some concerns with initialization order of the containers API (see comment). Ideally it would be initialized early, like e.g. SSH server, then the race could be avoided.
agent/api.go
Outdated
agentcontainers.WithDevcontainers(manifest.Devcontainers, manifest.Scripts), | ||
) | ||
} | ||
if cAPI := a.containerAPI.Load(); cAPI != nil { |
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 imagine there could be a race here? And since this method will only be called once, we would then never correctly set the routes if that happens. Just considering since we saw the race the other way today, but this seems to have worked as intended before.
It would also be good to verify that cAPI.UpdateSubAgentClient
still gets called in an appropriate location/order considering the refactor.
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.
Quite possibly. I'll need to address updating the WithManifestInfo
call as we won't have the manifest information yet.
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 given it a go, let me know if I've done anything obviously wrong.
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 think it can work! It's a bit different than what I had in mind but yours is probably better tbh 😄
api.asyncWg.Add(1) | ||
api.mu.Unlock() | ||
|
||
return api.recreateDevcontainer(dc, dc.ConfigPath) |
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'd like to bump this idea, not critical to get this merged though.
|
||
return api | ||
} | ||
|
||
func (api *API) Init(opts ...Option) { |
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.
Suggestion: Document purpose and also special behavior (like must only be called once).
An option is also to move watcher/updater start here, which would remove the need for the waiting goroutine in Perhaps better if kept as-is, but just amend the channel closing.NewAPI
, but either works.
break | ||
case <-api.initialized: | ||
go api.watcherLoop() | ||
go api.updaterLoop() |
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.
An important consideration here is that Close
is dependent on these functions closing their channels.
agent/api.go
Outdated
agentcontainers.WithDevcontainers(manifest.Devcontainers, manifest.Scripts), | ||
) | ||
} | ||
if cAPI := a.containerAPI.Load(); cAPI != nil { |
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 think it can work! It's a bit different than what I had in mind but yours is probably better tbh 😄
Fixes coder/internal#706
Context for the implementation here
coder/internal#706 (comment)
Synchronously starts dev containers defined in terraform with our
DevcontainerCLI
abstraction, instead of piggybacking off of ouragentscripts
package. This gives us more control over logs, instead of being reliant on packages which may or may not exist in the user-provided image.