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

Refactor out interface specific information from execdriver.Network

Docker-DCO-1.1-Signed-off-by: Timothy Hobbs <timothyhobbs@seznam.cz> (github: https://github.com/timthelion)
This commit is contained in:
Timothy Hobbs 2014-03-16 20:52:27 +01:00
parent 353df19ab7
commit 659b719aa6
6 changed files with 35 additions and 21 deletions

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,8 +122,8 @@ 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

@ -20,11 +20,7 @@ func createContainer(c *execdriver.Command) *libcontainer.Container {
container.Env = c.Env container.Env = c.Env
loopbackNetwork := libcontainer.Network{ loopbackNetwork := libcontainer.Network{
// Using constants here because Mtu: c.Network.Mtu,
// when networking is disabled
// These settings simply don't exist:
// https://github.com/dotcloud/docker/blob/c7ea6e5da80af3d9ba7558f876efbf0801d988d8/runtime/container.go#L367
Mtu: 1500,
Address: fmt.Sprintf("%s/%d", "127.0.0.1", 0), Address: fmt.Sprintf("%s/%d", "127.0.0.1", 0),
Gateway: "localhost", Gateway: "localhost",
Type: "loopback", Type: "loopback",
@ -35,15 +31,15 @@ func createContainer(c *execdriver.Command) *libcontainer.Container {
&loopbackNetwork, &loopbackNetwork,
} }
if c.Network != nil { if c.Network.Interface != nil {
vethNetwork := libcontainer.Network{ vethNetwork := 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", c.Network.Interface.IPAddress, c.Network.Interface.IPPrefixLen),
Gateway: c.Network.Gateway, 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.Networks = append(container.Networks, &vethNetwork)