mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
cc02894a50
This way we won't vendor test related functions in docker anymore. It also moves netns related functions to a new ns package to be able to call the ns init function in tests. I think this also helps with the overall package isolation. Signed-off-by: David Calavera <david.calavera@gmail.com>
43 lines
900 B
Go
43 lines
900 B
Go
package ns
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"syscall"
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
"github.com/vishvananda/netns"
|
|
)
|
|
|
|
var initNs netns.NsHandle
|
|
|
|
// Init initializes a new network namespace
|
|
func Init() {
|
|
var err error
|
|
initNs, err = netns.Get()
|
|
if err != nil {
|
|
log.Errorf("could not get initial namespace: %v", err)
|
|
}
|
|
}
|
|
|
|
// SetNamespace sets the initial namespace handler
|
|
func SetNamespace() error {
|
|
if err := netns.Set(initNs); err != nil {
|
|
linkInfo, linkErr := getLink()
|
|
if linkErr != nil {
|
|
linkInfo = linkErr.Error()
|
|
}
|
|
|
|
return fmt.Errorf("failed to set to initial namespace, %v, initns fd %d: %v", linkInfo, initNs, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ParseHandlerInt transforms the namespace handler into a integer
|
|
func ParseHandlerInt() int {
|
|
return int(initNs)
|
|
}
|
|
|
|
func getLink() (string, error) {
|
|
return os.Readlink(fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid()))
|
|
}
|