Introduce ipam plugin
This commit is contained in:
1 parent
c1d2c159f0
commit
ec3de1a072
5 files changed
+97
-12
No files matched your search
@@ -0,0 +1,74 @@
|
||||
package ipamplugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/docker/libnetwork/ipamapi"
|
||||
"github.com/weaveworks/weave/api"
|
||||
. "github.com/weaveworks/weave/common"
|
||||
"github.com/weaveworks/weave/common/docker"
|
||||
)
|
||||
|
||||
const (
|
||||
WeaveContainer = "weave"
|
||||
)
|
||||
|
||||
type ipam struct {
|
||||
client *docker.Client
|
||||
weave *api.Client
|
||||
}
|
||||
|
||||
func NewIpam(client *docker.Client, version string) (ipamapi.Ipam, error) {
|
||||
return &ipam{client: client}, nil
|
||||
}
|
||||
|
||||
func (i *ipam) configureWeaveClient() error {
|
||||
if i.weave != nil {
|
||||
return nil
|
||||
}
|
||||
ip, err := i.client.GetContainerIP(WeaveContainer)
|
||||
if err != nil {
|
||||
Log.Warningf("weave ipam not available: %s", err)
|
||||
return fmt.Errorf("weave ipam not available: %s", err)
|
||||
}
|
||||
i.weave = api.NewClient(ip)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *ipam) GetDefaultAddressSpaces() (string, string, error) {
|
||||
Log.Debugln("GetDefaultAddressSpaces")
|
||||
return "weavelocal", "weaveglobal", nil
|
||||
}
|
||||
|
||||
func (i *ipam) RequestPool(addressSpace, pool, subPool string, options map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
|
||||
Log.Debugln("RequestPool", addressSpace, pool, subPool, options)
|
||||
_, cidr, _ := net.ParseCIDR("10.32.0.0/12")
|
||||
return "weavepool", cidr, nil, nil
|
||||
}
|
||||
|
||||
func (i *ipam) ReleasePool(poolID string) error {
|
||||
Log.Debugln("ReleasePool", poolID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *ipam) RequestAddress(poolID string, address net.IP, options map[string]string) (*net.IPNet, map[string]string, error) {
|
||||
Log.Debugln("RequestAddress", poolID, address, options)
|
||||
// Pass magic string to weave IPAM, which then stores the address under its own string
|
||||
if err := i.configureWeaveClient(); err == nil {
|
||||
ip, err := i.weave.AllocateIP("_")
|
||||
Log.Debugln("allocateIP returned", ip, err)
|
||||
return ip, nil, err
|
||||
} else {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (i *ipam) ReleaseAddress(poolID string, address net.IP) error {
|
||||
Log.Debugln("ReleaseAddress", poolID, address)
|
||||
if err := i.configureWeaveClient(); err == nil {
|
||||
return i.weave.ReleaseIP(address.String())
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -27,19 +27,14 @@ type driver struct {
|
||||
endpoints map[string]struct{}
|
||||
}
|
||||
|
||||
func New(version string, nameserver string) (skel.Driver, error) {
|
||||
client, err := docker.NewClient("unix:///var/run/docker.sock")
|
||||
if err != nil {
|
||||
return nil, errorf("could not connect to docker: %s", err)
|
||||
}
|
||||
|
||||
func New(client *docker.Client, version string, nameserver string) (skel.Driver, error) {
|
||||
driver := &driver{
|
||||
nameserver: nameserver,
|
||||
version: version,
|
||||
endpoints: make(map[string]struct{}),
|
||||
}
|
||||
|
||||
_, err = NewWatcher(client, driver)
|
||||
_, err := NewWatcher(client, driver)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user