From 6ad3e69faf9c88425db559cc8bf51c0a9f83c5e1 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Mon, 18 May 2020 14:47:26 +0000 Subject: [PATCH] Plumb through /proc path config to Docker plugin --- net/netns.go | 6 +++--- net/veth.go | 4 ++-- plugin/net/driver.go | 6 ++++-- plugin/net/watcher.go | 9 ++------- plugin/plugin.go | 9 +++++---- prog/weaver/main.go | 1 + 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/net/netns.go b/net/netns.go index 0a5c2249..56081914 100644 --- a/net/netns.go +++ b/net/netns.go @@ -55,9 +55,9 @@ func WithNetNSByPath(path string, work func() error) error { } func NSPathByPid(pid int) string { - return NSPathByPidWithRoot("/", pid) + return NSPathByPidWithProc("/proc", pid) } -func NSPathByPidWithRoot(root string, pid int) string { - return filepath.Join(root, fmt.Sprintf("/proc/%d/ns/net", pid)) +func NSPathByPidWithProc(procPath string, pid int) string { + return filepath.Join(procPath, fmt.Sprint(pid), "/ns/net") } diff --git a/net/veth.go b/net/veth.go index aa21ff9a..dc2f776a 100644 --- a/net/veth.go +++ b/net/veth.go @@ -236,7 +236,7 @@ func setupIface(procPath, ifaceName, newIfName string) error { } // configureARP is a helper for the Docker plugin which doesn't set the addresses itself -func ConfigureARP(prefix, rootPath string) error { +func ConfigureARP(prefix, procPath string) error { links, err := netlink.LinkList() if err != nil { return err @@ -244,7 +244,7 @@ func ConfigureARP(prefix, rootPath string) error { for _, link := range links { ifName := link.Attrs().Name if strings.HasPrefix(ifName, prefix) { - configureARPCache(rootPath+"/proc", ifName) + configureARPCache(procPath, ifName) if addrs, err := netlink.AddrList(link, netlink.FAMILY_V4); err == nil { for _, addr := range addrs { arping.GratuitousArpOverIfaceByName(addr.IPNet.IP, ifName) diff --git a/plugin/net/driver.go b/plugin/net/driver.go index cea245b7..5af2aa9b 100644 --- a/plugin/net/driver.go +++ b/plugin/net/driver.go @@ -39,9 +39,10 @@ type driver struct { // used only by plugin-v2 forceMulticast bool networks map[string]network + procPath string } -func New(client *docker.Client, weave *weaveapi.Client, name, scope string, dns, isPluginV2, forceMulticast bool) (skel.Driver, error) { +func New(client *docker.Client, weave *weaveapi.Client, name, scope string, dns, isPluginV2, forceMulticast bool, procPath string) (skel.Driver, error) { driver := &driver{ name: name, scope: scope, @@ -51,6 +52,7 @@ func New(client *docker.Client, weave *weaveapi.Client, name, scope string, dns, // make sure that it's used only by plugin-v2 forceMulticast: isPluginV2 && forceMulticast, networks: make(map[string]network), + procPath: procPath, } // Do not start watcher in the case of plugin v2, which prevents us from @@ -134,7 +136,7 @@ func (driver *driver) CreateEndpoint(create *api.CreateEndpointRequest) (*api.Cr // create veths. note we assume endpoint IDs are unique in the first 9 chars name, peerName := vethPair(create.EndpointID) - if _, err := weavenet.CreateAndAttachVeth("/proc", name, peerName, weavenet.WeaveBridgeName, 0, false, true, nil); err != nil { + if _, err := weavenet.CreateAndAttachVeth(driver.procPath, name, peerName, weavenet.WeaveBridgeName, 0, false, true, nil); err != nil { return nil, driver.error("JoinEndpoint", "%s", err) } diff --git a/plugin/net/watcher.go b/plugin/net/watcher.go index 2c386a8c..b763d1a6 100644 --- a/plugin/net/watcher.go +++ b/plugin/net/watcher.go @@ -43,14 +43,9 @@ func (w *watcher) ContainerStarted(id string) { w.driver.warn("ContainerStarted", "unable to register %s with weaveDNS: %s", id, err) } } - rootDir := "/" - if w.driver.isPluginV2 { - // We bind mount host's /proc to /host/proc for plugin-v2 - rootDir = "/host" - } - netNSPath := weavenet.NSPathByPidWithRoot(rootDir, info.State.Pid) + netNSPath := weavenet.NSPathByPidWithProc(w.driver.procPath, info.State.Pid) if err := weavenet.WithNetNSByPath(netNSPath, func() error { - return weavenet.ConfigureARP(weavenet.VethName, rootDir) + return weavenet.ConfigureARP(weavenet.VethName, w.driver.procPath) }); err != nil { w.driver.warn("ContainerStarted", "unable to configure interfaces: %s", err) } diff --git a/plugin/plugin.go b/plugin/plugin.go index c1890193..5b644bf6 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -32,6 +32,7 @@ type Config struct { EnableV2Multicast bool DNS bool DefaultSubnet string + ProcPath string // path to reach host /proc filesystem } type Plugin struct { @@ -62,7 +63,7 @@ func (plugin *Plugin) run(dockerClient *docker.Client, weave *weaveapi.Client, r endChan := make(chan error, 1) if plugin.Socket != "" { - globalListener, err := listenAndServe(dockerClient, weave, plugin.Socket, endChan, "global", false, plugin.DNS, plugin.EnableV2, plugin.EnableV2Multicast) + globalListener, err := listenAndServe(dockerClient, weave, plugin.Socket, endChan, "global", false, plugin.DNS, plugin.EnableV2, plugin.EnableV2Multicast, plugin.ProcPath) if err != nil { return err } @@ -70,7 +71,7 @@ func (plugin *Plugin) run(dockerClient *docker.Client, weave *weaveapi.Client, r defer globalListener.Close() } if plugin.MeshSocket != "" { - meshListener, err := listenAndServe(dockerClient, weave, plugin.MeshSocket, endChan, "local", true, plugin.DNS, plugin.EnableV2, plugin.EnableV2Multicast) + meshListener, err := listenAndServe(dockerClient, weave, plugin.MeshSocket, endChan, "local", true, plugin.DNS, plugin.EnableV2, plugin.EnableV2Multicast, plugin.ProcPath) if err != nil { return err } @@ -87,7 +88,7 @@ func (plugin *Plugin) run(dockerClient *docker.Client, weave *weaveapi.Client, r return <-endChan } -func listenAndServe(dockerClient *docker.Client, weave *weaveapi.Client, address string, endChan chan<- error, scope string, withIpam, dns bool, isPluginV2, forceMulticast bool) (net.Listener, error) { +func listenAndServe(dockerClient *docker.Client, weave *weaveapi.Client, address string, endChan chan<- error, scope string, withIpam, dns bool, isPluginV2, forceMulticast bool, procPath string) (net.Listener, error) { var name string if isPluginV2 { name = pluginV2Name @@ -95,7 +96,7 @@ func listenAndServe(dockerClient *docker.Client, weave *weaveapi.Client, address name = pluginNameFromAddress(address) } - d, err := netplugin.New(dockerClient, weave, name, scope, dns, isPluginV2, forceMulticast) + d, err := netplugin.New(dockerClient, weave, name, scope, dns, isPluginV2, forceMulticast, procPath) if err != nil { return nil, err } diff --git a/prog/weaver/main.go b/prog/weaver/main.go index 7194b205..ca89c642 100644 --- a/prog/weaver/main.go +++ b/prog/weaver/main.go @@ -453,6 +453,7 @@ func main() { pluginConfig.DNS = !noDNS pluginConfig.DefaultSubnet = defaultSubnet.String() + pluginConfig.ProcPath = procPath plugin := plugin.NewPlugin(pluginConfig) // The weave script always waits for a status call to succeed,