Resolve weave IP every time we need it, in case it changed
This commit is contained in:
1 parent
a6f07fbaac
commit
ecd93bbfe4
3 files changed
+26
-51
No files matched your search
+14
-1
@@ -18,10 +18,19 @@ const (
|
|||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
baseUrl string
|
baseUrl string
|
||||||
|
resolve func() (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) httpVerb(verb string, url string, values url.Values) (string, error) {
|
func (client *Client) httpVerb(verb string, url string, values url.Values) (string, error) {
|
||||||
url = client.baseUrl + url
|
baseUrl := client.baseUrl
|
||||||
|
if client.resolve != nil {
|
||||||
|
addr, err := client.resolve()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
baseUrl = fmt.Sprintf("http://%s:%d", addr, WeaveHttpPort)
|
||||||
|
}
|
||||||
|
url = baseUrl + url
|
||||||
Log.Debugf("weave %s to %s with %v", verb, url, values)
|
Log.Debugf("weave %s to %s with %v", verb, url, values)
|
||||||
var body io.Reader
|
var body io.Reader
|
||||||
if values != nil {
|
if values != nil {
|
||||||
@@ -54,6 +63,10 @@ func NewClient(addr string) *Client {
|
|||||||
return &Client{baseUrl: fmt.Sprintf("http://%s:%d", addr, WeaveHttpPort)}
|
return &Client{baseUrl: fmt.Sprintf("http://%s:%d", addr, WeaveHttpPort)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewClientWithResolver(resolver func() (string, error)) *Client {
|
||||||
|
return &Client{resolve: resolver}
|
||||||
|
}
|
||||||
|
|
||||||
func (client *Client) Connect(remote string) error {
|
func (client *Client) Connect(remote string) error {
|
||||||
_, err := client.httpVerb("POST", "/connect", url.Values{"peer": {remote}})
|
_, err := client.httpVerb("POST", "/connect", url.Values{"peer": {remote}})
|
||||||
return err
|
return err
|
||||||
|
|||||||
+9
-34
@@ -1,7 +1,6 @@
|
|||||||
package ipamplugin
|
package ipamplugin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/docker/libnetwork/ipamapi"
|
"github.com/docker/libnetwork/ipamapi"
|
||||||
@@ -20,20 +19,8 @@ type ipam struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewIpam(client *docker.Client, version string) (ipamapi.Ipam, error) {
|
func NewIpam(client *docker.Client, version string) (ipamapi.Ipam, error) {
|
||||||
return &ipam{client: client}, nil
|
resolver := func() (string, error) { return client.GetContainerIP(WeaveContainer) }
|
||||||
}
|
return &ipam{client: client, weave: api.NewClientWithResolver(resolver)}, 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) {
|
func (i *ipam) GetDefaultAddressSpaces() (string, string, error) {
|
||||||
@@ -43,13 +30,9 @@ func (i *ipam) GetDefaultAddressSpaces() (string, string, error) {
|
|||||||
|
|
||||||
func (i *ipam) RequestPool(addressSpace, pool, subPool string, options map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
|
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)
|
Log.Debugln("RequestPool", addressSpace, pool, subPool, options)
|
||||||
if err := i.configureWeaveClient(); err == nil {
|
cidr, err := i.weave.DefaultSubnet()
|
||||||
cidr, err := i.weave.DefaultSubnet()
|
Log.Debugln("RequestPool returning ", cidr, err)
|
||||||
Log.Debugln("RequestPool returning ", cidr, err)
|
return "weavepool", cidr, nil, err
|
||||||
return "weavepool", cidr, nil, err
|
|
||||||
} else {
|
|
||||||
return "", nil, nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *ipam) ReleasePool(poolID string) error {
|
func (i *ipam) ReleasePool(poolID string) error {
|
||||||
@@ -60,20 +43,12 @@ func (i *ipam) ReleasePool(poolID string) error {
|
|||||||
func (i *ipam) RequestAddress(poolID string, address net.IP, options map[string]string) (*net.IPNet, map[string]string, error) {
|
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)
|
Log.Debugln("RequestAddress", poolID, address, options)
|
||||||
// Pass magic string to weave IPAM, which then stores the address under its own string
|
// 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("_")
|
||||||
ip, err := i.weave.AllocateIP("_")
|
Log.Debugln("allocateIP returned", ip, err)
|
||||||
Log.Debugln("allocateIP returned", ip, err)
|
return ip, nil, err
|
||||||
return ip, nil, err
|
|
||||||
} else {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *ipam) ReleaseAddress(poolID string, address net.IP) error {
|
func (i *ipam) ReleaseAddress(poolID string, address net.IP) error {
|
||||||
Log.Debugln("ReleaseAddress", poolID, address)
|
Log.Debugln("ReleaseAddress", poolID, address)
|
||||||
if err := i.configureWeaveClient(); err == nil {
|
return i.weave.ReleaseIP(address.String())
|
||||||
return i.weave.ReleaseIP(address.String())
|
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+3
-16
@@ -22,7 +22,9 @@ type Watcher interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewWatcher(client *docker.Client, driver *driver) (Watcher, error) {
|
func NewWatcher(client *docker.Client, driver *driver) (Watcher, error) {
|
||||||
w := &watcher{client: client, driver: driver}
|
resolver := func() (string, error) { return client.GetContainerIP(WeaveContainer) }
|
||||||
|
w := &watcher{client: client, driver: driver,
|
||||||
|
weave: weaveapi.NewClientWithResolver(resolver)}
|
||||||
err := client.AddObserver(w)
|
err := client.AddObserver(w)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -31,18 +33,6 @@ func NewWatcher(client *docker.Client, driver *driver) (Watcher, error) {
|
|||||||
return w, nil
|
return w, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *watcher) haveWeaveClient() bool {
|
|
||||||
if w.weave == nil {
|
|
||||||
dnsip, err := w.client.GetContainerIP(WeaveContainer)
|
|
||||||
if err != nil {
|
|
||||||
Log.Warningf("nameserver not available: %s", err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
w.weave = weaveapi.NewClient(dnsip)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *watcher) ContainerStarted(id string) {
|
func (w *watcher) ContainerStarted(id string) {
|
||||||
Log.Debugf("Container started %s", id)
|
Log.Debugf("Container started %s", id)
|
||||||
info, err := w.client.InspectContainer(id)
|
info, err := w.client.InspectContainer(id)
|
||||||
@@ -50,9 +40,6 @@ func (w *watcher) ContainerStarted(id string) {
|
|||||||
Log.Warningf("error inspecting container: %s", err)
|
Log.Warningf("error inspecting container: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !w.haveWeaveClient() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// check that it's on our network, via the endpointID
|
// check that it's on our network, via the endpointID
|
||||||
for _, net := range info.NetworkSettings.Networks {
|
for _, net := range info.NetworkSettings.Networks {
|
||||||
if w.driver.HasEndpoint(net.EndpointID) {
|
if w.driver.HasEndpoint(net.EndpointID) {
|
||||||
|
|||||||
Reference in new issue
Block a user