mirror of
https://github.com/henrygd/beszel.git
synced 2026-08-16 15:27:47 +02:00
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:
@@ -373,16 +373,26 @@ func convertContainerPortsToString(ctr *container.ApiInfo) string {
|
||||
return ""
|
||||
}
|
||||
sort.Slice(ctr.Ports, func(i, j int) bool {
|
||||
return ctr.Ports[i].PublicPort < ctr.Ports[j].PublicPort
|
||||
if 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
|
||||
seenPorts := make(map[uint16]struct{})
|
||||
seen := make(map[string]struct{})
|
||||
for _, p := range ctr.Ports {
|
||||
_, ok := seenPorts[p.PublicPort]
|
||||
if p.PublicPort == 0 || ok {
|
||||
if p.PublicPort == 0 {
|
||||
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 {
|
||||
builder.WriteString(", ")
|
||||
}
|
||||
|
||||
@@ -1920,6 +1920,14 @@ func TestConvertContainerPortsToString(t *testing.T) {
|
||||
},
|
||||
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",
|
||||
ports: []port{
|
||||
@@ -1928,6 +1936,22 @@ func TestConvertContainerPortsToString(t *testing.T) {
|
||||
},
|
||||
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",
|
||||
ports: []port{
|
||||
|
||||
Reference in New Issue
Block a user