From 5f9906ddffc25c9af89e20027338892e9f70d60c Mon Sep 17 00:00:00 2001 From: Jana Radhakrishnan Date: Tue, 5 May 2015 04:20:45 +0000 Subject: [PATCH] - Removed sandbox override option from the driver. - Reworked the host network mode support by introducing a new JoinOption. Signed-off-by: Jana Radhakrishnan --- driverapi/driverapi.go | 4 +-- drivers/host/host.go | 4 +-- endpoint.go | 60 +++++++++++++++++++++++--------------- libnetwork_test.go | 3 +- sandbox/namespace_linux.go | 8 ++--- 5 files changed, 45 insertions(+), 34 deletions(-) diff --git a/driverapi/driverapi.go b/driverapi/driverapi.go index 48c1bc7..5f5637b 100644 --- a/driverapi/driverapi.go +++ b/driverapi/driverapi.go @@ -56,7 +56,5 @@ type Driver interface { // JoinInfo represents a set of resources that the driver has the ability to provide during // join time. type JoinInfo struct { - SandboxKey string - NoSandboxCreate bool - HostsPath string + HostsPath string } diff --git a/drivers/host/host.go b/drivers/host/host.go index 0c7f00f..c218fbd 100644 --- a/drivers/host/host.go +++ b/drivers/host/host.go @@ -42,9 +42,7 @@ func (d *driver) EndpointInfo(nid, eid types.UUID) (map[string]interface{}, erro // Join method is invoked when a Sandbox is attached to an endpoint. func (d *driver) Join(nid, eid types.UUID, sboxKey string, options map[string]interface{}) (*driverapi.JoinInfo, error) { jInfo := &driverapi.JoinInfo{ - SandboxKey: sandbox.GenerateKey("host"), - NoSandboxCreate: true, - HostsPath: "/etc/hosts", + HostsPath: "/etc/hosts", } return jInfo, nil diff --git a/endpoint.go b/endpoint.go index 63baef2..d8e9806 100644 --- a/endpoint.go +++ b/endpoint.go @@ -54,18 +54,29 @@ type ContainerData struct { SandboxKey string } -type containerConfig struct { - hostName string - domainName string - generic map[string]interface{} - hostsPath string - ExtraHosts []extraHost - parentUpdates []parentUpdate +// These are the container configs used to customize container /etc/hosts file. +type hostsPathConfig struct { + hostName string + domainName string + hostsPath string + extraHosts []extraHost + parentUpdates []parentUpdate +} + +// These are the container configs used to customize container /etc/resolv.conf file. +type resolvConfPathConfig struct { resolvConfPath string dnsList []string dnsSearchList []string } +type containerConfig struct { + hostsPathConfig + resolvConfPathConfig + generic map[string]interface{} + useDefaultSandBox bool +} + type extraHost struct { name string IP string @@ -168,8 +179,10 @@ func (ep *endpoint) Join(containerID string, options ...EndpointOption) (*Contai ep.container = &containerInfo{ config: containerConfig{ - ExtraHosts: []extraHost{}, - parentUpdates: []parentUpdate{}, + hostsPathConfig: hostsPathConfig{ + extraHosts: []extraHost{}, + parentUpdates: []parentUpdate{}, + }, }} defer func() { if err != nil { @@ -188,6 +201,9 @@ func (ep *endpoint) Join(containerID string, options ...EndpointOption) (*Contai } sboxKey := sandbox.GenerateKey(containerID) + if ep.container.config.useDefaultSandBox { + sboxKey = sandbox.GenerateKey("default") + } joinInfo, err := ep.network.driver.Join(ep.network.id, ep.id, sboxKey, ep.container.config.generic) @@ -211,18 +227,8 @@ func (ep *endpoint) Join(containerID string, options ...EndpointOption) (*Contai return nil, err } - create := true - if joinInfo != nil { - if joinInfo.SandboxKey != "" { - sboxKey = joinInfo.SandboxKey - } - - if joinInfo.NoSandboxCreate { - create = false - } - } - - sb, err := ep.network.ctrlr.sandboxAdd(sboxKey, create) + sb, err := ep.network.ctrlr.sandboxAdd(sboxKey, + !ep.container.config.useDefaultSandBox) if err != nil { return nil, err } @@ -325,7 +331,7 @@ func (ep *endpoint) buildHostsFiles() error { name = name + "." + ep.container.config.domainName } - for _, extraHost := range ep.container.config.ExtraHosts { + for _, extraHost := range ep.container.config.extraHosts { extraContent = append(extraContent, etchosts.Record{Hosts: extraHost.name, IP: extraHost.IP}) } @@ -432,7 +438,7 @@ func JoinOptionHostsPath(path string) EndpointOption { // which is a name and IP as strings. func JoinOptionExtraHost(name string, IP string) EndpointOption { return func(ep *endpoint) { - ep.container.config.ExtraHosts = append(ep.container.config.ExtraHosts, extraHost{name: name, IP: IP}) + ep.container.config.extraHosts = append(ep.container.config.extraHosts, extraHost{name: name, IP: IP}) } } @@ -468,6 +474,14 @@ func JoinOptionDNSSearch(search string) EndpointOption { } } +// JoinOptionUseDefaultSandbox function returns an option setter for using default sandbox to +// be passed to endpoint Join method. +func JoinOptionUseDefaultSandbox() EndpointOption { + return func(ep *endpoint) { + ep.container.config.useDefaultSandBox = true + } +} + // CreateOptionPortMapping function returns an option setter for the container exposed // ports option to be passed to network.CreateEndpoint() method. func CreateOptionPortMapping(portBindings []netutils.PortBinding) EndpointOption { diff --git a/libnetwork_test.go b/libnetwork_test.go index a66f2c8..fc51f49 100644 --- a/libnetwork_test.go +++ b/libnetwork_test.go @@ -103,7 +103,8 @@ func TestHost(t *testing.T) { _, err = ep.Join("host_container", libnetwork.JoinOptionHostname("test"), libnetwork.JoinOptionDomainname("docker.io"), - libnetwork.JoinOptionExtraHost("web", "192.168.0.1")) + libnetwork.JoinOptionExtraHost("web", "192.168.0.1"), + libnetwork.JoinOptionUseDefaultSandbox()) if err != nil { t.Fatal(err) } diff --git a/sandbox/namespace_linux.go b/sandbox/namespace_linux.go index 92781b9..5d32d50 100644 --- a/sandbox/namespace_linux.go +++ b/sandbox/namespace_linux.go @@ -44,8 +44,8 @@ func GenerateKey(containerID string) string { // NewSandbox provides a new sandbox instance created in an os specific way // provided a key which uniquely identifies the sandbox -func NewSandbox(key string, create bool) (Sandbox, error) { - info, err := createNetworkNamespace(key, create) +func NewSandbox(key string, osCreate bool) (Sandbox, error) { + info, err := createNetworkNamespace(key, osCreate) if err != nil { return nil, err } @@ -53,7 +53,7 @@ func NewSandbox(key string, create bool) (Sandbox, error) { return &networkNamespace{path: key, sinfo: info}, nil } -func createNetworkNamespace(path string, create bool) (*Info, error) { +func createNetworkNamespace(path string, osCreate bool) (*Info, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() @@ -67,7 +67,7 @@ func createNetworkNamespace(path string, create bool) (*Info, error) { return nil, err } - if create { + if osCreate { defer netns.Set(origns) newns, err := netns.New() if err != nil {