Skip to content

Commit

Permalink
portallocator: un-export PortAllocator.Begin, PortAllocator.End
Browse files Browse the repository at this point in the history
These values are configured when instantiating the allocator, and not
intended to be mutated externally. They're only used internally with
the exception of a test in the bridge driver that uses it to pick a
port that can be used for testing.

This patch:

- un-exports the Begin and End fields
- introduces a GetPortRange() utility to allow the bridge driver
  to get the port, but marking it as a function for internal use.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Aug 27, 2024
1 parent fb1ae4b commit 8e580ef
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion libnetwork/drivers/bridge/port_mapping_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func TestAddPortMappings(t *testing.T) {
ctrIP4 := newIPNet(t, "172.19.0.2/16")
ctrIP4Mapped := newIPNet(t, "::ffff:172.19.0.2/112")
ctrIP6 := newIPNet(t, "fdf8:b88e:bb5c:3483::2/64")
firstEphemPort := uint16(portallocator.Get().Begin)
firstEphemPort, _ := portallocator.GetPortRange()

testcases := []struct {
name string
Expand Down
23 changes: 16 additions & 7 deletions libnetwork/portallocator/portallocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ type (
mutex sync.Mutex
defaultIP net.IP
ipMap ipMapping
Begin int
End int
begin int
end int
}
portRange struct {
begin int
Expand All @@ -80,6 +80,15 @@ type (
protoMap map[string]*portMap
)

// GetPortRange returns the PortAllocator's default port range.
//
// This function is for internal use in tests, and must not be used
// for other purposes.
func GetPortRange() (start, end uint16) {
p := Get()
return uint16(p.begin), uint16(p.end)
}

// Get returns the PortAllocator
func Get() *PortAllocator {
// Port Allocator is a singleton
Expand All @@ -98,8 +107,8 @@ func newInstance() *PortAllocator {
return &PortAllocator{
ipMap: ipMapping{},
defaultIP: net.IPv4zero,
Begin: start,
End: end,
begin: start,
end: end,
}
}

Expand Down Expand Up @@ -148,9 +157,9 @@ func (p *PortAllocator) RequestPortsInRange(ips []net.IP, proto string, portStar
ipstr := ip.String()
if _, ok := p.ipMap[ipstr]; !ok {
p.ipMap[ipstr] = protoMap{
"tcp": newPortMap(p.Begin, p.End),
"udp": newPortMap(p.Begin, p.End),
"sctp": newPortMap(p.Begin, p.End),
"tcp": newPortMap(p.begin, p.end),
"udp": newPortMap(p.begin, p.end),
"sctp": newPortMap(p.begin, p.end),
}
}
pMaps[i] = p.ipMap[ipstr][proto]
Expand Down
22 changes: 11 additions & 11 deletions libnetwork/portallocator/portallocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestRequestNewPort(t *testing.T) {
t.Fatal(err)
}

if expected := p.Begin; port != expected {
if expected := p.begin; port != expected {
t.Fatalf("Expected port %d got %d", expected, port)
}
}
Expand Down Expand Up @@ -101,13 +101,13 @@ func TestUnknowProtocol(t *testing.T) {
func TestAllocateAllPorts(t *testing.T) {
p := newInstance()

for i := 0; i <= p.End-p.Begin; i++ {
for i := 0; i <= p.end-p.begin; i++ {
port, err := p.RequestPort(net.IPv4zero, "tcp", 0)
if err != nil {
t.Fatal(err)
}

if expected := p.Begin + i; port != expected {
if expected := p.begin + i; port != expected {
t.Fatalf("Expected port %d got %d", expected, port)
}
}
Expand All @@ -122,7 +122,7 @@ func TestAllocateAllPorts(t *testing.T) {
}

// release a port in the middle and ensure we get another tcp port
port := p.Begin + 5
port := p.begin + 5
p.ReleasePort(net.IPv4zero, "tcp", port)
newPort, err := p.RequestPort(net.IPv4zero, "tcp", 0)
if err != nil {
Expand All @@ -148,13 +148,13 @@ func BenchmarkAllocatePorts(b *testing.B) {
p := newInstance()

for i := 0; i < b.N; i++ {
for i := 0; i <= p.End-p.Begin; i++ {
for i := 0; i <= p.end-p.begin; i++ {
port, err := p.RequestPort(net.IPv4zero, "tcp", 0)
if err != nil {
b.Fatal(err)
}

if expected := p.Begin + i; port != expected {
if expected := p.begin + i; port != expected {
b.Fatalf("Expected port %d got %d", expected, port)
}
}
Expand Down Expand Up @@ -289,15 +289,15 @@ func TestPortAllocationWithCustomRange(t *testing.T) {
func TestNoDuplicateBPR(t *testing.T) {
p := newInstance()

if port, err := p.RequestPort(net.IPv4zero, "tcp", p.Begin); err != nil {
if port, err := p.RequestPort(net.IPv4zero, "tcp", p.begin); err != nil {
t.Fatal(err)
} else if port != p.Begin {
t.Fatalf("Expected port %d got %d", p.Begin, port)
} else if port != p.begin {
t.Fatalf("Expected port %d got %d", p.begin, port)
}

if port, err := p.RequestPort(net.IPv4zero, "tcp", 0); err != nil {
t.Fatal(err)
} else if port == p.Begin {
} else if port == p.begin {
t.Fatalf("Acquire(0) allocated the same port twice: %d", port)
}
}
Expand All @@ -310,7 +310,7 @@ func TestRequestPortForMultipleIPs(t *testing.T) {
// Default port range.
port, err := p.RequestPortsInRange(addrs, "tcp", 0, 0)
assert.Check(t, err)
assert.Check(t, is.Equal(port, p.Begin))
assert.Check(t, is.Equal(port, p.begin))

// Single-port range.
port, err = p.RequestPortsInRange(addrs, "tcp", 10000, 10000)
Expand Down

0 comments on commit 8e580ef

Please sign in to comment.