From 0273491e6e42229e6869214132d38ea1d8531ccf Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 10 May 2017 15:34:54 +0000 Subject: [PATCH] Pass check-alive flag to IPAM API --- api/ipam.go | 25 +++++++++++++++++-------- plugin/ipam/cni.go | 4 ++-- plugin/ipam/driver.go | 4 ++-- proxy/proxy.go | 4 ++-- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/api/ipam.go b/api/ipam.go index b42e1959..0f72b0b0 100644 --- a/api/ipam.go +++ b/api/ipam.go @@ -3,6 +3,7 @@ package api import ( "fmt" "net" + "net/url" ) // Special token used in place of a container identifier when: @@ -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) } -// 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 ipamValues(checkAlive bool) url.Values { + values := make(url.Values) + if checkAlive { + values.Set("check-alive", "true") + } + return values } -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) +// returns an IP for the ID given, allocating a fresh one if necessary +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, 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 } @@ -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 diff --git a/plugin/ipam/cni.go b/plugin/ipam/cni.go index 628cb1db..c0f97b5b 100644 --- a/plugin/ipam/cni.go +++ b/plugin/ipam/cni.go @@ -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 { diff --git a/plugin/ipam/driver.go b/plugin/ipam/driver.go index 78a650e7..9bb90a2d 100644 --- a/plugin/ipam/driver.go +++ b/plugin/ipam/driver.go @@ -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) @@ -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 diff --git a/proxy/proxy.go b/proxy/proxy.go index 02c9896c..594ade9a 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -552,14 +552,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: