Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
johnstcn merged 12 commits intomainfromcj/agentcontainers-port-fix-2
Mar 18, 2025
Merged
Show file tree
Hide file tree
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…
johnstcnMar 11, 2025
0ecceb0
chore(agent/agentcontainers): add dedicated test for convertDockerIns…
johnstcnMar 11, 2025
55998d0
fix(agent/agentcontainers): fix incorrectly parsed port
johnstcnMar 11, 2025
fb78d33
nolint paralleltest
johnstcnMar 12, 2025
a7d1ea4
chore: adjust testdata structure
johnstcnMar 12, 2025
393f6e9
use a []byte instead of a string
johnstcnMar 12, 2025
95b156e
fix(agent/agentcontainers): create new WorkspaceAgentDevcontainerPort…
johnstcnMar 13, 2025
f8f3000
fix(site): correct container port link
johnstcnMar 13, 2025
999469f
chore(site): add stories for AgentDevcontainerCard
johnstcnMar 13, 2025
8338af3
make fmt lint
johnstcnMar 13, 2025
2f0180e
rm extraneous null check
johnstcnMar 18, 2025
1ae6015
address PR comment
johnstcnMar 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
fix(agent/agentcontainers): create new WorkspaceAgentDevcontainerPort…
… type, return container and host port
  • Loading branch information
@johnstcn
johnstcn committedMar 13, 2025
commit95b156ec142cfcf1cd474e68346a33b85ca0eafe
68 changes: 53 additions & 15 deletionsagent/agentcontainers/containers_dockercli.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"fmt"
"net"
"os/user"
"slices"
"sort"
Expand DownExpand Up@@ -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,
Expand All@@ -387,7 +401,7 @@ func convertDockerInspect(raw []byte) ([]codersdk.WorkspaceAgentDevcontainer, []
ID: in.ID,
Image: in.Config.Image,
Labels: in.Config.Labels,
Ports: make([]codersdk.WorkspaceAgentListeningPort, 0),
Ports: make([]codersdk.WorkspaceAgentDevcontainerPort, 0),
Running: in.State.Running,
Status: in.State.String(),
Volumes: make(map[string]string, len(in.Mounts)),
Expand All@@ -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)
//Each port binding may have multiple entries mappedtothe same interface.
//Keep track oftheports we've already seen.
seen := make(map[int]struct{}, len(in.NetworkSettings.Ports))
//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] {
_, network, err := convertDockerPort(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 docker port: %s", err.Error()))
warns = append(warns, fmt.Sprintf("convert dockerhostport: %s", err.Error()))
continue
}
if hp > 65535 || hp <0 { // invalid port
warns = append(warns, fmt.Sprintf("convert docker port: invalid host port %d", hp))
if hp > 65535 || hp <1 { // invalid port
warns = append(warns, fmt.Sprintf("convert dockerhostport: invalid host port %d", hp))
continue
}
if _, ok := seen[hp]; ok {
// We've already seen this port, so skip it.
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.WorkspaceAgentListeningPort{
Network: network,
Port: uint16(hp),
out.Ports = append(out.Ports, codersdk.WorkspaceAgentDevcontainerPort{
Network: network,
Port: cp,
HostPort: uint16(hp),
HostIP: p.HostIP,
})
seen[hp] = struct{}{}
}
}

Expand All@@ -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, ", ")))
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The 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
}

Expand DownExpand Up@@ -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()
}
89 changes: 70 additions & 19 deletionsagent/agentcontainers/containers_internal_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,7 @@ package agentcontainers

import (
"fmt"
"math/rand"
"os"
"path/filepath"
"slices"
Expand DownExpand Up@@ -438,7 +439,7 @@ func TestConvertDockerInspect(t *testing.T) {
Labels: map[string]string{},
Running: true,
Status: "running",
Ports: []codersdk.WorkspaceAgentListeningPort{},
Ports: []codersdk.WorkspaceAgentDevcontainerPort{},
Volumes: map[string]string{},
},
},
Expand All@@ -454,7 +455,7 @@ func TestConvertDockerInspect(t *testing.T) {
Labels: map[string]string{"baz": "zap", "foo": "bar"},
Running: true,
Status: "running",
Ports: []codersdk.WorkspaceAgentListeningPort{},
Ports: []codersdk.WorkspaceAgentDevcontainerPort{},
Volumes: map[string]string{},
},
},
Expand All@@ -470,7 +471,7 @@ func TestConvertDockerInspect(t *testing.T) {
Labels: map[string]string{},
Running: true,
Status: "running",
Ports: []codersdk.WorkspaceAgentListeningPort{},
Ports: []codersdk.WorkspaceAgentDevcontainerPort{},
Volumes: map[string]string{
"/tmp/test/a": "/var/coder/a",
"/tmp/test/b": "/var/coder/b",
Expand All@@ -489,10 +490,12 @@ func TestConvertDockerInspect(t *testing.T) {
Labels: map[string]string{},
Running: true,
Status: "running",
Ports: []codersdk.WorkspaceAgentListeningPort{
Ports: []codersdk.WorkspaceAgentDevcontainerPort{
{
Network: "tcp",
Port: 12345,
Network: "tcp",
Port: 12345,
HostPort: 12345,
HostIP: "0.0.0.0",
},
},
Volumes: map[string]string{},
Expand All@@ -510,16 +513,60 @@ func TestConvertDockerInspect(t *testing.T) {
Labels: map[string]string{},
Running: true,
Status: "running",
Ports: []codersdk.WorkspaceAgentListeningPort{
Ports: []codersdk.WorkspaceAgentDevcontainerPort{
{
Network: "tcp",
Port: 12345,
Network: "tcp",
Port: 23456,
HostPort: 12345,
HostIP: "0.0.0.0",
},
},
Volumes: map[string]string{},
},
},
},
{
name: "container_sameportdiffip",
expect: []codersdk.WorkspaceAgentDevcontainer{
{
CreatedAt: time.Date(2025, 3, 11, 17, 56, 34, 842164541, time.UTC),
ID: "a",
FriendlyName: "a",
Image: "debian:bookworm",
Labels: map[string]string{},
Running: true,
Status: "running",
Ports: []codersdk.WorkspaceAgentDevcontainerPort{
{
Network: "tcp",
Port: 8001,
HostPort: 8000,
HostIP: "0.0.0.0",
},
},
Volumes: map[string]string{},
},
{
CreatedAt: time.Date(2025, 3, 11, 17, 56, 34, 842164541, time.UTC),
ID: "b",
FriendlyName: "b",
Image: "debian:bookworm",
Labels: map[string]string{},
Running: true,
Status: "running",
Ports: []codersdk.WorkspaceAgentDevcontainerPort{
{
Network: "tcp",
Port: 8001,
HostPort: 8000,
HostIP: "::",
},
},
Volumes: map[string]string{},
},
},
expectWarns: []string{"host port 8000 is mapped to multiple containers on different interfaces: a, b"},
},
{
name: "container_volume",
expect: []codersdk.WorkspaceAgentDevcontainer{
Expand All@@ -531,7 +578,7 @@ func TestConvertDockerInspect(t *testing.T) {
Labels: map[string]string{},
Running: true,
Status: "running",
Ports: []codersdk.WorkspaceAgentListeningPort{},
Ports: []codersdk.WorkspaceAgentDevcontainerPort{},
Volumes: map[string]string{
"/var/lib/docker/volumes/testvol/_data": "/testvol",
},
Expand All@@ -552,7 +599,7 @@ func TestConvertDockerInspect(t *testing.T) {
},
Running: true,
Status: "running",
Ports: []codersdk.WorkspaceAgentListeningPort{},
Ports: []codersdk.WorkspaceAgentDevcontainerPort{},
Volumes: map[string]string{},
},
},
Expand All@@ -571,7 +618,7 @@ func TestConvertDockerInspect(t *testing.T) {
},
Running: true,
Status: "running",
Ports: []codersdk.WorkspaceAgentListeningPort{},
Ports: []codersdk.WorkspaceAgentDevcontainerPort{},
Volumes: map[string]string{},
},
},
Expand All@@ -590,11 +637,12 @@ func TestConvertDockerInspect(t *testing.T) {
},
Running: true,
Status: "running",
Ports: []codersdk.WorkspaceAgentListeningPort{
Ports: []codersdk.WorkspaceAgentDevcontainerPort{
{
Network: "tcp",
// Container port 8080 is mapped to 32768 on the host.
Port: 32768,
Network: "tcp",
Port: 8080,
HostPort: 32768,
HostIP: "0.0.0.0",
},
},
Volumes: map[string]string{},
Expand DownExpand Up@@ -771,10 +819,13 @@ func fakeContainer(t *testing.T, mut ...func(*codersdk.WorkspaceAgentDevcontaine
testutil.GetRandomName(t): testutil.GetRandomName(t),
},
Running: true,
Ports: []codersdk.WorkspaceAgentListeningPort{
Ports: []codersdk.WorkspaceAgentDevcontainerPort{
{
Network: "tcp",
Port: testutil.RandomPortNoListen(t),
Network: "tcp",
Port: testutil.RandomPortNoListen(t),
HostPort: testutil.RandomPortNoListen(t),
//nolint:gosec // this is a test
HostIP: []string{"127.0.0.1", "[::1]", "localhost", "0.0.0.0", "[::]", testutil.GetRandomName(t)}[rand.Intn(6)],
},
},
Status: testutil.MustRandString(t, 10),
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff 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"
}
]
}
}
}
]
23 changes: 22 additions & 1 deletioncoderd/apidoc/docs.go
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Loading

[8]ページ先頭

©2009-2025 Movatter.jp