Skip to content

Commit

Permalink
Claim address requested by caller
Browse files Browse the repository at this point in the history
  • Loading branch information
bboreham committed Jan 27, 2016
1 parent 31f736b commit 610a90c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
6 changes: 6 additions & 0 deletions api/ipam.go
Expand Up @@ -32,6 +32,12 @@ func (client *Client) LookupIP(ID string) (*net.IPNet, error) {
return client.ipamOp(ID, "GET")
}

// Claim a specific IP on behalf of the ID
func (client *Client) ClaimIP(ID string, addr net.IP) error {
_, err := client.httpVerb("PUT", fmt.Sprintf("/ip/%s/%s", ID, addr), nil)
return err
}

// release an IP which is no longer needed
func (client *Client) ReleaseIP(ID string) error {
_, err := client.ipamOp(ID, "DELETE")
Expand Down
6 changes: 6 additions & 0 deletions ipam/claim.go
Expand Up @@ -75,6 +75,9 @@ func (c *claim) Try(alloc *Allocator) bool {
case "":
if err := alloc.space.Claim(c.addr); err == nil {
alloc.debugln("Claimed", c.addr, "for", c.ident)
if c.ident == "_" { // Special "I don't have a unique ID" identifier
c.ident = c.addr.String()
}
alloc.addOwned(c.ident, c.addr)
c.sendResult(nil)
} else {
Expand All @@ -84,6 +87,9 @@ func (c *claim) Try(alloc *Allocator) bool {
// same identifier is claiming same address; that's OK
alloc.debugln("Re-Claimed", c.addr, "for", c.ident)
c.sendResult(nil)
case c.addr.String():
// Address already allocated via "_" name
c.sendResult(fmt.Errorf("address %s already in use", c.addr))
default:
// Addr already owned by container on this machine
c.sendResult(fmt.Errorf("address %s is already owned by %s", c.addr.String(), existingIdent))
Expand Down
21 changes: 14 additions & 7 deletions plugin/ipam/driver.go
Expand Up @@ -78,14 +78,21 @@ func (i *ipam) RequestAddress(poolID string, address net.IP, options map[string]
if _, subnet, err = net.ParseCIDR(parts[1]); err != nil {
return
}
if _, iprange, err = net.ParseCIDR(parts[2]); err != nil {
return
}
// We are lying slightly to IPAM here: the range is not a subnet
if ip, err = i.weave.AllocateIPInSubnet("_", iprange); err != nil {
return
if address != nil { // try to claim specific address requested
if err = i.weave.ClaimIP("_", address); err != nil {
return
}
ip = &net.IPNet{IP: address, Mask: subnet.Mask}
} else {
if _, iprange, err = net.ParseCIDR(parts[2]); err != nil {
return
}
// We are lying slightly to IPAM here: the range is not a subnet
if ip, err = i.weave.AllocateIPInSubnet("_", iprange); err != nil {
return
}
ip.Mask = subnet.Mask // fix up the subnet we lied about
}
ip.Mask = subnet.Mask // fix up the subnet we lied about
return
}

Expand Down

0 comments on commit 610a90c

Please sign in to comment.