- 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
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
80ac9a3
0ecceb0
55998d0
fb78d33
a7d1ea4
393f6e9
95b156e
f8f3000
999469f
8338af3
2f0180e
1ae6015
File 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
… type, return container and host port
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -6,6 +6,7 @@ import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net" | ||
"os/user" | ||
"slices" | ||
"sort" | ||
@@ -379,6 +380,19 @@ func convertDockerInspect(raw []byte) ([]codersdk.WorkspaceAgentDevcontainer, [] | ||
} | ||
outs := make([]codersdk.WorkspaceAgentDevcontainer, 0, len(ins)) | ||
// Say you have two containers: | ||
// - Container A with Host IP 127.0.0.1:8000 mapped to container port 8001 | ||
// - Container B with Host IP [::1]:8000 mapped to container port 8001 | ||
// A request to localhost:8000 may be routed to either container. | ||
// We don't know which one for sure, so we need to surface this to the user. | ||
// Keep track of all host ports we see. If we see the same host port | ||
// mapped to multiple containers on different host IPs, we need to | ||
// warn the user about this. | ||
// Note that we only do this for loopback or unspecified IPs. | ||
// We'll assume that the user knows what they're doing if they bind to | ||
// a specific IP address. | ||
hostPortContainers := make(map[int][]string) | ||
for _, in := range ins { | ||
out := codersdk.WorkspaceAgentDevcontainer{ | ||
CreatedAt: in.Created, | ||
@@ -387,7 +401,7 @@ func convertDockerInspect(raw []byte) ([]codersdk.WorkspaceAgentDevcontainer, [] | ||
ID: in.ID, | ||
Image: in.Config.Image, | ||
Labels: in.Config.Labels, | ||
Ports: make([]codersdk.WorkspaceAgentDevcontainerPort, 0), | ||
Running: in.State.Running, | ||
Status: in.State.String(), | ||
Volumes: make(map[string]string, len(in.Mounts)), | ||
@@ -399,35 +413,43 @@ func convertDockerInspect(raw []byte) ([]codersdk.WorkspaceAgentDevcontainer, [] | ||
portKeys := maps.Keys(in.NetworkSettings.Ports) | ||
// Sort the ports for deterministic output. | ||
sort.Strings(portKeys) | ||
//If we see the same port boundtoboth ipv4 and ipv6 loopback or unspecified | ||
//interfaces tothesame container port, there is no point in adding it multiple times. | ||
loopbackHostPortContainerPorts := make(map[int]uint16, 0) | ||
for _, pk := range portKeys { | ||
for _, p := range in.NetworkSettings.Ports[pk] { | ||
cp, network, err := convertDockerPort(pk) | ||
if err != nil { | ||
warns = append(warns, fmt.Sprintf("convert docker port: %s", err.Error())) | ||
// Default network to "tcp" if we can't parse it. | ||
network = "tcp" | ||
} | ||
hp, err := strconv.Atoi(p.HostPort) | ||
if err != nil { | ||
warns = append(warns, fmt.Sprintf("convert dockerhostport: %s", err.Error())) | ||
continue | ||
} | ||
if hp > 65535 || hp <1 { // invalid port | ||
warns = append(warns, fmt.Sprintf("convert dockerhostport: invalid host port %d", hp)) | ||
continue | ||
} | ||
// Deduplicate host ports for loopback and unspecified IPs. | ||
if isLoopbackOrUnspecified(p.HostIP) { | ||
if found, ok := loopbackHostPortContainerPorts[hp]; ok && found == cp { | ||
// We've already seen this port, so skip it. | ||
continue | ||
} | ||
loopbackHostPortContainerPorts[hp] = cp | ||
// Also keep track of the host port and the container ID. | ||
hostPortContainers[hp] = append(hostPortContainers[hp], in.ID) | ||
} | ||
out.Ports = append(out.Ports, codersdk.WorkspaceAgentDevcontainerPort{ | ||
Network: network, | ||
Port: cp, | ||
HostPort: uint16(hp), | ||
HostIP: p.HostIP, | ||
}) | ||
} | ||
} | ||
@@ -444,6 +466,13 @@ func convertDockerInspect(raw []byte) ([]codersdk.WorkspaceAgentDevcontainer, [] | ||
outs = append(outs, out) | ||
} | ||
// Check if any host ports are mapped to multiple containers. | ||
for hp, ids := range hostPortContainers { | ||
if len(ids) > 1 { | ||
warns = append(warns, fmt.Sprintf("host port %d is mapped to multiple containers on different interfaces: %s", hp, strings.Join(ids, ", "))) | ||
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. Is ids here the sha or a human readable name? The latter may be easier on the eyes but both work as long as we surface the used value in the UI. 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. I used the ID here for specificity. FriendlyName might be a good call; I'll address that in a follow-up! | ||
} | ||
} | ||
return outs, warns, nil | ||
} | ||
@@ -471,3 +500,12 @@ func convertDockerPort(in string) (uint16, string, error) { | ||
return 0, "", xerrors.Errorf("invalid port format: %s", in) | ||
} | ||
} | ||
// convenience function to check if an IP address is loopback or unspecified | ||
func isLoopbackOrUnspecified(ips string) bool { | ||
nip := net.ParseIP(ips) | ||
if nip == nil { | ||
return false // technically correct, I suppose | ||
} | ||
return nip.IsLoopback() || nip.IsUnspecified() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
[ | ||
{ | ||
"Id": "a", | ||
"Created": "2025-03-11T17:56:34.842164541Z", | ||
"State": { | ||
"Running": true, | ||
"ExitCode": 0, | ||
"Error": "" | ||
}, | ||
"Name": "/a", | ||
"Mounts": [], | ||
"Config": { | ||
"Image": "debian:bookworm", | ||
"Labels": {} | ||
}, | ||
"NetworkSettings": { | ||
"Ports": { | ||
"8001/tcp": [ | ||
{ | ||
"HostIp": "0.0.0.0", | ||
"HostPort": "8000" | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
{ | ||
"Id": "b", | ||
"Created": "2025-03-11T17:56:34.842164541Z", | ||
"State": { | ||
"Running": true, | ||
"ExitCode": 0, | ||
"Error": "" | ||
}, | ||
"Name": "/b", | ||
"Config": { | ||
"Image": "debian:bookworm", | ||
"Labels": {} | ||
}, | ||
"NetworkSettings": { | ||
"Ports": { | ||
"8001/tcp": [ | ||
{ | ||
"HostIp": "::", | ||
"HostPort": "8000" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
] |
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.
Uh oh!
There was an error while loading.Please reload this page.