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) // 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) 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) alloc.doOperation(op, &alloc.pendingClaims)
return <-resultChan return <-resultChan
} }
+7 -7
View File
@@ -170,7 +170,7 @@ func TestAllocatorClaim(t *testing.T) {
addr1, _ := address.ParseCIDR(testAddr1) addr1, _ := address.ParseCIDR(testAddr1)
// First claim should trigger "dunno, I'm going to wait" // 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) require.NoError(t, err)
alloc.Prime() alloc.Prime()
@@ -178,23 +178,23 @@ func TestAllocatorClaim(t *testing.T) {
addrx, err := allocs[0].Allocate(container1, subnet, true, returnFalse) addrx, err := allocs[0].Allocate(container1, subnet, true, returnFalse)
// Now try the claim again // Now try the claim again
err = alloc.Claim(container3, addr1, true) err = alloc.Claim(container3, addr1, true, true)
require.NoError(t, err) require.NoError(t, err)
// Check we get this address back if we try an allocate // Check we get this address back if we try an allocate
addr3, _ := alloc.Allocate(container3, subnet, true, returnFalse) addr3, _ := alloc.Allocate(container3, subnet, true, returnFalse)
require.Equal(t, testAddr1, address.MakeCIDR(subnet, addr3).String(), "address") require.Equal(t, testAddr1, address.MakeCIDR(subnet, addr3).String(), "address")
// one more claim should still work // one more claim should still work
err = alloc.Claim(container3, addr1, true) err = alloc.Claim(container3, addr1, true, true)
require.NoError(t, err) require.NoError(t, err)
// claim for a different container should fail // claim for a different container should fail
err = alloc.Claim(container1, addr1, true) err = alloc.Claim(container1, addr1, true, true)
require.Error(t, err) require.Error(t, err)
// claiming the address allocated on the other peer should fail // 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") require.Error(t, err, "claiming address allocated on other peer should fail")
// Check an address outside of our universe // Check an address outside of our universe
addr2, _ := address.ParseCIDR(testAddr2) addr2, _ := address.ParseCIDR(testAddr2)
err = alloc.Claim(container1, addr2, true) err = alloc.Claim(container1, addr2, true, true)
require.NoError(t, err) require.NoError(t, err)
} }
@@ -530,7 +530,7 @@ func TestAllocatorFuzz(t *testing.T) {
addressIndex := rand.Int31n(int32(subnet.Size())) addressIndex := rand.Int31n(int32(subnet.Size()))
alloc := allocs[allocIndex] alloc := allocs[allocIndex]
addr := address.Add(subnet.Addr, address.Offset(addressIndex)) 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 { if err == nil {
noteAllocation(allocIndex, name, addr) noteAllocation(allocIndex, name, addr)
} }
+3 -2
View File
@@ -13,6 +13,7 @@ type claim struct {
resultChan chan<- error resultChan chan<- error
ident string ident string
cidr address.CIDR cidr address.CIDR
isContainer bool
noErrorOnUnknown bool noErrorOnUnknown bool
} }
@@ -35,7 +36,7 @@ func (c *claim) Try(alloc *Allocator) bool {
if !alloc.ring.Contains(c.cidr.Addr) { if !alloc.ring.Contains(c.cidr.Addr) {
// Address not within our universe; assume user knows what they are doing // 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.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) c.sendResult(nil)
return true 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 if c.ident == "_" { // Special "I don't have a unique ID" identifier
c.ident = c.cidr.String() c.ident = c.cidr.String()
} }
alloc.addOwned(c.ident, c.cidr) alloc.addOwned(c.ident, c.cidr, c.isContainer)
c.sendResult(nil) c.sendResult(nil)
} else { } else {
c.sendResult(err) 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) vars := mux.Vars(r)
if cidr, ok := parseCIDR(w, vars["ip"]+"/"+vars["prefixlen"], false); ok { if cidr, ok := parseCIDR(w, vars["ip"]+"/"+vars["prefixlen"], false); ok {
noErrorOnUnknown := r.FormValue("noErrorOnUnknown") == "true" 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)) badRequest(w, fmt.Errorf("Unable to claim: %s", err))
return return
} }
+10 -3
View File
@@ -1379,6 +1379,12 @@ check_overlap() {
# Claim addresses for a container in IPAM. Expects to be called from # Claim addresses for a container in IPAM. Expects to be called from
# with_container_addresses. # with_container_addresses.
ipam_reclaim() { 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 for CIDR in $3 ; do
http_call $HTTP_ADDR PUT /ip/$1/$CIDR?noErrorOnUnknown=true || true http_call $HTTP_ADDR PUT /ip/$1/$CIDR?noErrorOnUnknown=true || true
done done
@@ -1433,7 +1439,7 @@ ipam_cidrs() {
# Assignment of a plain IP address; warn if it clashes but carry on # Assignment of a plain IP address; warn if it clashes but carry on
check_overlap $arg || true check_overlap $arg || true
# Abort on failure, but not 4 (=404), which means IPAM is disabled # 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 fi
ALL_CIDRS="$ALL_CIDRS $arg" ALL_CIDRS="$ALL_CIDRS $arg"
fi fi
@@ -1740,7 +1746,8 @@ fetch_router_args() {
populate_router() { populate_router() {
if [ -n "$IPRANGE" ] ; then if [ -n "$IPRANGE" ] ; then
# Tell the newly-started weave IP allocator about existing weave IPs # 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 fi
if [ -z "$NO_DNS_OPT" ] ; then if [ -z "$NO_DNS_OPT" ] ; then
# Tell the newly-started weaveDNS about existing weave IPs # Tell the newly-started weaveDNS about existing weave IPs
@@ -2170,7 +2177,7 @@ EOF
RES=$(docker restart $1) RES=$(docker restart $1)
CONTAINER=$(container_id $1) CONTAINER=$(container_id $1)
for CIDR in $ALL_CIDRS ; do for CIDR in $ALL_CIDRS ; do
call_weave PUT /ip/$CONTAINER/$CIDR call_weave PUT /ip/$CONTAINER/$CIDR?check-alive=true
done done
with_container_netns_or_die $1 attach $ALL_CIDRS with_container_netns_or_die $1 attach $ALL_CIDRS
when_weave_running with_container_fqdn $1 put_dns_fqdn $ALL_CIDRS when_weave_running with_container_fqdn $1 put_dns_fqdn $ALL_CIDRS