1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #4702 from timthelion/Issue#4681-1

Fix Issue#4681
This commit is contained in:
Victor Vieux 2014-03-17 15:31:54 -07:00
commit 30541ade82
11 changed files with 130 additions and 53 deletions

View file

@ -434,28 +434,6 @@ func TestOutput(t *testing.T) {
} }
} }
func TestContainerNetwork(t *testing.T) {
runtime := mkRuntime(t)
defer nuke(runtime)
container, _, err := runtime.Create(
&runconfig.Config{
Image: GetTestImage(runtime).ID,
Cmd: []string{"ping", "-c", "1", "127.0.0.1"},
},
"",
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container)
if err := container.Run(); err != nil {
t.Fatal(err)
}
if code := container.State.GetExitCode(); code != 0 {
t.Fatalf("Unexpected ping 127.0.0.1 exit code %d (expected 0)", code)
}
}
func TestKillDifferentUser(t *testing.T) { func TestKillDifferentUser(t *testing.T) {
runtime := mkRuntime(t) runtime := mkRuntime(t)
defer nuke(runtime) defer nuke(runtime)
@ -1523,6 +1501,53 @@ func TestVolumesFromWithVolumes(t *testing.T) {
} }
} }
func TestContainerNetwork(t *testing.T) {
runtime := mkRuntime(t)
defer nuke(runtime)
container, _, err := runtime.Create(
&runconfig.Config{
Image: GetTestImage(runtime).ID,
// If I change this to ping 8.8.8.8 it fails. Any idea why? - timthelion
Cmd: []string{"ping", "-c", "1", "127.0.0.1"},
},
"",
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container)
if err := container.Run(); err != nil {
t.Fatal(err)
}
if code := container.State.GetExitCode(); code != 0 {
t.Fatalf("Unexpected ping 127.0.0.1 exit code %d (expected 0)", code)
}
}
// Issue #4681
func TestLoopbackFunctionsWhenNetworkingIsDissabled(t *testing.T) {
runtime := mkRuntime(t)
defer nuke(runtime)
container, _, err := runtime.Create(
&runconfig.Config{
Image: GetTestImage(runtime).ID,
Cmd: []string{"ping", "-c", "1", "127.0.0.1"},
NetworkDisabled: true,
},
"",
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container)
if err := container.Run(); err != nil {
t.Fatal(err)
}
if code := container.State.GetExitCode(); code != 0 {
t.Fatalf("Unexpected ping 127.0.0.1 exit code %d (expected 0)", code)
}
}
func TestOnlyLoopbackExistsWhenUsingDisableNetworkOption(t *testing.T) { func TestOnlyLoopbackExistsWhenUsingDisableNetworkOption(t *testing.T) {
eng := NewTestEngine(t) eng := NewTestEngine(t)
runtime := mkRuntimeFromEngine(eng, t) runtime := mkRuntimeFromEngine(eng, t)

View file

@ -0,0 +1,24 @@
package network
import (
"fmt"
"github.com/dotcloud/docker/pkg/libcontainer"
)
// Loopback is a network strategy that provides a basic loopback device
type Loopback struct {
}
func (l *Loopback) Create(n *libcontainer.Network, nspid int, context libcontainer.Context) error {
return nil
}
func (l *Loopback) Initialize(config *libcontainer.Network, context libcontainer.Context) error {
if err := SetMtu("lo", config.Mtu); err != nil {
return fmt.Errorf("set lo mtu to %d %s", config.Mtu, err)
}
if err := InterfaceUp("lo"); err != nil {
return fmt.Errorf("lo up %s", err)
}
return nil
}

View file

@ -11,6 +11,7 @@ var (
var strategies = map[string]NetworkStrategy{ var strategies = map[string]NetworkStrategy{
"veth": &Veth{}, "veth": &Veth{},
"loopback": &Loopback{},
} }
// NetworkStrategy represents a specific network configuration for // NetworkStrategy represents a specific network configuration for

View file

@ -68,12 +68,6 @@ func (v *Veth) Initialize(config *libcontainer.Network, context libcontainer.Con
if err := InterfaceUp("eth0"); err != nil { if err := InterfaceUp("eth0"); err != nil {
return fmt.Errorf("eth0 up %s", err) return fmt.Errorf("eth0 up %s", err)
} }
if err := SetMtu("lo", config.Mtu); err != nil {
return fmt.Errorf("set lo mtu to %d %s", config.Mtu, err)
}
if err := InterfaceUp("lo"); err != nil {
return fmt.Errorf("lo up %s", err)
}
if config.Gateway != "" { if config.Gateway != "" {
if err := SetDefaultGateway(config.Gateway); err != nil { if err := SetDefaultGateway(config.Gateway); err != nil {
return fmt.Errorf("set gateway to %s %s", config.Gateway, err) return fmt.Errorf("set gateway to %s %s", config.Gateway, err)

View file

@ -134,7 +134,11 @@ func setupNetwork(container *libcontainer.Container, context libcontainer.Contex
if err != nil { if err != nil {
return err return err
} }
return strategy.Initialize(config, context)
err1 := strategy.Initialize(config, context)
if err1 != nil {
return err1
}
} }
return nil return nil
} }

View file

@ -364,14 +364,18 @@ func populateCommand(c *Container) {
driverConfig []string driverConfig []string
) )
en = &execdriver.Network{
Mtu: c.runtime.config.Mtu,
Interface: nil,
}
if !c.Config.NetworkDisabled { if !c.Config.NetworkDisabled {
network := c.NetworkSettings network := c.NetworkSettings
en = &execdriver.Network{ en.Interface = &execdriver.NetworkInterface{
Gateway: network.Gateway, Gateway: network.Gateway,
Bridge: network.Bridge, Bridge: network.Bridge,
IPAddress: network.IPAddress, IPAddress: network.IPAddress,
IPPrefixLen: network.IPPrefixLen, IPPrefixLen: network.IPPrefixLen,
Mtu: c.runtime.config.Mtu,
} }
} }

View file

@ -84,11 +84,15 @@ type Driver interface {
// Network settings of the container // Network settings of the container
type Network struct { type Network struct {
Interface *NetworkInterface `json:"interface"` // if interface is nil then networking is disabled
Mtu int `json:"mtu"`
}
type NetworkInterface struct {
Gateway string `json:"gateway"` Gateway string `json:"gateway"`
IPAddress string `json:"ip"` IPAddress string `json:"ip"`
Bridge string `json:"bridge"` Bridge string `json:"bridge"`
IPPrefixLen int `json:"ip_prefix_len"` IPPrefixLen int `json:"ip_prefix_len"`
Mtu int `json:"mtu"`
} }
type Resources struct { type Resources struct {
@ -118,7 +122,7 @@ type Command struct {
WorkingDir string `json:"working_dir"` WorkingDir string `json:"working_dir"`
ConfigPath string `json:"config_path"` // this should be able to be removed when the lxc template is moved into the driver ConfigPath string `json:"config_path"` // this should be able to be removed when the lxc template is moved into the driver
Tty bool `json:"tty"` Tty bool `json:"tty"`
Network *Network `json:"network"` // if network is nil then networking is disabled Network *Network `json:"network"`
Config []string `json:"config"` // generic values that specific drivers can consume Config []string `json:"config"` // generic values that specific drivers can consume
Resources *Resources `json:"resources"` Resources *Resources `json:"resources"`
Mounts []Mount `json:"mounts"` Mounts []Mount `json:"mounts"`

View file

@ -98,13 +98,15 @@ func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallba
DriverName, DriverName,
} }
if c.Network != nil { if c.Network.Interface != nil {
params = append(params, params = append(params,
"-g", c.Network.Gateway, "-g", c.Network.Interface.Gateway,
"-i", fmt.Sprintf("%s/%d", c.Network.IPAddress, c.Network.IPPrefixLen), "-i", fmt.Sprintf("%s/%d", c.Network.Interface.IPAddress, c.Network.Interface.IPPrefixLen),
"-mtu", strconv.Itoa(c.Network.Mtu),
) )
} }
params = append(params,
"-mtu", strconv.Itoa(c.Network.Mtu),
)
if c.User != "" { if c.User != "" {
params = append(params, "-u", c.User) params = append(params, "-u", c.User)

View file

@ -7,17 +7,17 @@ import (
) )
const LxcTemplate = ` const LxcTemplate = `
{{if .Network}} {{if .Network.Interface}}
# network configuration # network configuration
lxc.network.type = veth lxc.network.type = veth
lxc.network.link = {{.Network.Bridge}} lxc.network.link = {{.Network.Interface.Bridge}}
lxc.network.name = eth0 lxc.network.name = eth0
lxc.network.mtu = {{.Network.Mtu}}
{{else}} {{else}}
# network is disabled (-n=false) # network is disabled (-n=false)
lxc.network.type = empty lxc.network.type = empty
lxc.network.flags = up lxc.network.flags = up
{{end}} {{end}}
lxc.network.mtu = {{.Network.Mtu}}
# root filesystem # root filesystem
{{$ROOTFS := .Rootfs}} {{$ROOTFS := .Rootfs}}

View file

@ -43,6 +43,10 @@ func TestLXCConfig(t *testing.T) {
Memory: int64(mem), Memory: int64(mem),
CpuShares: int64(cpu), CpuShares: int64(cpu),
}, },
Network: &execdriver.Network{
Mtu: 1500,
Interface: nil,
},
} }
p, err := driver.generateLXCConfig(command) p, err := driver.generateLXCConfig(command)
if err != nil { if err != nil {
@ -75,6 +79,10 @@ func TestCustomLxcConfig(t *testing.T) {
"lxc.utsname = docker", "lxc.utsname = docker",
"lxc.cgroup.cpuset.cpus = 0,1", "lxc.cgroup.cpuset.cpus = 0,1",
}, },
Network: &execdriver.Network{
Mtu: 1500,
Interface: nil,
},
} }
p, err := driver.generateLXCConfig(command) p, err := driver.generateLXCConfig(command)

View file

@ -19,19 +19,30 @@ func createContainer(c *execdriver.Command) *libcontainer.Container {
container.WorkingDir = c.WorkingDir container.WorkingDir = c.WorkingDir
container.Env = c.Env container.Env = c.Env
if c.Network != nil { loopbackNetwork := libcontainer.Network{
container.Networks = []*libcontainer.Network{
{
Mtu: c.Network.Mtu, Mtu: c.Network.Mtu,
Address: fmt.Sprintf("%s/%d", c.Network.IPAddress, c.Network.IPPrefixLen), Address: fmt.Sprintf("%s/%d", "127.0.0.1", 0),
Gateway: c.Network.Gateway, Gateway: "localhost",
Type: "loopback",
Context: libcontainer.Context{},
}
container.Networks = []*libcontainer.Network{
&loopbackNetwork,
}
if c.Network.Interface != nil {
vethNetwork := libcontainer.Network{
Mtu: c.Network.Mtu,
Address: fmt.Sprintf("%s/%d", c.Network.Interface.IPAddress, c.Network.Interface.IPPrefixLen),
Gateway: c.Network.Interface.Gateway,
Type: "veth", Type: "veth",
Context: libcontainer.Context{ Context: libcontainer.Context{
"prefix": "veth", "prefix": "veth",
"bridge": c.Network.Bridge, "bridge": c.Network.Interface.Bridge,
},
}, },
} }
container.Networks = append(container.Networks, &vethNetwork)
} }
container.Cgroups.Name = c.ID container.Cgroups.Name = c.ID