fix(agent): show all IP:port mappings for containers bound to multiple IPs (#1993)

* feat: support multiple docker ips

* deduplicate wildcard docker bindings

---------

Co-authored-by: henrygd <hank@henrygd.me>
This commit is contained in:
Sven van Ginkel
2026-08-15 17:50:20 +02:00
committed by GitHub
parent 66ac62a125
commit b68acea5a8
2 changed files with 39 additions and 5 deletions

View File

@@ -373,16 +373,26 @@ func convertContainerPortsToString(ctr *container.ApiInfo) string {
return "" return ""
} }
sort.Slice(ctr.Ports, func(i, j int) bool { sort.Slice(ctr.Ports, func(i, j int) bool {
if ctr.Ports[i].PublicPort != ctr.Ports[j].PublicPort {
return ctr.Ports[i].PublicPort < ctr.Ports[j].PublicPort return ctr.Ports[i].PublicPort < ctr.Ports[j].PublicPort
}
return ctr.Ports[i].IP < ctr.Ports[j].IP
}) })
var builder strings.Builder var builder strings.Builder
seenPorts := make(map[uint16]struct{}) seen := make(map[string]struct{})
for _, p := range ctr.Ports { for _, p := range ctr.Ports {
_, ok := seenPorts[p.PublicPort] if p.PublicPort == 0 {
if p.PublicPort == 0 || ok {
continue continue
} }
seenPorts[p.PublicPort] = struct{}{} keyIP := p.IP
if keyIP == "0.0.0.0" || keyIP == "::" {
keyIP = ""
}
key := keyIP + ":" + strconv.Itoa(int(p.PublicPort))
if _, ok := seen[key]; ok {
continue
}
seen[key] = struct{}{}
if builder.Len() > 0 { if builder.Len() > 0 {
builder.WriteString(", ") builder.WriteString(", ")
} }

View File

@@ -1920,6 +1920,14 @@ func TestConvertContainerPortsToString(t *testing.T) {
}, },
expected: "80, 443", expected: "80, 443",
}, },
{
name: "ipv4 and ipv6 wildcard bindings are deduplicated",
ports: []port{
{PublicPort: 80, IP: "0.0.0.0"},
{PublicPort: 80, IP: "::"},
},
expected: "80",
},
{ {
name: "multiple ports with different IPs", name: "multiple ports with different IPs",
ports: []port{ ports: []port{
@@ -1928,6 +1936,22 @@ func TestConvertContainerPortsToString(t *testing.T) {
}, },
expected: "80, 1.2.3.4:443", expected: "80, 1.2.3.4:443",
}, },
{
name: "same port bound to multiple IPs shows all entries",
ports: []port{
{PublicPort: 65533, IP: "172.16.151.72"},
{PublicPort: 65533, IP: "172.16.156.25"},
},
expected: "172.16.151.72:65533, 172.16.156.25:65533",
},
{
name: "same port bound to IPv4 and IPv6",
ports: []port{
{PublicPort: 65534, IP: "172.16.151.72"},
{PublicPort: 65534, IP: "fd04:38e2:98c6:3fd::72"},
},
expected: "172.16.151.72:65534, fd04:38e2:98c6:3fd::72:65534",
},
{ {
name: "ports slice is nilled after call", name: "ports slice is nilled after call",
ports: []port{ ports: []port{