Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
53 changes: 49 additions & 4 deletions pkg/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -110,15 +111,59 @@ func Ellipsis(str string, maxDisplayWidth int) string {
return str[:maxDisplayWidth-1] + "…"
}

func formatRange(startHost, endHost, startContainer, endContainer int32) string {
if startHost == endHost && startContainer == endContainer {
return fmt.Sprintf("%d->%d", startHost, startContainer)
}
return fmt.Sprintf("%d-%d->%d-%d", startHost, endHost, startContainer, endContainer)
}

func FormatPorts(ports []cni.PortMapping) string {
if len(ports) == 0 {
return ""
}
strs := make([]string, len(ports))
for i, p := range ports {
strs[i] = fmt.Sprintf("%s:%d->%d/%s", p.HostIP, p.HostPort, p.ContainerPort, p.Protocol)

type key struct {
HostIP string
Protocol string
}
grouped := make(map[key][]cni.PortMapping)

for _, p := range ports {
k := key{HostIP: p.HostIP, Protocol: p.Protocol}
grouped[k] = append(grouped[k], p)
}
return strings.Join(strs, ", ")

var displayPorts []string
for k, pms := range grouped {
sort.Slice(pms, func(i, j int) bool {
return pms[i].HostPort < pms[j].HostPort
})

var i int
var ranges []string
for i = 0; i < len(pms); {
start, end := pms[i], pms[i]
for i+1 < len(pms) &&
pms[i+1].HostPort == end.HostPort+1 &&
pms[i+1].ContainerPort == end.ContainerPort+1 {
i++
end = pms[i]
}

ranges = append(
ranges,
formatRange(start.HostPort, end.HostPort, start.ContainerPort, end.ContainerPort),
)
i++
}
displayPorts = append(
displayPorts,
fmt.Sprintf("%s:%s/%s", k.HostIP, strings.Join(ranges, ", "), k.Protocol),
)
}

return strings.Join(displayPorts, ", ")
}

func TimeSinceInHuman(since time.Time) string {
Expand Down
105 changes: 105 additions & 0 deletions pkg/formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"time"

"gotest.tools/v3/assert"

"github.com/containerd/go-cni"
)

func TestTimeSinceInHuman(t *testing.T) {
Expand Down Expand Up @@ -87,3 +89,106 @@ func TestTimeSinceInHuman(t *testing.T) {
})
}
}

func TestFormatPorts(t *testing.T) {
t.Parallel()

tests := []struct {
name string
input []cni.PortMapping
expected string
}{
{
name: "a single tcp port on localhost",
input: []cni.PortMapping{
{
HostPort: 3000,
ContainerPort: 8080,
Protocol: "tcp",
HostIP: "127.0.0.1",
},
},
expected: "127.0.0.1:3000->8080/tcp",
},
{
name: "consecutive tcp ports on localhost",
input: []cni.PortMapping{
{
HostPort: 3000,
ContainerPort: 8080,
Protocol: "tcp",
HostIP: "127.0.0.1",
},
{
HostPort: 3001,
ContainerPort: 8081,
Protocol: "tcp",
HostIP: "127.0.0.1",
},
},
expected: "127.0.0.1:3000-3001->8080-8081/tcp",
},
{
name: "a single tcp port on anyhost",
input: []cni.PortMapping{
{
HostPort: 3000,
ContainerPort: 8080,
Protocol: "tcp",
HostIP: "0.0.0.0",
},
},
expected: "0.0.0.0:3000->8080/tcp",
},
{
name: "a single udp port on anyhost",
input: []cni.PortMapping{
{
HostPort: 3000,
ContainerPort: 8080,
Protocol: "udp",
HostIP: "0.0.0.0",
},
},
expected: "0.0.0.0:3000->8080/udp",
},
{
name: "mixed tcp and udp with consecutive ports on anyhost",
input: []cni.PortMapping{
{
HostPort: 3000,
ContainerPort: 8080,
Protocol: "tcp",
HostIP: "0.0.0.0",
},
{
HostPort: 3001,
ContainerPort: 8081,
Protocol: "tcp",
HostIP: "0.0.0.0",
},
{
HostPort: 3002,
ContainerPort: 8082,
Protocol: "udp",
HostIP: "0.0.0.0",
},
{
HostPort: 3003,
ContainerPort: 8083,
Protocol: "udp",
HostIP: "0.0.0.0",
},
},
expected: "0.0.0.0:3000-3001->8080-8081/tcp, 0.0.0.0:3002-3003->8082-8083/udp",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := FormatPorts(tt.input)
assert.Equal(t, tt.expected, result)
})
}
}
Loading