Skip to content

Commit

Permalink
AddInterface method
Browse files Browse the repository at this point in the history
  • Loading branch information
LK4D4 committed Dec 17, 2014
1 parent 68016f1 commit 452a5d6
Show file tree
Hide file tree
Showing 12 changed files with 471 additions and 210 deletions.
20 changes: 11 additions & 9 deletions daemon/create.go
Expand Up @@ -20,6 +20,7 @@ func (daemon *Daemon) ContainerCreate(job *engine.Job) engine.Status {
} else if len(job.Args) > 1 {
return job.Errorf("Usage: %s", job.Name)
}

config := runconfig.ContainerConfigFromJob(job)
if config.Memory != 0 && config.Memory < 4194304 {
return job.Errorf("Minimum memory limit allowed is 4MB")
Expand Down Expand Up @@ -80,21 +81,22 @@ func (daemon *Daemon) attachContainerToDefaultNetwork(cid, name string) (network
return nil, err
}

// We need a name for the default endpoint.
if name == "" {
if name, err = daemon.generateNewName(cid); err != nil {
return nil, err
}
}

// Link the sandbox to the network, thus creating a new endpoint with the
// provided name.
// FIXME:networking Do we need Link() to return the Endpoint?
ep, err := defaultNet.Link(sandbox, name, false /* replace */)
// FIXME:networking Deal with link name length
ep, err := defaultNet.Link(sandbox, name[1:6], false /* replace */)
if err != nil {
return nil, err
}

// FIXME:networking This should be in network.NetController
//for _, iface := range ifaces {
// if err := sandbox.AddIface(iface); err != nil {
// return nil, nil, err
// }
//}

return ep, nil
}

Expand Down Expand Up @@ -159,7 +161,7 @@ func (daemon *Daemon) Create(config *runconfig.Config, hostConfig *runconfig.Hos
}

// Initialize sandboxing environment (ie actual kernel namespaces etc.)
if err := daemon.execDriver.Init(c.ID); err != nil {
if err := daemon.execDriver.Init(c.ID, map[string]string{"net": ""}); err != nil {
return nil, nil, err
}

Expand Down
81 changes: 27 additions & 54 deletions daemon/daemon.go
Expand Up @@ -327,11 +327,10 @@ func (daemon *Daemon) restore() error {

// Create a default network for the default driver.
// FIXME should take parameters from docker -d
if _, err := daemon.networks.NewNetwork("default", []string{}); err != nil {
if net, err := daemon.networks.NewNetwork("default0", []string{}); err != nil {
return fmt.Errorf("failed to create default network using default driver: %v", err)
} else {
// FIXME:networking Returned net is nil
//daemon.networks.DefaultNetworkID = net.Id()
daemon.networks.DefaultNetworkID = net.Id()
}
}

Expand Down Expand Up @@ -430,27 +429,6 @@ func (daemon *Daemon) mergeAndVerifyConfig(config *runconfig.Config, img *image.
return warnings, nil
}

func (daemon *Daemon) generateIdAndName(name string) (string, string, error) {
var (
err error
id = utils.GenerateRandomID()
)

// FIXME: names are now associated with networks
if name == "" {
if name, err = daemon.generateNewName(id); err != nil {
return "", "", err
}
return id, name, nil
}

if name, err = daemon.reserveName(id, name); err != nil {
return "", "", err
}

return id, name, nil
}

func (daemon *Daemon) reserveName(id, name string) (string, error) {
if !validContainerNamePattern.MatchString(name) {
return "", fmt.Errorf("Invalid container name (%s), only %s are allowed", name, validContainerNameChars)
Expand Down Expand Up @@ -551,13 +529,6 @@ func parseSecurityOpt(container *Container, config *runconfig.HostConfig) error
return err
}

// FIXME: FFS only create the data structure here.
// There are already 2 other methods (Daemon.ContainerCreate and Daemon.Create) which
// compete over initializing a container. We don't need a 3d competitor.
func (daemon *Daemon) newContainer(netid, name string, config *runconfig.Config, img *image.Image) (*Container, error) {
return nil, nil
}

func (daemon *Daemon) createRootfs(container *Container, img *image.Image) error {
// Step 1: create the container directory.
// This doubles as a barrier to avoid race conditions.
Expand Down Expand Up @@ -611,34 +582,36 @@ func (daemon *Daemon) GetByName(name string) (*Container, error) {
}

func (daemon *Daemon) Children(name string) (map[string]*Container, error) {
name, err := GetFullContainerName(name)
if err != nil {
return nil, err
}
children := make(map[string]*Container)
return map[string]*Container{}, nil
//name, err := GetFullContainerName(name)
//if err != nil {
//return nil, err
//}
//children := make(map[string]*Container)

err = daemon.containerGraph.Walk(name, func(p string, e *graphdb.Entity) error {
c := daemon.Get(e.ID())
if c == nil {
return fmt.Errorf("Could not get container for name %s and id %s", e.ID(), p)
}
children[p] = c
return nil
}, 0)
//err = daemon.containerGraph.Walk(name, func(p string, e *graphdb.Entity) error {
//c := daemon.Get(e.ID())
//if c == nil {
//return fmt.Errorf("Could not get container for name %s and id %s", e.ID(), p)
//}
//children[p] = c
//return nil
//}, 0)

if err != nil {
return nil, err
}
return children, nil
//if err != nil {
//return nil, err
//}
//return children, nil
}

func (daemon *Daemon) Parents(name string) ([]string, error) {
name, err := GetFullContainerName(name)
if err != nil {
return nil, err
}
return nil, nil
//name, err := GetFullContainerName(name)
//if err != nil {
//return nil, err
//}

return daemon.containerGraph.Parents(name)
//return daemon.containerGraph.Parents(name)
}

func (daemon *Daemon) RegisterLink(parent, child *Container, alias string) error {
Expand Down Expand Up @@ -893,7 +866,7 @@ func NewDaemonFromDirectory(config *Config, eng *engine.Engine) (*Daemon, error)
trustStore: t,
state: rootState,
networks: network.NewController(controllerStates["networks"]),
sandboxes: sandbox.NewController(controllerStates["sandboxes"]),
sandboxes: sandbox.NewController(ed),
}

daemon.extensions = extensions.NewController(controllerStates["extensions"], daemon)
Expand Down
28 changes: 26 additions & 2 deletions daemon/execdriver/driver.go
Expand Up @@ -50,7 +50,7 @@ type ExitStatus struct {
}

type Driver interface {
Init(id string) error // initialize fs storage for container with id
Init(id string, nsList map[string]string) error // initialize fs storage for container with id
Run(c *Command, pipes *Pipes, startCallback StartCallback) (ExitStatus, error) // Run executes the process and blocks until the process exits and returns the exit code
// Exec executes the process in an existing container, blocks until the process exits and returns the exit code
Exec(c *Command, processConfig *ProcessConfig, pipes *Pipes, startCallback StartCallback) (int, error)
Expand All @@ -62,7 +62,7 @@ type Driver interface {
GetPidsForContainer(id string) ([]int, error) // Returns a list of pids for the given container.
Terminate(c *Command) error // kill it with fire
Clean(id string) error // clean all traces of container exec
NetNsPath(id string) string // returns path to network namespace of specific container
AddIface(id string, iface *NetworkSettings) error
}

// Network settings of the container
Expand Down Expand Up @@ -137,3 +137,27 @@ type Command struct {
LxcConfig []string `json:"lxc_config"`
AppArmorProfile string `json:"apparmor_profile"`
}

type NetworkSettings struct {
Name string
// The bridge to use.
Bridge string `json:"bridge,omitempty"`
// MacAddress contains the MAC address to set on the network interface
MacAddress string `json:"mac_address,omitempty"`
// Address contains the IPv4 and mask to set on the network interface
Address string `json:"address,omitempty"`
// IPv6Address contains the IPv6 and mask to set on the network interface
IPv6Address string `json:"ipv6_address,omitempty"`
// Gateway sets the gateway address that is used as the default for the interface
Gateway string `json:"gateway,omitempty"`
// IPv6Gateway sets the ipv6 gateway address that is used as the default for the interface
IPv6Gateway string `json:"ipv6_gateway,omitempty"`
// Mtu sets the mtu value for the interface and will be mirrored on both the host and
// container's interfaces if a pair is created, specifically in the case of type veth
// Note: This does not apply to loopback interfaces.
Mtu int `json:"mtu,omitempty"`
// TxQueueLen sets the tx_queuelen value for the interface and will be mirrored on both the host and
// container's interfaces if a pair is created, specifically in the case of type veth
// Note: This does not apply to loopback interfaces.
TxQueueLen int `json:"txqueuelen,omitempty"`
}
7 changes: 6 additions & 1 deletion daemon/execdriver/lxc/driver.go
Expand Up @@ -50,7 +50,7 @@ func NewDriver(root, initPath string, apparmor bool) (*driver, error) {
}, nil
}

func (d *driver) Init(id string) error {
func (d *driver) Init(id string, nslist map[string]string) error {
return nil
}

Expand Down Expand Up @@ -532,3 +532,8 @@ func (t *TtyConsole) Close() error {
func (d *driver) Exec(c *execdriver.Command, processConfig *execdriver.ProcessConfig, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (int, error) {
return -1, ErrExec
}

// AddIface is not implemented for lxc
func (d *driver) AddIface(id string, iface *execdriver.NetworkSettings) error {
return nil
}
46 changes: 3 additions & 43 deletions daemon/execdriver/native/create.go
Expand Up @@ -80,53 +80,12 @@ func (d *driver) createContainer(c *execdriver.Command) (*libcontainer.Config, e
return container, nil
}

func (d *driver) createNamespaces(id string) (err error) {
nsPath := filepath.Join(d.root, id, "ns")
if err := os.MkdirAll(nsPath, 0655); err != nil {
return err
}
defer func() {
if err != nil {
os.RemoveAll(nsPath)
}
}()
for _, ns := range []string{"net"} {
p := filepath.Join(nsPath, ns)
if err := execdriver.CreateNamespace(ns, p); err != nil {
return err
}
}
return nil
}

func (d *driver) createNetwork(container *libcontainer.Config, c *execdriver.Command) error {
if c.Network.HostNetworking {
container.Namespaces["NEWNET"] = false
return nil
}

//container.Networks = []*libcontainer.Network{
//{
//Mtu: c.Network.Mtu,
//Address: fmt.Sprintf("%s/%d", "127.0.0.1", 0),
//Gateway: "localhost",
//Type: "loopback",
//},
//}

//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),
//MacAddress: c.Network.Interface.MacAddress,
//Gateway: c.Network.Interface.Gateway,
//Type: "veth",
//Bridge: c.Network.Interface.Bridge,
//VethPrefix: "veth",
//}
//container.Networks = append(container.Networks, &vethNetwork)
//}

if c.Network.ContainerID != "" {
d.Lock()
active := d.activeContainers[c.Network.ContainerID]
Expand All @@ -145,10 +104,11 @@ func (d *driver) createNetwork(container *libcontainer.Config, c *execdriver.Com
return nil
}

nsPath := d.NetNsPath(c.ID)
nspath := d.nsPath(c.ID)
netNsPath := filepath.Join(nspath, "net")
container.Networks = append(container.Networks, &libcontainer.Network{
Type: "netns",
NsPath: nsPath,
NsPath: netNsPath,
})
return nil
}
Expand Down

0 comments on commit 452a5d6

Please sign in to comment.