Skip to content

Commit

Permalink
bugfix: judge enablement of libnetwork resolver by network mode
Browse files Browse the repository at this point in the history
Signed-off-by: mathspanda <[email protected]>
  • Loading branch information
mathspanda authored and allencloud committed Oct 26, 2018
1 parent 6a6c159 commit 6de45fe
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 11 deletions.
19 changes: 10 additions & 9 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,7 @@ func (mgr *ContainerManager) prepareContainerNetwork(ctx context.Context, c *Con
}

for name, endpointSetting := range c.NetworkSettings.Networks {
endpoint := mgr.buildContainerEndpoint(c)
endpoint.Name = name
endpoint := mgr.buildContainerEndpoint(c, name)
endpoint.EndpointConfig = endpointSetting
if _, err := mgr.NetworkMgr.EndpointCreate(ctx, endpoint); err != nil {
logrus.Errorf("failed to create endpoint: %v", err)
Expand Down Expand Up @@ -1372,9 +1371,8 @@ func (mgr *ContainerManager) Disconnect(ctx context.Context, containerName, netw
}

c.Lock()
endpoint := mgr.buildContainerEndpoint(c)
endpoint := mgr.buildContainerEndpoint(c, network.Name)
c.Unlock()
endpoint.Name = network.Name
endpoint.EndpointConfig = epConfig
if err := mgr.NetworkMgr.EndpointRemove(ctx, endpoint); err != nil {
// TODO(ziren): it is a trick, we should wrapper sanbox
Expand Down Expand Up @@ -1484,9 +1482,8 @@ func (mgr *ContainerManager) connectToNetwork(ctx context.Context, container *Co
}

container.Lock()
endpoint := mgr.buildContainerEndpoint(container)
endpoint := mgr.buildContainerEndpoint(container, network.Name)
container.Unlock()
endpoint.Name = network.Name
endpoint.EndpointConfig = epConfig
if _, err := mgr.NetworkMgr.EndpointCreate(ctx, endpoint); err != nil {
logrus.Errorf("failed to create endpoint: %v", err)
Expand Down Expand Up @@ -1780,8 +1777,7 @@ func (mgr *ContainerManager) releaseContainerNetwork(c *Container) error {
}

for name, epConfig := range c.NetworkSettings.Networks {
endpoint := mgr.buildContainerEndpoint(c)
endpoint.Name = name
endpoint := mgr.buildContainerEndpoint(c, name)
endpoint.EndpointConfig = epConfig
if err := mgr.NetworkMgr.EndpointRemove(context.Background(), endpoint); err != nil {
// TODO(ziren): it is a trick, we should wrapper "sanbox
Expand Down Expand Up @@ -1811,8 +1807,13 @@ func (mgr *ContainerManager) releaseContainerIOs(containerID string) {

// buildContainerEndpoint builds Endpoints according to container
// caller should lock container when calling this func.
func (mgr *ContainerManager) buildContainerEndpoint(c *Container) *networktypes.Endpoint {
func (mgr *ContainerManager) buildContainerEndpoint(c *Container, name string) *networktypes.Endpoint {
ep := BuildContainerEndpoint(c)
ep.Name = name

if !IsUserDefined(name) {
ep.DisableResolver = true
}

if mgr.containerPlugin != nil {
ep.Priority, ep.DisableResolver, ep.GenericParams = mgr.containerPlugin.PreCreateEndpoint(c.ID, c.Config.Env)
Expand Down
4 changes: 2 additions & 2 deletions daemon/mgr/network_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func IsBridge(mode string) bool {
return mode == "bridge"
}

// IsUserDefined is used to check if network mode is bridge mode.
// IsUserDefined is used to check if network mode is user-created.
func IsUserDefined(mode string) bool {
return !isContainer(mode) && !isHost(mode) && isNone(mode)
return !IsBridge(mode) && !IsContainer(mode) && !IsHost(mode) && !IsNone(mode)
}

// IsDefault indicates whether container uses the default network stack.
Expand Down
68 changes: 68 additions & 0 deletions test/cli_run_dns_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"strings"

"github.com/alibaba/pouch/test/command"
"github.com/alibaba/pouch/test/environment"

"github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
)

// PouchRunDNSSuite is the test suite for run CLI.
type PouchRunDNSSuite struct{}

func init() {
check.Suite(&PouchRunDNSSuite{})
}

// SetUpSuite does common setup in the beginning of each test suite.
func (suite *PouchRunDNSSuite) SetUpSuite(c *check.C) {
SkipIfFalse(c, environment.IsLinux)

environment.PruneAllContainers(apiClient)

PullImage(c, busyboxImage)
}

// TearDownTest does cleanup work in the end of each test.
func (suite *PouchRunDNSSuite) TearDownTest(c *check.C) {
}

// TestRunWithUserDefinedNetwork tests enabling libnetwork resolver if user-defined network.
func (suite *PouchRunSuite) TestRunWithUserDefinedNetwork(c *check.C) {
cname := "TestRunWithUserDefinedNetwork"

// Create a user-defined network
command.PouchRun("network", "create", "--name", cname,
"-d", "bridge",
"--gateway", GateWay,
"--subnet", Subnet).Assert(c, icmd.Success)
defer command.PouchRun("network", "remove", cname)

// Assign the user-defined network to a container
res := command.PouchRun("run", "--name", cname,
"--net", cname, busyboxImage,
"cat", "/etc/resolv.conf")
res.Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, cname)

c.Assert(strings.Contains(res.Stdout(), "nameserver 127.0.0.11"), check.Equals, true)
}

// TestRunWithBridgeNetwork tests disabling libnetwork resolver if not user-defined network.
func (suite *PouchRunSuite) TestRunWithBridgeNetwork(c *check.C) {
cname := "TestRunWithBridgeNetwork"

// Use bridge network if not set --net.
res := command.PouchRun("run", "--name", cname, busyboxImage,
"cat", "/etc/resolv.conf")
res.Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, cname)

hostRes := icmd.RunCommand("cat", "/etc/resolv.conf")
hostRes.Assert(c, icmd.Success)

c.Assert(res.Stdout(), check.Equals, hostRes.Stdout())
}

0 comments on commit 6de45fe

Please sign in to comment.