Find weave veths by looking at what is connected to the bridge, not by name

and don't even look at containers running in the root namespace
This commit is contained in:
Bryan Boreham committed 2016-03-17 12:41:37 +00:00
1 parent 011fb55e5f
commit 2d6a916b2d
1 file changed
+33 -1
+33 -1
View File
@@ -1,6 +1,7 @@
package common
import (
"fmt"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
"net"
@@ -101,8 +102,39 @@ func linkToNetDev(link netlink.Link) (*NetDev, error) {
// Lookup the weave interface of a container
func GetWeaveNetDevs(processID int) ([]NetDev, error) {
// Bail out if this container is running in the root namespace
nsToplevel, _ := netns.GetFromPid(1)
nsContainr, _ := netns.GetFromPid(processID)
if nsToplevel.Equal(nsContainr) {
return nil, nil
}
var weaveBridge netlink.Link
forEachLink(func(link netlink.Link) error {
if link.Attrs().Name == "weave" {
weaveBridge = link
}
return nil
})
if weaveBridge == nil {
return nil, fmt.Errorf("Cannot find weave bridge")
}
// Scan devices in root namespace to find those attached to weave bridge
indexes := make(map[int]struct{})
err := forEachLink(func(link netlink.Link) error {
if link.Attrs().MasterIndex == weaveBridge.Attrs().Index {
indexes[link.Attrs().Index] = struct{}{}
}
return nil
})
if err != nil {
return nil, err
}
return FindNetDevs(processID, func(link netlink.Link) bool {
return strings.HasPrefix(link.Attrs().Name, "ethwe")
// For checking, rely on index number of veth end inside namespace being -1 of end outside
_, isveth := link.(*netlink.Veth)
_, found := indexes[link.Attrs().Index+1]
return isveth && found
})
}