Ver Fonte

Merge pull request #743 from aboch/ipr

Fix bug in getAddressRange() in ipam driver
Madhu Venugopal há 9 anos atrás
pai
commit
8092022e62

+ 1 - 5
libnetwork/ipam/allocator.go

@@ -220,7 +220,7 @@ func (a *Allocator) parsePoolRequest(addressSpace, pool, subPool string, v6 bool
 			return nil, nil, nil, ipamapi.ErrInvalidPool
 			return nil, nil, nil, ipamapi.ErrInvalidPool
 		}
 		}
 		if subPool != "" {
 		if subPool != "" {
-			if ipr, err = getAddressRange(subPool); err != nil {
+			if ipr, err = getAddressRange(subPool, nw); err != nil {
 				return nil, nil, nil, err
 				return nil, nil, nil, err
 			}
 			}
 		}
 		}
@@ -431,9 +431,6 @@ func (a *Allocator) ReleaseAddress(poolID string, address net.IP) error {
 	aSpace.Unlock()
 	aSpace.Unlock()
 
 
 	mask := p.Pool.Mask
 	mask := p.Pool.Mask
-	if p.Range != nil {
-		mask = p.Range.Sub.Mask
-	}
 
 
 	h, err := types.GetHostPartIP(address, mask)
 	h, err := types.GetHostPartIP(address, mask)
 	if err != nil {
 	if err != nil {
@@ -471,7 +468,6 @@ func (a *Allocator) getAddress(nw *net.IPNet, bitmask *bitseq.Handle, prefAddres
 		ordinal = ipToUint64(types.GetMinimalIP(hostPart))
 		ordinal = ipToUint64(types.GetMinimalIP(hostPart))
 		err = bitmask.Set(ordinal)
 		err = bitmask.Set(ordinal)
 	} else {
 	} else {
-		base.IP = ipr.Sub.IP
 		ordinal, err = bitmask.SetAnyInRange(ipr.Start, ipr.End)
 		ordinal, err = bitmask.SetAnyInRange(ipr.Start, ipr.End)
 	}
 	}
 	if err != nil {
 	if err != nil {

+ 53 - 0
libnetwork/ipam/allocator_test.go

@@ -666,6 +666,59 @@ func TestRequestReleaseAddressFromSubPool(t *testing.T) {
 	if !types.CompareIPNet(rp, ip) {
 	if !types.CompareIPNet(rp, ip) {
 		t.Fatalf("Unexpected IP from subpool. Expected: %s. Got: %v.", rp, ip)
 		t.Fatalf("Unexpected IP from subpool. Expected: %s. Got: %v.", rp, ip)
 	}
 	}
+
+	// Request any addresses from subpool after explicit address request
+	unoExp, _ := types.ParseCIDR("10.2.2.0/16")
+	dueExp, _ := types.ParseCIDR("10.2.2.2/16")
+	treExp, _ := types.ParseCIDR("10.2.2.1/16")
+	if poolID, _, _, err = a.RequestPool("rosso", "10.2.0.0/16", "10.2.2.0/24", nil, false); err != nil {
+		t.Fatal(err)
+	}
+	tre, _, err := a.RequestAddress(poolID, treExp.IP, nil)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if !types.CompareIPNet(tre, treExp) {
+		t.Fatalf("Unexpected address: %v", tre)
+	}
+
+	uno, _, err := a.RequestAddress(poolID, nil, nil)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if !types.CompareIPNet(uno, unoExp) {
+		t.Fatalf("Unexpected address: %v", uno)
+	}
+
+	due, _, err := a.RequestAddress(poolID, nil, nil)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if !types.CompareIPNet(due, dueExp) {
+		t.Fatalf("Unexpected address: %v", due)
+	}
+
+	if err = a.ReleaseAddress(poolID, uno.IP); err != nil {
+		t.Fatal(err)
+	}
+	uno, _, err = a.RequestAddress(poolID, nil, nil)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if !types.CompareIPNet(uno, unoExp) {
+		t.Fatalf("Unexpected address: %v", uno)
+	}
+
+	if err = a.ReleaseAddress(poolID, tre.IP); err != nil {
+		t.Fatal(err)
+	}
+	tre, _, err = a.RequestAddress(poolID, nil, nil)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if !types.CompareIPNet(tre, treExp) {
+		t.Fatalf("Unexpected address: %v", tre)
+	}
 }
 }
 
 
 func TestGetAddress(t *testing.T) {
 func TestGetAddress(t *testing.T) {

+ 3 - 3
libnetwork/ipam/utils.go

@@ -15,12 +15,12 @@ const (
 	v6 = 6
 	v6 = 6
 )
 )
 
 
-func getAddressRange(pool string) (*AddressRange, error) {
+func getAddressRange(pool string, masterNw *net.IPNet) (*AddressRange, error) {
 	ip, nw, err := net.ParseCIDR(pool)
 	ip, nw, err := net.ParseCIDR(pool)
 	if err != nil {
 	if err != nil {
 		return nil, ipamapi.ErrInvalidSubPool
 		return nil, ipamapi.ErrInvalidSubPool
 	}
 	}
-	lIP, e := types.GetHostPartIP(nw.IP, nw.Mask)
+	lIP, e := types.GetHostPartIP(nw.IP, masterNw.Mask)
 	if e != nil {
 	if e != nil {
 		return nil, fmt.Errorf("failed to compute range's lowest ip address: %v", e)
 		return nil, fmt.Errorf("failed to compute range's lowest ip address: %v", e)
 	}
 	}
@@ -28,7 +28,7 @@ func getAddressRange(pool string) (*AddressRange, error) {
 	if e != nil {
 	if e != nil {
 		return nil, fmt.Errorf("failed to compute range's broadcast ip address: %v", e)
 		return nil, fmt.Errorf("failed to compute range's broadcast ip address: %v", e)
 	}
 	}
-	hIP, e := types.GetHostPartIP(bIP, nw.Mask)
+	hIP, e := types.GetHostPartIP(bIP, masterNw.Mask)
 	if e != nil {
 	if e != nil {
 		return nil, fmt.Errorf("failed to compute range's highest ip address: %v", e)
 		return nil, fmt.Errorf("failed to compute range's highest ip address: %v", e)
 	}
 	}