Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
| ... | ... |
@@ -19,7 +19,7 @@ var ( |
| 19 | 19 |
ErrNetworkAlreadyAllocated = errors.New("requested network overlaps with existing network")
|
| 20 | 20 |
ErrNetworkAlreadyRegisterd = errors.New("requested network is already registered")
|
| 21 | 21 |
ErrNetworkOverlapsWithNameservers = errors.New("requested network overlaps with nameserver")
|
| 22 |
- ErrNoAvailableIps = errors.New("no available ips on network")
|
|
| 22 |
+ ErrNoAvailableIPs = errors.New("no available ip addresses on network")
|
|
| 23 | 23 |
ErrIPAlreadyAllocated = errors.New("ip already allocated")
|
| 24 | 24 |
|
| 25 | 25 |
lock = sync.Mutex{}
|
| ... | ... |
@@ -100,11 +100,11 @@ func getNextIp(network *net.IPNet) (*net.IP, error) {
|
| 100 | 100 |
ownIP = ipToInt(&network.IP) |
| 101 | 101 |
available = availableIPS[n] |
| 102 | 102 |
allocated = allocatedIPs[n] |
| 103 |
- |
|
| 104 |
- first, _ = networkRange(network) |
|
| 105 |
- base = ipToInt(&first) |
|
| 106 |
- |
|
| 107 |
- pos = int32(available.Pop()) |
|
| 103 |
+ first, _ = networkRange(network) |
|
| 104 |
+ base = ipToInt(&first) |
|
| 105 |
+ size = int(networkSize(network.Mask)) |
|
| 106 |
+ max = int32(size - 2) // size -1 for the broadcast address, -1 for the gateway address |
|
| 107 |
+ pos = int32(available.Pop()) |
|
| 108 | 108 |
) |
| 109 | 109 |
|
| 110 | 110 |
// We pop and push the position not the ip |
| ... | ... |
@@ -115,29 +115,22 @@ func getNextIp(network *net.IPNet) (*net.IP, error) {
|
| 115 | 115 |
return ip, nil |
| 116 | 116 |
} |
| 117 | 117 |
|
| 118 |
- var ( |
|
| 119 |
- size = int(networkSize(network.Mask)) |
|
| 120 |
- max = int32(size - 2) // size -1 for the broadcast address, -1 for the gateway address |
|
| 121 |
- ) |
|
| 122 |
- |
|
| 123 |
- if pos = int32(allocated.PullBack()); pos == 0 {
|
|
| 124 |
- pos = 1 |
|
| 125 |
- } |
|
| 126 |
- |
|
| 118 |
+ pos = int32(allocated.PullBack()) |
|
| 127 | 119 |
for i := int32(0); i < max; i++ {
|
| 128 |
- next := int32(base + pos) |
|
| 129 | 120 |
pos = pos%max + 1 |
| 121 |
+ next := int32(base + pos) |
|
| 130 | 122 |
|
| 131 | 123 |
if next == ownIP {
|
| 132 | 124 |
continue |
| 133 | 125 |
} |
| 134 | 126 |
|
| 135 |
- ip := intToIP(next) |
|
| 136 |
- allocated.Push(int(pos)) |
|
| 137 |
- |
|
| 138 |
- return ip, nil |
|
| 127 |
+ if !allocated.Exists(int(pos)) {
|
|
| 128 |
+ ip := intToIP(next) |
|
| 129 |
+ allocated.Push(int(pos)) |
|
| 130 |
+ return ip, nil |
|
| 131 |
+ } |
|
| 139 | 132 |
} |
| 140 |
- return nil, ErrNoAvailableIps |
|
| 133 |
+ return nil, ErrNoAvailableIPs |
|
| 141 | 134 |
} |
| 142 | 135 |
|
| 143 | 136 |
func registerIP(network *net.IPNet, ip *net.IP) error {
|
| ... | ... |
@@ -386,9 +386,9 @@ func TestIPAllocator(t *testing.T) {
|
| 386 | 386 |
// 2(u) - 3(u) - 4(u) - 5(u) - 6(u) |
| 387 | 387 |
// ↑ |
| 388 | 388 |
|
| 389 |
- assertIPEquals(t, &expectedIPs[3], newIPs[0]) |
|
| 390 |
- assertIPEquals(t, &expectedIPs[4], newIPs[1]) |
|
| 391 |
- assertIPEquals(t, &expectedIPs[2], newIPs[2]) |
|
| 389 |
+ assertIPEquals(t, &expectedIPs[2], newIPs[0]) |
|
| 390 |
+ assertIPEquals(t, &expectedIPs[3], newIPs[1]) |
|
| 391 |
+ assertIPEquals(t, &expectedIPs[4], newIPs[2]) |
|
| 392 | 392 |
|
| 393 | 393 |
_, err = RequestIP(network, nil) |
| 394 | 394 |
if err == nil {
|