Skip to content

Commit

Permalink
Plumb checkAlive through Claim calls
Browse files Browse the repository at this point in the history
  • Loading branch information
bboreham committed Apr 14, 2016
1 parent 0fbbc97 commit 23326ee
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
10 changes: 8 additions & 2 deletions ipam/allocator.go
Expand Up @@ -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, checkAlive, 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,
checkAlive: checkAlive,
noErrorOnUnknown: noErrorOnUnknown,
}
alloc.doOperation(op, &alloc.pendingClaims)
return <-resultChan
}
Expand Down
14 changes: 7 additions & 7 deletions ipam/allocator_test.go
Expand Up @@ -170,31 +170,31 @@ 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.Consense()
// Do an allocate on the other peer, which we will try to claim later
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)
}

Expand Down Expand Up @@ -537,7 +537,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)
}
Expand Down
5 changes: 3 additions & 2 deletions ipam/claim.go
Expand Up @@ -13,6 +13,7 @@ type claim struct {
resultChan chan<- error
ident string
cidr address.CIDR
checkAlive bool
noErrorOnUnknown bool
}

Expand All @@ -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.checkAlive)
c.sendResult(nil)
return true
}
Expand Down Expand Up @@ -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.checkAlive)
c.sendResult(nil)
} else {
c.sendResult(err)
Expand Down
3 changes: 2 additions & 1 deletion ipam/http.go
Expand Up @@ -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
}
Expand Down
13 changes: 10 additions & 3 deletions weave
Expand Up @@ -1365,6 +1365,12 @@ check_overlap() {
# Claim addresses for a container in IPAM. Expects to be called from
# with_container_addresses.
ipam_reclaim() {
for CIDR in $3 ; do
http_call $HTTP_ADDR PUT "/ip/$1/$CIDR?noErrorOnUnknown=true&?check-alive=true" || true
done
}

ipam_reclaim_no_check_alive() {
for CIDR in $3 ; do
http_call $HTTP_ADDR PUT /ip/$1/$CIDR?noErrorOnUnknown=true || true
done
Expand Down Expand Up @@ -1419,7 +1425,7 @@ ipam_cidrs() {
# Assignment of a plain IP address; warn if it clashes but carry on
check_overlap $arg || true
# Abort on failure, but not 4 (=404), which means IPAM is disabled
when_weave_running http_call $HTTP_ADDR PUT /ip/$CONTAINER_ID/$arg || [ $? -eq 4 ] || return 1
when_weave_running http_call $HTTP_ADDR PUT /ip/$CONTAINER_ID/$arg$CHECK_ALIVE || [ $? -eq 4 ] || return 1
fi
ALL_CIDRS="$ALL_CIDRS $arg"
fi
Expand Down Expand Up @@ -1710,7 +1716,8 @@ fetch_router_args() {
populate_router() {
if [ -n "$IPRANGE" ] ; then
# Tell the newly-started weave IP allocator about existing weave IPs
with_container_addresses ipam_reclaim weave:expose $(docker ps -q --no-trunc)
with_container_addresses ipam_reclaim_no_check_alive weave:expose
with_container_addresses ipam_reclaim $(docker ps -q --no-trunc)
fi
if [ -z "$NO_DNS_OPT" ] ; then
# Tell the newly-started weaveDNS about existing weave IPs
Expand Down Expand Up @@ -2127,7 +2134,7 @@ EOF
RES=$(docker restart $1)
CONTAINER=$(container_id $1)
for CIDR in $ALL_CIDRS ; do
call_weave PUT /ip/$CONTAINER/$CIDR
call_weave PUT /ip/$CONTAINER/$CIDR?check-alive=true
done
with_container_netns_or_die $1 attach $ALL_CIDRS
when_weave_running with_container_fqdn $1 put_dns_fqdn $ALL_CIDRS
Expand Down

0 comments on commit 23326ee

Please sign in to comment.