Skip to content

Commit

Permalink
Detect cancelled allocation more cleanly
Browse files Browse the repository at this point in the history
to avoid the weave script trying to parse an error message as a CIDR
  • Loading branch information
bboreham committed Sep 14, 2015
1 parent 33da0b2 commit ae3ff49
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ipam/allocate.go
Expand Up @@ -59,7 +59,7 @@ func (g *allocate) Try(alloc *Allocator) bool {
}

func (g *allocate) Cancel() {
g.resultChan <- allocateResult{0, fmt.Errorf("Allocate request for %s cancelled", g.ident)}
g.resultChan <- allocateResult{0, &errorCancelled{"Allocate", g.ident}}
}

func (g *allocate) ForContainer(ident string) bool {
Expand Down
9 changes: 9 additions & 0 deletions ipam/allocator.go
Expand Up @@ -184,6 +184,15 @@ func hasBeenCancelled(cancelChan <-chan bool) func() bool {
}
}

type errorCancelled struct {
kind string
ident string
}

func (e *errorCancelled) Error() string {
return fmt.Sprintf("%s request for %s cancelled", e.kind, e.ident)
}

// Actor client API

// Allocate (Sync) - get new IP address for container with given name in range
Expand Down
2 changes: 1 addition & 1 deletion ipam/claim.go
Expand Up @@ -95,7 +95,7 @@ func (c *claim) deniedBy(alloc *Allocator, owner router.PeerName) {
}

func (c *claim) Cancel() {
c.sendResult(fmt.Errorf("Operation cancelled."))
c.sendResult(&errorCancelled{"Claim", c.ident})
}

func (c *claim) ForContainer(ident string) bool {
Expand Down
6 changes: 6 additions & 0 deletions ipam/http.go
Expand Up @@ -35,12 +35,18 @@ func doAllocate(alloc *Allocator, dockerCli *docker.Client, w http.ResponseWrite
ident := vars["id"]
addr, err := alloc.Allocate(ident, subnet.HostRange(), closedChan)
if err != nil {
if _, ok := err.(*errorCancelled); ok { // cancellation is not really an error
common.Log.Infoln("[allocator]:", err.Error())
fmt.Fprint(w, "cancelled")
return
}
badRequest(w, err)
return
}
if r.FormValue("check-alive") == "true" && dockerCli != nil && dockerCli.IsContainerNotRunning(ident) {
common.Log.Infof("[allocator] '%s' is not running: freeing %s", ident, addr)
alloc.Free(ident, addr)
fmt.Fprint(w, "cancelled")
return
}

Expand Down
4 changes: 4 additions & 0 deletions weave
Expand Up @@ -919,6 +919,8 @@ ipam_cidrs() {
if [ "$IPAM_CIDRS" = "404 page not found" ] ; then
echo "No IP address supplied (use the -iprange option on 'weave launch' to enable IP address allocation)" >&2
return 1
elif [ "$IPAM_CIDRS" = "cancelled" ] ; then
return 1
fi
ALL_CIDRS="$IPAM_CIDRS"
fi
Expand All @@ -933,6 +935,8 @@ ipam_cidrs() {
if [ "$CIDR" = "404 page not found" ] ; then
echo "IP address allocation must be enabled to use 'net:'" >&2
return 1
elif [ "$IPAM_CIDRS" = "cancelled" ] ; then
return 1
fi
if ! is_cidr "$CIDR" ; then
echo "$CIDR" >&2
Expand Down

0 comments on commit ae3ff49

Please sign in to comment.