From cfe26ae346b62095c8aeb31bde92b5152f5c99ed Mon Sep 17 00:00:00 2001 From: Martynas Pumputis Date: Tue, 11 Apr 2017 22:29:11 +0100 Subject: [PATCH] Add an ugly way to match whether network is ours to plugin Docker Plugin V2 does not expose a plugin name to the plugin, so we check for "net-plugin" within driver name of a network to detect whether network is ours. --- plugin/net/driver.go | 28 ++++++++++++++-------------- plugin/plugin.go | 18 +++++++++++++++--- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/plugin/net/driver.go b/plugin/net/driver.go index 14a301a7..6ddadb07 100644 --- a/plugin/net/driver.go +++ b/plugin/net/driver.go @@ -28,23 +28,23 @@ type network struct { } type driver struct { - name string - scope string - docker *docker.Client - dns bool - isPluginV2 bool sync.RWMutex - networks map[string]network + scope string + docker *docker.Client + dns bool + isPluginV2 bool + networks map[string]network + isNetworkOur func(driverName string) bool } -func New(client *docker.Client, weave *weaveapi.Client, name, scope string, dns, isPluginV2 bool) (skel.Driver, error) { +func New(client *docker.Client, weave *weaveapi.Client, scope string, dns, isPluginV2 bool, isNetworkOur func(string) bool) (skel.Driver, error) { driver := &driver{ - name: name, - scope: scope, - docker: client, - dns: dns, - isPluginV2: isPluginV2, - networks: make(map[string]network), + scope: scope, + docker: client, + dns: dns, + isPluginV2: isPluginV2, + networks: make(map[string]network), + isNetworkOur: isNetworkOur, } _, err := NewWatcher(client, weave, driver) @@ -193,7 +193,7 @@ func (driver *driver) findNetworkInfo(id string) (network, error) { if err != nil { return network, err } - return driver.setupNetworkInfo(id, info.Driver == driver.name, info.Options) + return driver.setupNetworkInfo(id, driver.isNetworkOur(info.Driver), info.Options) } func (driver *driver) setupNetworkInfo(id string, isOurs bool, options map[string]string) (network, error) { diff --git a/plugin/plugin.go b/plugin/plugin.go index 8a9c48f6..3343a902 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -18,6 +18,8 @@ import ( "github.com/weaveworks/weave/plugin/skel" ) +const pluginV2Name = "net-plugin" + var Log = common.Log func Start(weaveAPIAddr string, dockerClient *docker.Client, address string, meshAddress string, dns bool, isPluginV2 bool) { @@ -62,9 +64,19 @@ func run(dockerClient *docker.Client, weave *weaveapi.Client, address, meshAddre } func listenAndServe(dockerClient *docker.Client, weave *weaveapi.Client, address string, endChan chan<- error, scope string, withIpam, dns bool, isPluginV2 bool) (net.Listener, error) { - name := strings.TrimSuffix(path.Base(address), ".sock") - // TODO(mp) fix plugin name, as it is used to determine whether network is our - d, err := netplugin.New(dockerClient, weave, name, scope, dns, isPluginV2) + var isNetworkOur func(string) bool + if isPluginV2 { + isNetworkOur = func(driverName string) bool { + return strings.Contains(driverName, pluginV2Name) + } + } else { + name := strings.TrimSuffix(path.Base(address), ".sock") + isNetworkOur = func(driverName string) bool { + return driverName == name + } + } + + d, err := netplugin.New(dockerClient, weave, scope, dns, isPluginV2, isNetworkOur) if err != nil { return nil, err }