-
Notifications
You must be signed in to change notification settings - Fork 712
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3207 from weaveworks/fetch-namespace-ips
Fetch container IPs directly from the namespace instead of calling 'weave ps'
- Loading branch information
Showing
223 changed files
with
42,648 additions
and
7,092 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// withNetNS function requires a fix that first appeared in Go version 1.10 | ||
// +build go1.10 | ||
|
||
package docker | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"runtime" | ||
|
||
"github.com/vishvananda/netlink" | ||
"github.com/vishvananda/netns" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// Code adapted from github.com/weaveworks/weave/net/netdev.go | ||
|
||
// Return any non-local IP addresses for processID if in a non-root namespace | ||
func namespaceIPAddresses(processID int) ([]*net.IPNet, error) { | ||
// Ignore if this process is running in the root namespace | ||
netnsRoot, err := netns.GetFromPid(1) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to open root namespace: %s", err) | ||
} | ||
defer netnsRoot.Close() | ||
netnsContainer, err := netns.GetFromPid(processID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer netnsContainer.Close() | ||
if netnsRoot.Equal(netnsContainer) { | ||
return nil, nil | ||
} | ||
|
||
var cidrs []*net.IPNet | ||
err = withNetNS(netnsContainer, func() error { | ||
cidrs, err = allNonLocalAddresses() | ||
return err | ||
}) | ||
|
||
return cidrs, err | ||
} | ||
|
||
// return all non-local IP addresses from the current namespace | ||
func allNonLocalAddresses() ([]*net.IPNet, error) { | ||
var cidrs []*net.IPNet | ||
links, err := netlink.LinkList() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, link := range links { | ||
addrs, err := netlink.AddrList(link, netlink.FAMILY_ALL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, addr := range addrs { | ||
// Exclude link-local ipv6 addresses, localhost, etc. Hope this is the correct test. | ||
if addr.Scope == unix.RT_SCOPE_UNIVERSE { | ||
cidrs = append(cidrs, addr.IPNet) | ||
} | ||
} | ||
} | ||
return cidrs, nil | ||
} | ||
|
||
// Run the 'work' function in a different network namespace | ||
func withNetNS(ns netns.NsHandle, work func() error) error { | ||
runtime.LockOSThread() | ||
defer runtime.UnlockOSThread() | ||
|
||
oldNs, err := netns.Get() | ||
if err == nil { | ||
defer oldNs.Close() | ||
|
||
err = netns.Set(ns) | ||
if err == nil { | ||
defer netns.Set(oldNs) | ||
|
||
err = work() | ||
} | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// +build !linux | ||
|
||
package docker | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
) | ||
|
||
func namespaceIPAddresses(processID int) ([]*net.IPNet, error) { | ||
return nil, errors.New("namespaceIPAddresses not implemented on this platform") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.