Merge pull request #1916 from weaveworks/issues/1734-fixed-ip
Support requesting a specific IP from the plugin
This commit is contained in:
commit
4f97b3ac85
4 files changed
+27
-7
No files matched your search
@@ -32,6 +32,12 @@ func (client *Client) LookupIP(ID string) (*net.IPNet, error) {
|
|||||||
return client.ipamOp(ID, "GET")
|
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
|
// release an IP which is no longer needed
|
||||||
func (client *Client) ReleaseIP(ID string) error {
|
func (client *Client) ReleaseIP(ID string) error {
|
||||||
_, err := client.ipamOp(ID, "DELETE")
|
_, err := client.ipamOp(ID, "DELETE")
|
||||||
|
|||||||
@@ -75,6 +75,9 @@ func (c *claim) Try(alloc *Allocator) bool {
|
|||||||
case "":
|
case "":
|
||||||
if err := alloc.space.Claim(c.addr); err == nil {
|
if err := alloc.space.Claim(c.addr); err == nil {
|
||||||
alloc.debugln("Claimed", c.addr, "for", c.ident)
|
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)
|
alloc.addOwned(c.ident, c.addr)
|
||||||
c.sendResult(nil)
|
c.sendResult(nil)
|
||||||
} else {
|
} else {
|
||||||
@@ -84,6 +87,9 @@ func (c *claim) Try(alloc *Allocator) bool {
|
|||||||
// same identifier is claiming same address; that's OK
|
// same identifier is claiming same address; that's OK
|
||||||
alloc.debugln("Re-Claimed", c.addr, "for", c.ident)
|
alloc.debugln("Re-Claimed", c.addr, "for", c.ident)
|
||||||
c.sendResult(nil)
|
c.sendResult(nil)
|
||||||
|
case c.addr.String():
|
||||||
|
// Address already allocated via "_" name
|
||||||
|
c.sendResult(fmt.Errorf("address %s already in use", c.addr))
|
||||||
default:
|
default:
|
||||||
// Addr already owned by container on this machine
|
// Addr already owned by container on this machine
|
||||||
c.sendResult(fmt.Errorf("address %s is already owned by %s", c.addr.String(), existingIdent))
|
c.sendResult(fmt.Errorf("address %s is already owned by %s", c.addr.String(), existingIdent))
|
||||||
|
|||||||
+14
-7
@@ -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 {
|
if _, subnet, err = net.ParseCIDR(parts[1]); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, iprange, err = net.ParseCIDR(parts[2]); err != nil {
|
if address != nil { // try to claim specific address requested
|
||||||
return
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -265,6 +265,7 @@ func sendError(w http.ResponseWriter, msg string, code int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func errorResponse(w http.ResponseWriter, fmtString string, item ...interface{}) {
|
func errorResponse(w http.ResponseWriter, fmtString string, item ...interface{}) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
json.NewEncoder(w).Encode(map[string]string{
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
"Err": fmt.Sprintf(fmtString, item...),
|
"Err": fmt.Sprintf(fmtString, item...),
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in new issue
Block a user