libcontainer/network: add netns strategy
Docker-DCO-1.1-Signed-off-by: Johan Euphrosine <proppy@google.com> (github: proppy)
This commit is contained in:
parent
0424993f6d
commit
e50e99bb8b
2 changed files with 44 additions and 0 deletions
42
libcontainer/network/netns.go
Normal file
42
libcontainer/network/netns.go
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
package network
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
||||||
|
"github.com/dotcloud/docker/pkg/system"
|
||||||
|
)
|
||||||
|
|
||||||
|
// crosbymichael: could make a network strategy that instead of returning veth pair names it returns a pid to an existing network namespace
|
||||||
|
type NetNS struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *NetNS) Create(n *libcontainer.Network, nspid int, context libcontainer.Context) error {
|
||||||
|
nsname, exists := n.Context["nsname"]
|
||||||
|
|
||||||
|
if !exists {
|
||||||
|
return fmt.Errorf("nspath does not exist in network context")
|
||||||
|
}
|
||||||
|
|
||||||
|
context["nspath"] = fmt.Sprintf("/var/run/netns/%s", nsname)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *NetNS) Initialize(config *libcontainer.Network, context libcontainer.Context) error {
|
||||||
|
nspath, exists := context["nspath"]
|
||||||
|
if !exists {
|
||||||
|
return fmt.Errorf("nspath does not exist in network context")
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.OpenFile(nspath, os.O_RDONLY, 0)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed get network namespace fd: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := system.Setns(f.Fd(), syscall.CLONE_NEWNET); err != nil {
|
||||||
|
return fmt.Errorf("failed to setns current network namespace: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ package network
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/dotcloud/docker/pkg/libcontainer"
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,6 +13,7 @@ var (
|
||||||
var strategies = map[string]NetworkStrategy{
|
var strategies = map[string]NetworkStrategy{
|
||||||
"veth": &Veth{},
|
"veth": &Veth{},
|
||||||
"loopback": &Loopback{},
|
"loopback": &Loopback{},
|
||||||
|
"netns": &NetNS{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// NetworkStrategy represents a specific network configuration for
|
// NetworkStrategy represents a specific network configuration for
|
||||||
|
|
Loading…
Add table
Reference in a new issue