Plumb isContainer through Claim calls

This commit is contained in:
Bryan Boreham committed 2016-04-18 10:16:54 +01:00
1 parent 2b159b0c75
commit 5e4ab7f307
5 files changed
+30 -15

No files matched your search

+8 -2
View File
@@ -263,9 +263,15 @@ func (alloc *Allocator) Lookup(ident string, r address.Range) ([]address.CIDR, e
}
// Claim an address that we think we should own (Sync)
func (alloc *Allocator) Claim(ident string, cidr address.CIDR, noErrorOnUnknown bool) error {
func (alloc *Allocator) Claim(ident string, cidr address.CIDR, isContainer, noErrorOnUnknown bool) error {
resultChan := make(chan error)
op := &claim{resultChan: resultChan, ident: ident, cidr: cidr, noErrorOnUnknown: noErrorOnUnknown}
op := &claim{
resultChan: resultChan,
ident: ident,
cidr: cidr,
isContainer: isContainer,
noErrorOnUnknown: noErrorOnUnknown,
}
alloc.doOperation(op, &alloc.pendingClaims)
return <-resultChan
}
+7 -7
View File
@@ -170,7 +170,7 @@ func TestAllocatorClaim(t *testing.T) {
addr1, _ := address.ParseCIDR(testAddr1)
// First claim should trigger "dunno, I'm going to wait"
err := alloc.Claim(container3, addr1, true)
err := alloc.Claim(container3, addr1, true, true)
require.NoError(t, err)
alloc.Prime()
@@ -178,23 +178,23 @@ func TestAllocatorClaim(t *testing.T) {
addrx, err := allocs[0].Allocate(container1, subnet, true, returnFalse)
// Now try the claim again
err = alloc.Claim(container3, addr1, true)
err = alloc.Claim(container3, addr1, true, true)
require.NoError(t, err)
// Check we get this address back if we try an allocate
addr3, _ := alloc.Allocate(container3, subnet, true, returnFalse)
require.Equal(t, testAddr1, address.MakeCIDR(subnet, addr3).String(), "address")
// one more claim should still work
err = alloc.Claim(container3, addr1, true)
err = alloc.Claim(container3, addr1, true, true)
require.NoError(t, err)
// claim for a different container should fail
err = alloc.Claim(container1, addr1, true)
err = alloc.Claim(container1, addr1, true, true)
require.Error(t, err)
// claiming the address allocated on the other peer should fail
err = alloc.Claim(container1, address.MakeCIDR(subnet, addrx), true)
err = alloc.Claim(container1, address.MakeCIDR(subnet, addrx), true, true)
require.Error(t, err, "claiming address allocated on other peer should fail")
// Check an address outside of our universe
addr2, _ := address.ParseCIDR(testAddr2)
err = alloc.Claim(container1, addr2, true)
err = alloc.Claim(container1, addr2, true, true)
require.NoError(t, err)
}
@@ -530,7 +530,7 @@ func TestAllocatorFuzz(t *testing.T) {
addressIndex := rand.Int31n(int32(subnet.Size()))
alloc := allocs[allocIndex]
addr := address.Add(subnet.Addr, address.Offset(addressIndex))
err := alloc.Claim(name, address.MakeCIDR(subnet, addr), true)
err := alloc.Claim(name, address.MakeCIDR(subnet, addr), true, true)
if err == nil {
noteAllocation(allocIndex, name, addr)
}
+3 -2
View File
@@ -13,6 +13,7 @@ type claim struct {
resultChan chan<- error
ident string
cidr address.CIDR
isContainer bool
noErrorOnUnknown bool
}
@@ -35,7 +36,7 @@ func (c *claim) Try(alloc *Allocator) bool {
if !alloc.ring.Contains(c.cidr.Addr) {
// Address not within our universe; assume user knows what they are doing
alloc.infof("Address %s claimed by %s - not in our range", c.cidr, c.ident)
alloc.addOwned(c.ident, c.cidr)
alloc.addOwned(c.ident, c.cidr, c.isContainer)
c.sendResult(nil)
return true
}
@@ -79,7 +80,7 @@ func (c *claim) Try(alloc *Allocator) bool {
if c.ident == "_" { // Special "I don't have a unique ID" identifier
c.ident = c.cidr.String()
}
alloc.addOwned(c.ident, c.cidr)
alloc.addOwned(c.ident, c.cidr, c.isContainer)
c.sendResult(nil)
} else {
c.sendResult(err)
+2 -1
View File
@@ -74,7 +74,8 @@ func (alloc *Allocator) HandleHTTP(router *mux.Router, defaultSubnet address.CID
vars := mux.Vars(r)
if cidr, ok := parseCIDR(w, vars["ip"]+"/"+vars["prefixlen"], false); ok {
noErrorOnUnknown := r.FormValue("noErrorOnUnknown") == "true"
if err := alloc.Claim(vars["id"], cidr, noErrorOnUnknown); err != nil {
checkAlive := r.FormValue("check-alive") == "true"
if err := alloc.Claim(vars["id"], cidr, checkAlive, noErrorOnUnknown); err != nil {
badRequest(w, fmt.Errorf("Unable to claim: %s", err))
return
}