Skip to content

Commit 1e1be54

Browse files
committed
libn/networkdb: prevent spurious rejoins in tests
The rejoinClusterBootStrap periodic task rejoins with the bootstrap nodes if none of them are members of the cluster. It correlates the cluster nodes with the bootstrap list by comparing IP addresses, ignoring ports. In normal operation this works out fine as every node has a unique IP address, but in unit tests every node listens on a distinct port of 127.0.0.1. This situation causes the check to incorrectly filter out all nodes from the list, mistaking them for the local node. Filter out the local node using pointer equality of the *node to avoid any ambiguity. Correlate the remote nodes by IP:port so that the check behaves the same in tests and in production. Signed-off-by: Cory Snider <[email protected]>
1 parent 8326491 commit 1e1be54

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

libnetwork/networkdb/cluster.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"math/big"
1111
rnd "math/rand"
1212
"net"
13+
"net/netip"
1314
"strings"
1415
"time"
1516

@@ -296,17 +297,22 @@ func (nDB *NetworkDB) rejoinClusterBootStrap() {
296297
bootStrapIPs := make([]string, 0, len(nDB.bootStrapIP))
297298
for _, bootIP := range nDB.bootStrapIP {
298299
// bootstrap IPs are usually IP:port from the Join
299-
var bootstrapIP net.IP
300-
ipStr, _, err := net.SplitHostPort(bootIP)
300+
bootstrapIP, err := netip.ParseAddrPort(bootIP)
301301
if err != nil {
302-
// try to parse it as an IP with port
302+
// try to parse it as an IP without port
303303
// Note this seems to be the case for swarm that do not specify any port
304-
ipStr = bootIP
304+
addr, err := netip.ParseAddr(bootIP)
305+
if err == nil {
306+
bootstrapIP = netip.AddrPortFrom(addr, uint16(nDB.config.BindPort))
307+
}
305308
}
306-
bootstrapIP = net.ParseIP(ipStr)
307-
if bootstrapIP != nil {
309+
if bootstrapIP.IsValid() {
308310
for _, node := range nDB.nodes {
309-
if node.Addr.Equal(bootstrapIP) && !node.Addr.Equal(myself.Addr) {
311+
if node == myself {
312+
continue
313+
}
314+
nodeIP, _ := netip.AddrFromSlice(node.Addr)
315+
if bootstrapIP == netip.AddrPortFrom(nodeIP, node.Port) {
310316
// One of the bootstrap nodes (and not myself) is part of the cluster, return
311317
nDB.RUnlock()
312318
return

0 commit comments

Comments
 (0)