Skip to content

Commit

Permalink
switch to go-plugins-helpers for IPAM
Browse files Browse the repository at this point in the history
  • Loading branch information
rade committed Feb 18, 2016
1 parent 497a104 commit caed4a7
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 107 deletions.
21 changes: 21 additions & 0 deletions .gitmodules
Expand Up @@ -58,3 +58,24 @@
[submodule "vendor/github.com/boltdb/bolt"]
path = vendor/github.com/boltdb/bolt
url = https://github.com/boltdb/bolt
[submodule "vendor/github.com/docker/go-plugins-helpers"]
path = vendor/github.com/docker/go-plugins-helpers
url = https://github.com/docker/go-plugins-helpers
[submodule "vendor/github.com/coreos/go-systemd"]
path = vendor/github.com/coreos/go-systemd
url = https://github.com/coreos/go-systemd
[submodule "vendor/github.com/docker/go-connections"]
path = vendor/github.com/docker/go-connections
url = https://github.com/docker/go-connections
[submodule "vendor/golang.org/x/net"]
path = vendor/golang.org/x/net
url = https://go.googlesource.com/net
[submodule "vendor/github.com/armon/go-socks5"]
path = vendor/github.com/armon/go-socks5
url = https://github.com/armon/go-socks5
[submodule "vendor/golang.org/x/tools"]
path = vendor/golang.org/x/tools
url = https://go.googlesource.com/tools
[submodule "vendor/github.com/mgutz/ansi"]
path = vendor/github.com/mgutz/ansi
url = https://github.com/mgutz/ansi
76 changes: 42 additions & 34 deletions plugin/ipam/driver.go
Expand Up @@ -5,8 +5,7 @@ import (
"net"
"strings"

"github.com/docker/libnetwork/discoverapi"
"github.com/docker/libnetwork/ipamapi"
ipamapi "github.com/docker/go-plugins-helpers/ipam"
"github.com/docker/libnetwork/netlabel"
"github.com/weaveworks/weave/api"
. "github.com/weaveworks/weave/common"
Expand All @@ -24,52 +23,70 @@ func NewIpam(weave *api.Client) ipamapi.Ipam {
return &ipam{weave: weave}
}

func (i *ipam) GetDefaultAddressSpaces() (string, string, error) {
func (i *ipam) GetCapabilities() (*ipamapi.CapabilitiesResponse, error) {
Log.Debugln("GetCapabilities")
return &ipamapi.CapabilitiesResponse{RequiresMACAddress: false}, nil
}

func (i *ipam) GetDefaultAddressSpaces() (*ipamapi.AddressSpacesResponse, error) {
Log.Debugln("GetDefaultAddressSpaces")
return "weavelocal", "weaveglobal", nil
return &ipamapi.AddressSpacesResponse{
LocalDefaultAddressSpace: "weavelocal",
GlobalDefaultAddressSpace: "weaveglobal",
}, nil
}

func (i *ipam) RequestPool(addressSpace, pool, subPool string, options map[string]string, v6 bool) (poolname string, subnet *net.IPNet, data map[string]string, err error) {
Log.Debugln("RequestPool", addressSpace, pool, subPool, options)
defer func() { Log.Debugln("RequestPool returning", poolname, subnet, data, err) }()
if pool == "" {
func (i *ipam) RequestPool(req *ipamapi.RequestPoolRequest) (res *ipamapi.RequestPoolResponse, err error) {
Log.Debugln("RequestPool", req)
var subnet *net.IPNet
res = &ipamapi.RequestPoolResponse{}
defer func() { Log.Debugln("RequestPool returning", res, err) }()
if req.Pool == "" {
subnet, err = i.weave.DefaultSubnet()
} else {
_, subnet, err = net.ParseCIDR(pool)
_, subnet, err = net.ParseCIDR(req.Pool)
}
if err != nil {
return
}
iprange := subnet
if subPool != "" {
if _, iprange, err = net.ParseCIDR(subPool); err != nil {
if req.SubPool != "" {
if _, iprange, err = net.ParseCIDR(req.SubPool); err != nil {
return
}
}
// Cunningly-constructed pool "name" which gives us what we need later
poolname = strings.Join([]string{"weave", subnet.String(), iprange.String()}, "-")
res.PoolID = strings.Join([]string{"weave", subnet.String(), iprange.String()}, "-")
res.Pool = subnet.String()
// Pass back a fake "gateway address"; we don't actually use it,
// so just give the network address.
data = map[string]string{netlabel.Gateway: subnet.String()}
res.Data = map[string]string{netlabel.Gateway: subnet.String()}
return
}

func (i *ipam) ReleasePool(poolID string) error {
Log.Debugln("ReleasePool", poolID)
func (i *ipam) ReleasePool(req *ipamapi.ReleasePoolRequest) error {
Log.Debugln("ReleasePool", req)
return nil
}

func (i *ipam) RequestAddress(poolID string, address net.IP, options map[string]string) (ip *net.IPNet, _ map[string]string, err error) {
Log.Debugln("RequestAddress", poolID, address, options)
defer func() { Log.Debugln("allocateIP returned", ip, err) }()
func (i *ipam) RequestAddress(req *ipamapi.RequestAddressRequest) (res *ipamapi.RequestAddressResponse, err error) {
Log.Debugln("RequestAddress", req)
address := net.ParseIP(req.Address)
var ip *net.IPNet
res = &ipamapi.RequestAddressResponse{}
defer func() { Log.Debugln("allocateIP returned", res, err) }()
// If we pass magic string "_" to weave IPAM it stores the address under its own string
if poolID == "weavepool" { // old-style
if req.PoolID == "weavepool" { // old-style
ip, err = i.weave.AllocateIP("_")
if err != nil {
return
}
res.Address = ip.String()
return
}
parts := strings.Split(poolID, "-")
parts := strings.Split(req.PoolID, "-")
if len(parts) != 3 || parts[0] != "weave" {
err = fmt.Errorf("Unrecognized pool ID: %s", poolID)
err = fmt.Errorf("Unrecognized pool ID: %s", req.PoolID)
return
}
var subnet, iprange *net.IPNet
Expand All @@ -91,20 +108,11 @@ func (i *ipam) RequestAddress(poolID string, address net.IP, options map[string]
}
ip.Mask = subnet.Mask // fix up the subnet we lied about
}
res.Address = ip.String()
return
}

func (i *ipam) ReleaseAddress(poolID string, address net.IP) error {
Log.Debugln("ReleaseAddress", poolID, address)
return i.weave.ReleaseIP(address.String())
}

// Functions required by ipamapi "contract" but not actually used.

func (i *ipam) DiscoverNew(discoverapi.DiscoveryType, interface{}) error {
return nil
}

func (i *ipam) DiscoverDelete(discoverapi.DiscoveryType, interface{}) error {
return nil
func (i *ipam) ReleaseAddress(req *ipamapi.ReleaseAddressRequest) error {
Log.Debugln("ReleaseAddress", req.PoolID, req.Address)
return i.weave.ReleaseIP(req.Address)
}
77 changes: 5 additions & 72 deletions plugin/skel/listener.go
Expand Up @@ -8,14 +8,12 @@ import (

"github.com/gorilla/mux"

ipamapi "github.com/docker/go-plugins-helpers/ipam"
"github.com/docker/libnetwork/drivers/remote/api"
"github.com/docker/libnetwork/ipamapi"
iapi "github.com/docker/libnetwork/ipams/remote/api"
)

const (
networkReceiver = "NetworkDriver"
ipamReceiver = ipamapi.PluginEndpointType
)

type Driver interface {
Expand Down Expand Up @@ -61,11 +59,10 @@ func Listen(socket net.Listener, driver Driver, ipamDriver ipamapi.Ipam) error {
}

if ipamDriver != nil {
handleMethod(ipamReceiver, "GetDefaultAddressSpaces", listener.getDefaultAddressSpaces)
handleMethod(ipamReceiver, "RequestPool", listener.requestPool)
handleMethod(ipamReceiver, "ReleasePool", listener.releasePool)
handleMethod(ipamReceiver, "RequestAddress", listener.requestAddress)
handleMethod(ipamReceiver, "ReleaseAddress", listener.releaseAddress)
// FIXME looks like plugins-helpers does not allow a single
// mux to serve multiple APIs. This is problematic, since ATM
// we only have one socket.
ipamapi.NewHandler(ipamDriver).Serve(socket)
}

return http.Serve(socket, router)
Expand Down Expand Up @@ -192,70 +189,6 @@ func (listener *listener) discoverDelete(w http.ResponseWriter, r *http.Request)

// ===

func (listener *listener) getDefaultAddressSpaces(w http.ResponseWriter, r *http.Request) {
local, global, err := listener.i.GetDefaultAddressSpaces()
response := &iapi.GetAddressSpacesResponse{
LocalDefaultAddressSpace: local,
GlobalDefaultAddressSpace: global,
}
objectOrErrorResponse(w, response, err)
}

func (listener *listener) requestPool(w http.ResponseWriter, r *http.Request) {
var rq iapi.RequestPoolRequest
if err := decode(w, r, &rq); err != nil {
return
}
poolID, pool, data, err := listener.i.RequestPool(rq.AddressSpace, rq.Pool, rq.SubPool, rq.Options, rq.V6)
if err != nil {
errorResponse(w, err.Error())
return
}
response := &iapi.RequestPoolResponse{
PoolID: poolID,
Pool: pool.String(),
Data: data,
}
objectResponse(w, response)
}

func (listener *listener) releasePool(w http.ResponseWriter, r *http.Request) {
var rq iapi.ReleasePoolRequest
if err := decode(w, r, &rq); err != nil {
return
}
err := listener.i.ReleasePool(rq.PoolID)
emptyOrErrorResponse(w, err)
}

func (listener *listener) requestAddress(w http.ResponseWriter, r *http.Request) {
var rq iapi.RequestAddressRequest
if err := decode(w, r, &rq); err != nil {
return
}
address, data, err := listener.i.RequestAddress(rq.PoolID, net.ParseIP(rq.Address), rq.Options)
if err != nil {
errorResponse(w, err.Error())
return
}
response := &iapi.RequestAddressResponse{
Address: address.String(),
Data: data,
}
objectResponse(w, response)
}

func (listener *listener) releaseAddress(w http.ResponseWriter, r *http.Request) {
var rq iapi.ReleaseAddressRequest
if err := decode(w, r, &rq); err != nil {
return
}
err := listener.i.ReleaseAddress(rq.PoolID, net.ParseIP(rq.Address))
emptyOrErrorResponse(w, err)
}

// ===

func notFound(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
}
Expand Down
2 changes: 1 addition & 1 deletion prog/plugin/main.go
Expand Up @@ -9,7 +9,7 @@ import (
"os/signal"
"syscall"

"github.com/docker/libnetwork/ipamapi"
ipamapi "github.com/docker/go-plugins-helpers/ipam"
weaveapi "github.com/weaveworks/weave/api"
. "github.com/weaveworks/weave/common"
"github.com/weaveworks/weave/common/docker"
Expand Down
1 change: 1 addition & 0 deletions vendor/github.com/armon/go-socks5
Submodule go-socks5 added at 8a8737
1 change: 1 addition & 0 deletions vendor/github.com/coreos/go-systemd
Submodule go-systemd added at 7b2428
1 change: 1 addition & 0 deletions vendor/github.com/docker/go-connections
Submodule go-connections added at 34b505
1 change: 1 addition & 0 deletions vendor/github.com/docker/go-plugins-helpers
Submodule go-plugins-helpers added at c9f224
1 change: 1 addition & 0 deletions vendor/github.com/mgutz/ansi
Submodule ansi added at c286dc
1 change: 1 addition & 0 deletions vendor/golang.org/x/net
Submodule net added at b6d7b1
1 change: 1 addition & 0 deletions vendor/golang.org/x/tools
Submodule tools added at a17fa8

0 comments on commit caed4a7

Please sign in to comment.