Merge pull request #1916 from weaveworks/issues/1734-fixed-ip

Support requesting a specific IP from the plugin
This commit is contained in:
Adam Harrison committed 2016-01-29 14:22:23 +00:00
commit 4f97b3ac85
4 files changed
+27 -7

No files matched your search

+6
View File
@@ -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")
+6
View File
@@ -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 {
@@ -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))
+14 -7
View File
@@ -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
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
}
// 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
return
}
+1
View File
@@ -265,6 +265,7 @@ func sendError(w http.ResponseWriter, msg string, code int) {
}
func errorResponse(w http.ResponseWriter, fmtString string, item ...interface{}) {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{
"Err": fmt.Sprintf(fmtString, item...),
})