Initial implementation of multi-subnet IPAM
This commit is contained in:
1 parent
e6d230042d
commit
2553850ed4
11 files changed
+145
-82
No files matched your search
+19
-7
@@ -37,14 +37,26 @@ func (s *Space) Clear() {
|
||||
s.ours = s.ours[:0]
|
||||
}
|
||||
|
||||
func (s *Space) Allocate() (bool, address.Address) {
|
||||
if len(s.free) == 0 {
|
||||
return false, 0
|
||||
func (s *Space) Allocate(rangeStart, rangeEnd address.Address) (bool, address.Address) {
|
||||
// Need to walk forward to the range, then check free space, then pick an address
|
||||
// Walk down the free spaces, looking for one in range
|
||||
for i := 0; i < len(s.free); i += 2 {
|
||||
start, end := s.free[i], s.free[i+1]
|
||||
if end-1 < rangeStart {
|
||||
continue
|
||||
}
|
||||
if start > rangeEnd {
|
||||
break
|
||||
}
|
||||
res := start
|
||||
if res < rangeStart {
|
||||
res = rangeStart
|
||||
}
|
||||
s.ours = add(s.ours, res, res+1)
|
||||
s.free = subtract(s.free, res, res+1)
|
||||
return true, res
|
||||
}
|
||||
res := s.free[0]
|
||||
s.ours = add(s.ours, res, res+1)
|
||||
s.free = subtract(s.free, res, res+1)
|
||||
return true, res
|
||||
return false, 0
|
||||
}
|
||||
|
||||
func (s *Space) Claim(addr address.Address) error {
|
||||
|
||||
@@ -52,12 +52,12 @@ func TestLowlevel(t *testing.T) {
|
||||
|
||||
s := New()
|
||||
wt.AssertEquals(t, s.NumFreeAddresses(), address.Offset(0))
|
||||
ok, got := s.Allocate()
|
||||
ok, got := s.Allocate(0, 1000)
|
||||
wt.AssertFalse(t, ok, "allocate in empty space should fail")
|
||||
|
||||
s.Add(100, 100)
|
||||
wt.AssertEquals(t, s.NumFreeAddresses(), address.Offset(100))
|
||||
ok, got = s.Allocate()
|
||||
ok, got = s.Allocate(0, 1000)
|
||||
wt.AssertTrue(t, ok && got == 100, "allocate")
|
||||
wt.AssertEquals(t, s.NumFreeAddresses(), address.Offset(99))
|
||||
wt.AssertNoErr(t, s.Claim(150))
|
||||
@@ -86,6 +86,7 @@ func TestLowlevel(t *testing.T) {
|
||||
func TestSpaceAllocate(t *testing.T) {
|
||||
const (
|
||||
testAddr1 = "10.0.3.4"
|
||||
testAddr2 = "10.0.3.5"
|
||||
testAddrx = "10.0.3.19"
|
||||
testAddry = "10.0.9.19"
|
||||
containerID = "deadbeef"
|
||||
@@ -95,13 +96,13 @@ func TestSpaceAllocate(t *testing.T) {
|
||||
wt.AssertEquals(t, space1.NumFreeAddresses(), address.Offset(20))
|
||||
space1.assertInvariants()
|
||||
|
||||
_, addr1 := space1.Allocate()
|
||||
wt.AssertEqualString(t, addr1.String(), testAddr1, "address")
|
||||
_, addr1 := space1.Allocate(ip(testAddr2), ip(testAddrx))
|
||||
wt.AssertEqualString(t, addr1.String(), testAddr2, "address")
|
||||
wt.AssertEquals(t, space1.NumFreeAddresses(), address.Offset(19))
|
||||
space1.assertInvariants()
|
||||
|
||||
_, addr2 := space1.Allocate()
|
||||
wt.AssertFalse(t, addr2.String() == testAddr1, "address")
|
||||
_, addr2 := space1.Allocate(ip(testAddr2), ip(testAddrx))
|
||||
wt.AssertFalse(t, addr2.String() == testAddr2, "address")
|
||||
wt.AssertEquals(t, space1.NumFreeAddresses(), address.Offset(18))
|
||||
wt.AssertEquals(t, space1.NumFreeAddressesInRange(ip(testAddr1), ip(testAddrx)), address.Offset(13))
|
||||
wt.AssertEquals(t, space1.NumFreeAddressesInRange(ip(testAddr1), ip(testAddry)), address.Offset(18))
|
||||
@@ -132,12 +133,12 @@ func TestSpaceFree(t *testing.T) {
|
||||
wt.AssertTrue(t, space.free[pos] == ip(testAddr1) && size == 20, "Wrong space")
|
||||
|
||||
for i := 0; i < 20; i++ {
|
||||
ok, _ := space.Allocate()
|
||||
ok, _ := space.Allocate(ip(testAddr1), ip(testAddry))
|
||||
wt.AssertTrue(t, ok, "Failed to get address")
|
||||
}
|
||||
|
||||
// Check we are full
|
||||
ok, _ := space.Allocate()
|
||||
ok, _ := space.Allocate(ip(testAddr1), ip(testAddry))
|
||||
wt.AssertTrue(t, !ok, "Should have failed to get address")
|
||||
_, size, ok = space.Donate()
|
||||
wt.AssertTrue(t, size == 0, "Wrong space")
|
||||
@@ -222,7 +223,7 @@ func TestDonateHard(t *testing.T) {
|
||||
// Fill a fresh space
|
||||
spaceset := makeSpace(start, size)
|
||||
for i := address.Offset(0); i < size; i++ {
|
||||
ok, _ := spaceset.Allocate()
|
||||
ok, _ := spaceset.Allocate(start, address.Add(start, size))
|
||||
wt.AssertTrue(t, ok, "Failed to get IP!")
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user