diff --git a/plugins/ipam/host-local/allocator.go b/plugins/ipam/host-local/allocator.go index a250ac01..afb381f5 100644 --- a/plugins/ipam/host-local/allocator.go +++ b/plugins/ipam/host-local/allocator.go @@ -173,7 +173,7 @@ func (a *IPAllocator) Get(id string) (*types.IPConfig, error) { } startIP, endIP := a.getSearchRange() - for cur := startIP; !cur.Equal(endIP); cur = a.nextIP(cur) { + for cur := startIP; ; cur = a.nextIP(cur) { // don't allocate gateway IP if gw != nil && cur.Equal(gw) { continue @@ -190,6 +190,10 @@ func (a *IPAllocator) Get(id string) (*types.IPConfig, error) { Routes: a.conf.Routes, }, nil } + // break here to complete the loop + if cur.Equal(endIP) { + break + } } return nil, fmt.Errorf("no IP addresses available in network: %s", a.conf.Name) } diff --git a/plugins/ipam/host-local/allocator_test.go b/plugins/ipam/host-local/allocator_test.go index 2bcd2776..e1b19ee9 100644 --- a/plugins/ipam/host-local/allocator_test.go +++ b/plugins/ipam/host-local/allocator_test.go @@ -65,6 +65,12 @@ var _ = Describe("host-local ip allocator", func() { expectResult: "10.0.0.2", lastIP: "", }, + { + subnet: "10.0.0.0/30", + ipmap: map[string]string{}, + expectResult: "10.0.0.2", + lastIP: "", + }, { subnet: "10.0.0.0/29", ipmap: map[string]string{ @@ -107,6 +113,18 @@ var _ = Describe("host-local ip allocator", func() { expectResult: "10.0.0.3", lastIP: "10.0.0.128", }, + // wrap around and reserve lastIP + { + subnet: "10.0.0.0/29", + ipmap: map[string]string{ + "10.0.0.2": "id", + "10.0.0.4": "id", + "10.0.0.5": "id", + "10.0.0.6": "id", + }, + expectResult: "10.0.0.3", + lastIP: "10.0.0.3", + }, } for _, tc := range testCases {