Skip to content

Commit

Permalink
Pass check-alive flag to IPAM API
Browse files Browse the repository at this point in the history
  • Loading branch information
bboreham committed May 11, 2017
1 parent 84a6814 commit 19dc7db
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
23 changes: 16 additions & 7 deletions api/ipam.go
Expand Up @@ -3,6 +3,7 @@ package api
import (
"fmt"
"net"
"net/url"
)

// Special token used in place of a container identifier when:
Expand All @@ -14,21 +15,29 @@ import (
// which should be kept in mind if the entry needs to be removed at any point.
const NoContainerID string = "_"

func (client *Client) ipamOp(ID string, op string) (*net.IPNet, error) {
ip, err := client.httpVerb(op, fmt.Sprintf("/ip/%s", ID), nil)
func (client *Client) ipamOp(ID string, op string, values url.Values) (*net.IPNet, error) {
ip, err := client.httpVerb(op, fmt.Sprintf("/ip/%s", ID), values)
if err != nil {
return nil, err
}
return parseIP(ip)
}

func ipamValues(checkAlive bool) url.Values {
values := make(url.Values)
if checkAlive {
values.Set("check-alive", "true")
}
return values
}

// returns an IP for the ID given, allocating a fresh one if necessary
func (client *Client) AllocateIP(ID string) (*net.IPNet, error) {
return client.ipamOp(ID, "POST")
func (client *Client) AllocateIP(ID string, checkAlive bool) (*net.IPNet, error) {
return client.ipamOp(ID, "POST", ipamValues(checkAlive))
}

func (client *Client) AllocateIPInSubnet(ID string, subnet *net.IPNet) (*net.IPNet, error) {
ip, err := client.httpVerb("POST", fmt.Sprintf("/ip/%s/%s", ID, subnet), nil)
func (client *Client) AllocateIPInSubnet(ID string, subnet *net.IPNet, checkAlive bool) (*net.IPNet, error) {
ip, err := client.httpVerb("POST", fmt.Sprintf("/ip/%s/%s", ID, subnet), ipamValues(checkAlive))
if err != nil {
return nil, err
}
Expand All @@ -38,7 +47,7 @@ func (client *Client) AllocateIPInSubnet(ID string, subnet *net.IPNet) (*net.IPN
// returns an IP for the ID given, or nil if one has not been
// allocated
func (client *Client) LookupIP(ID string) (*net.IPNet, error) {
return client.ipamOp(ID, "GET")
return client.ipamOp(ID, "GET", nil)
}

// Claim a specific IP on behalf of the ID
Expand Down
4 changes: 2 additions & 2 deletions plugin/ipam/cni.go
Expand Up @@ -38,14 +38,14 @@ func (i *Ipam) Allocate(args *skel.CmdArgs) (types.Result, error) {
var ipnet *net.IPNet

if conf.Subnet == "" {
ipnet, err = i.weave.AllocateIP(containerID)
ipnet, err = i.weave.AllocateIP(containerID, false)
} else {
var subnet *net.IPNet
subnet, err = types.ParseCIDR(conf.Subnet)
if err != nil {
return nil, fmt.Errorf("subnet given in config, but not parseable: %s", err)
}
ipnet, err = i.weave.AllocateIPInSubnet(containerID, subnet)
ipnet, err = i.weave.AllocateIPInSubnet(containerID, subnet, false)
}

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions plugin/ipam/driver.go
Expand Up @@ -78,7 +78,7 @@ func (i *Ipam) RequestAddress(poolID string, address net.IP, options map[string]
i.logReq("RequestAddress", poolID, address, options)
defer func() { i.logRes("RequestAddress", err, ip) }()
if poolID == "weavepool" { // old-style
ip, err = i.weave.AllocateIP(api.NoContainerID)
ip, err = i.weave.AllocateIP(api.NoContainerID, false)
return
}
subnet, iprange, err := splitPoolID(poolID)
Expand All @@ -92,7 +92,7 @@ func (i *Ipam) RequestAddress(poolID string, address net.IP, options map[string]
}
} else {
// We are lying slightly to IPAM here: the range is not a subnet
if ip, err = i.weave.AllocateIPInSubnet(api.NoContainerID, iprange); err != nil {
if ip, err = i.weave.AllocateIPInSubnet(api.NoContainerID, iprange, false); err != nil {
return
}
ip.Mask = subnet.Mask // fix up the subnet we lied about
Expand Down
4 changes: 2 additions & 2 deletions proxy/proxy.go
Expand Up @@ -548,14 +548,14 @@ func (proxy *Proxy) allocateCIDRs(containerID string, cidrs []string) ([]*net.IP
for _, cidr := range cidrs {
switch {
case cidr == "net:default":
ipnet, err = proxy.weave.AllocateIP(containerID)
ipnet, err = proxy.weave.AllocateIP(containerID, true)
case strings.HasPrefix(cidr, "net:"):
var subnet *net.IPNet
_, subnet, err = net.ParseCIDR(strings.TrimPrefix(cidr, "net:"))
if err != nil {
break
}
ipnet, err = proxy.weave.AllocateIPInSubnet(containerID, subnet)
ipnet, err = proxy.weave.AllocateIPInSubnet(containerID, subnet, true)
case strings.HasPrefix(cidr, "ip:"):
ipnet, err = proxy.claimCIDR(containerID, strings.TrimPrefix(cidr, "ip:"))
default:
Expand Down

0 comments on commit 19dc7db

Please sign in to comment.