host-local: fix allocation of last address in a /30
authorMinhan Xia <mixia@google.com>
Fri, 16 Sep 2016 17:38:44 +0000 (10:38 -0700)
committerMinhan Xia <mixia@google.com>
Wed, 21 Sep 2016 20:10:05 +0000 (13:10 -0700)
plugins/ipam/host-local/allocator.go
plugins/ipam/host-local/allocator_test.go

index a250ac0..afb381f 100644 (file)
@@ -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)
 }
index 2bcd277..e1b19ee 100644 (file)
@@ -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 {