Refactor network creation and initialization into strategies
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
parent
cda4f27f57
commit
609c298810
7 changed files with 211 additions and 107 deletions
|
@ -3,6 +3,7 @@
|
|||
package nsinit
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/dotcloud/docker/pkg/libcontainer"
|
||||
"github.com/dotcloud/docker/pkg/libcontainer/capabilities"
|
||||
|
@ -27,13 +28,10 @@ func Init(container *libcontainer.Container, uncleanRootfs, console string, pipe
|
|||
log.Printf("initializing namespace at %s", rootfs)
|
||||
|
||||
// We always read this as it is a way to sync with the parent as well
|
||||
tempVethName, err := getVethName(pipe)
|
||||
context, err := GetContextFromParent(pipe)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tempVethName != "" {
|
||||
log.Printf("received veth name %s", tempVethName)
|
||||
}
|
||||
if console != "" {
|
||||
log.Printf("setting up console for %s", console)
|
||||
// close pipes so that we can replace it with the pty
|
||||
|
@ -62,7 +60,7 @@ func Init(container *libcontainer.Container, uncleanRootfs, console string, pipe
|
|||
if err := setupNewMountNamespace(rootfs, console, container.ReadonlyFs); err != nil {
|
||||
return fmt.Errorf("setup mount namespace %s", err)
|
||||
}
|
||||
if err := setupVethNetwork(container.Network, tempVethName); err != nil {
|
||||
if err := setupNetwork(container.Network, context); err != nil {
|
||||
return fmt.Errorf("setup networking %s", err)
|
||||
}
|
||||
if err := system.Sethostname(container.Hostname); err != nil {
|
||||
|
@ -145,46 +143,29 @@ func openTerminal(name string, flag int) (*os.File, error) {
|
|||
// setupVethNetwork uses the Network config if it is not nil to initialize
|
||||
// the new veth interface inside the container for use by changing the name to eth0
|
||||
// setting the MTU and IP address along with the default gateway
|
||||
func setupVethNetwork(config *libcontainer.Network, tempVethName string) error {
|
||||
func setupNetwork(config *libcontainer.Network, context libcontainer.Context) error {
|
||||
if config != nil {
|
||||
if err := network.InterfaceDown(tempVethName); err != nil {
|
||||
return fmt.Errorf("interface down %s %s", tempVethName, err)
|
||||
}
|
||||
if err := network.ChangeInterfaceName(tempVethName, "eth0"); err != nil {
|
||||
return fmt.Errorf("change %s to eth0 %s", tempVethName, err)
|
||||
}
|
||||
if err := network.SetInterfaceIp("eth0", config.Address); err != nil {
|
||||
return fmt.Errorf("set eth0 ip %s", err)
|
||||
}
|
||||
if err := network.SetMtu("eth0", config.Mtu); err != nil {
|
||||
return fmt.Errorf("set eth0 mtu to %d %s", config.Mtu, err)
|
||||
}
|
||||
if err := network.InterfaceUp("eth0"); err != nil {
|
||||
return fmt.Errorf("eth0 up %s", err)
|
||||
}
|
||||
if err := network.SetMtu("lo", config.Mtu); err != nil {
|
||||
return fmt.Errorf("set lo mtu to %d %s", config.Mtu, err)
|
||||
}
|
||||
if err := network.InterfaceUp("lo"); err != nil {
|
||||
return fmt.Errorf("lo up %s", err)
|
||||
}
|
||||
if config.Gateway != "" {
|
||||
if err := network.SetDefaultGateway(config.Gateway); err != nil {
|
||||
return fmt.Errorf("set gateway to %s %s", config.Gateway, err)
|
||||
}
|
||||
strategy, err := network.GetStrategy(config.Type)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return strategy.Initialize(config, context)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// getVethName reads from Stdin the temp veth name
|
||||
// sent by the parent processes after the veth pair
|
||||
// has been created and setup
|
||||
func getVethName(pipe io.ReadCloser) (string, error) {
|
||||
func GetContextFromParent(pipe io.ReadCloser) (libcontainer.Context, error) {
|
||||
defer pipe.Close()
|
||||
data, err := ioutil.ReadAll(pipe)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error reading from stdin %s", err)
|
||||
return nil, fmt.Errorf("error reading from stdin %s", err)
|
||||
}
|
||||
return string(data), nil
|
||||
var context libcontainer.Context
|
||||
if len(data) > 0 {
|
||||
if err := json.Unmarshal(data, &context); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Printf("received context %v", context)
|
||||
}
|
||||
return context, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue