- Notifications
You must be signed in to change notification settings - Fork1k
fix(agent/agentcontainers): improve testing of convertDockerInspect, return correct host port#16887
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
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
Show all changes
12 commits Select commitHold shift + click to select a range
80ac9a3
chore(agent/agentcontainers): refactor runDockerInspect and convertDo…
johnstcn0ecceb0
chore(agent/agentcontainers): add dedicated test for convertDockerIns…
johnstcn55998d0
fix(agent/agentcontainers): fix incorrectly parsed port
johnstcnfb78d33
nolint paralleltest
johnstcna7d1ea4
chore: adjust testdata structure
johnstcn393f6e9
use a []byte instead of a string
johnstcn95b156e
fix(agent/agentcontainers): create new WorkspaceAgentDevcontainerPort…
johnstcnf8f3000
fix(site): correct container port link
johnstcn999469f
chore(site): add stories for AgentDevcontainerCard
johnstcn8338af3
make fmt lint
johnstcn2f0180e
rm extraneous null check
johnstcn1ae6015
address PR comment
johnstcnFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
NextNext commit
chore(agent/agentcontainers): refactor runDockerInspect and convertDo…
…ckerInspect
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit80ac9a3a5295d10de1be6a5f42be8a63611c31e0
There are no files selected for viewing
130 changes: 69 additions & 61 deletionsagent/agentcontainers/containers_dockercli.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -162,23 +162,28 @@ func (dei *DockerEnvInfoer) ModifyCommand(cmd string, args ...string) (string, [ | ||
// devcontainerEnv is a helper function that inspects the container labels to | ||
// find the required environment variables for running a command in the container. | ||
func devcontainerEnv(ctx context.Context, execer agentexec.Execer, container string) ([]string, error) { | ||
stdout, stderr, err := runDockerInspect(ctx, execer, container) | ||
if err != nil { | ||
return nil, xerrors.Errorf("inspect container: %w: %q", err, stderr) | ||
} | ||
ins, _, err := convertDockerInspect(stdout) | ||
if err != nil { | ||
return nil, xerrors.Errorf("inspect container: %w", err) | ||
} | ||
if len(ins) != 1 { | ||
return nil, xerrors.Errorf("inspect container: expected 1 container, got %d", len(ins)) | ||
} | ||
in := ins[0] | ||
if in.Labels == nil { | ||
return nil, nil | ||
} | ||
// We want to look for the devcontainer metadata, which is in the | ||
// value of the label `devcontainer.metadata`. | ||
rawMeta, ok := in.Labels["devcontainer.metadata"] | ||
if !ok { | ||
return nil, nil | ||
} | ||
@@ -279,42 +284,36 @@ func (dcl *DockerCLILister) List(ctx context.Context) (codersdk.WorkspaceAgentLi | ||
return codersdk.WorkspaceAgentListContainersResponse{}, xerrors.Errorf("run docker inspect: %w", err) | ||
} | ||
if dockerInspectStderr != "" { | ||
res.Warnings = append(res.Warnings, dockerInspectStderr) | ||
} | ||
outs, warns, err := convertDockerInspect(ins) | ||
if err != nil { | ||
return codersdk.WorkspaceAgentListContainersResponse{}, xerrors.Errorf("convert docker inspect output: %w", err) | ||
} | ||
res.Warnings = append(res.Warnings, warns...) | ||
res.Containers = append(res.Containers, outs...) | ||
return res, nil | ||
} | ||
// runDockerInspect is a helper function that runs `docker inspect` on the given | ||
// container IDs and returns the parsed output. | ||
// The stderr output is also returned for logging purposes. | ||
func runDockerInspect(ctx context.Context, execer agentexec.Execer, ids ...string) (stdout, stderrstring, err error) { | ||
johnstcn marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
var stdoutBuf, stderrBuf bytes.Buffer | ||
cmd := execer.CommandContext(ctx, "docker", append([]string{"inspect"}, ids...)...) | ||
cmd.Stdout = &stdoutBuf | ||
cmd.Stderr = &stderrBuf | ||
err = cmd.Run() | ||
stdout = strings.TrimSpace(stdoutBuf.String()) | ||
stderr = strings.TrimSpace(stderrBuf.String()) | ||
if err != nil { | ||
returnstdout, stderr, err | ||
} | ||
return stdoutBuf.String(), stderr, nil | ||
johnstcn marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
// To avoid a direct dependency on the Docker API, we use the docker CLI | ||
@@ -367,50 +366,59 @@ func (dis dockerInspectState) String() string { | ||
return sb.String() | ||
} | ||
func convertDockerInspect(raw string) ([]codersdk.WorkspaceAgentDevcontainer, []string, error) { | ||
var warns []string | ||
var ins []dockerInspect | ||
if err := json.NewDecoder(strings.NewReader(raw)).Decode(&ins); err != nil { | ||
return nil, nil, xerrors.Errorf("decode docker inspect output: %w", err) | ||
} | ||
outs := make([]codersdk.WorkspaceAgentDevcontainer, 0, len(ins)) | ||
for _, in := range ins { | ||
out := codersdk.WorkspaceAgentDevcontainer{ | ||
CreatedAt: in.Created, | ||
// Remove the leading slash from the container name | ||
FriendlyName: strings.TrimPrefix(in.Name, "/"), | ||
ID: in.ID, | ||
Image: in.Config.Image, | ||
Labels: in.Config.Labels, | ||
Ports: make([]codersdk.WorkspaceAgentListeningPort, 0), | ||
Running: in.State.Running, | ||
Status: in.State.String(), | ||
Volumes: make(map[string]string, len(in.Mounts)), | ||
} | ||
if in.HostConfig.PortBindings == nil { | ||
in.HostConfig.PortBindings = make(map[string]any) | ||
} | ||
portKeys := maps.Keys(in.HostConfig.PortBindings) | ||
// Sort the ports for deterministic output. | ||
sort.Strings(portKeys) | ||
for _, p := range portKeys { | ||
if port, network, err := convertDockerPort(p); err != nil { | ||
warns = append(warns, err.Error()) | ||
} else { | ||
out.Ports = append(out.Ports, codersdk.WorkspaceAgentListeningPort{ | ||
Network: network, | ||
Port: port, | ||
}) | ||
} | ||
} | ||
if in.Mounts == nil { | ||
in.Mounts = []dockerInspectMount{} | ||
} | ||
// Sort the mounts for deterministic output. | ||
sort.Slice(in.Mounts, func(i, j int) bool { | ||
return in.Mounts[i].Source < in.Mounts[j].Source | ||
}) | ||
for _, k := range in.Mounts { | ||
out.Volumes[k.Source] = k.Destination | ||
} | ||
outs = append(outs, out) | ||
} | ||
returnouts, warns, nil | ||
} | ||
// convertDockerPort converts a Docker port string to a port number and network | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.