diff --git a/libnetwork/driverapi/driverapi.go b/libnetwork/driverapi/driverapi.go index 48c1bc701a..5f5637b647 100644 --- a/libnetwork/driverapi/driverapi.go +++ b/libnetwork/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/libnetwork/drivers/host/host.go b/libnetwork/drivers/host/host.go index 0c7f00f487..c218fbdd10 100644 --- a/libnetwork/drivers/host/host.go +++ b/libnetwork/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/libnetwork/endpoint.go b/libnetwork/endpoint.go index 63baef2acc..d8e98064fc 100644 --- a/libnetwork/endpoint.go +++ b/libnetwork/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/libnetwork_test.go b/libnetwork/libnetwork_test.go index a66f2c8fcd..fc51f49c97 100644 --- a/libnetwork/libnetwork_test.go +++ b/libnetwork/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/libnetwork/sandbox/namespace_linux.go b/libnetwork/sandbox/namespace_linux.go index 92781b90fe..5d32d504e5 100644 --- a/libnetwork/sandbox/namespace_linux.go +++ b/libnetwork/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 {