mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #21901 from mavenugo/sid
Add container's short-id as default network alias
This commit is contained in:
commit
8adc8c3a68
4 changed files with 49 additions and 10 deletions
|
@ -12,6 +12,7 @@ import (
|
|||
"github.com/docker/docker/container"
|
||||
"github.com/docker/docker/daemon/network"
|
||||
derr "github.com/docker/docker/errors"
|
||||
"github.com/docker/docker/pkg/stringid"
|
||||
"github.com/docker/docker/runconfig"
|
||||
containertypes "github.com/docker/engine-api/types/container"
|
||||
networktypes "github.com/docker/engine-api/types/network"
|
||||
|
@ -485,6 +486,18 @@ func (daemon *Daemon) updateNetworkConfig(container *container.Container, idOrNa
|
|||
if endpointConfig != nil && len(endpointConfig.Aliases) > 0 {
|
||||
return nil, runconfig.ErrUnsupportedNetworkAndAlias
|
||||
}
|
||||
} else {
|
||||
addShortID := true
|
||||
shortID := stringid.TruncateID(container.ID)
|
||||
for _, alias := range endpointConfig.Aliases {
|
||||
if alias == shortID {
|
||||
addShortID = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if addShortID {
|
||||
endpointConfig.Aliases = append(endpointConfig.Aliases, shortID)
|
||||
}
|
||||
}
|
||||
|
||||
n, err := daemon.FindNetwork(idOrName)
|
||||
|
@ -505,6 +518,9 @@ func (daemon *Daemon) updateNetworkConfig(container *container.Container, idOrNa
|
|||
}
|
||||
|
||||
func (daemon *Daemon) connectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings, updateSettings bool) (err error) {
|
||||
if endpointConfig == nil {
|
||||
endpointConfig = &networktypes.EndpointSettings{}
|
||||
}
|
||||
n, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, updateSettings)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -533,10 +549,7 @@ func (daemon *Daemon) connectToNetwork(container *container.Container, idOrName
|
|||
}
|
||||
}
|
||||
}()
|
||||
|
||||
if endpointConfig != nil {
|
||||
container.NetworkSettings.Networks[n.Name()] = endpointConfig
|
||||
}
|
||||
container.NetworkSettings.Networks[n.Name()] = endpointConfig
|
||||
|
||||
if err := daemon.updateEndpointNetworkSettings(container, n, ep); err != nil {
|
||||
return err
|
||||
|
|
|
@ -101,6 +101,9 @@ func (daemon *Daemon) getSize(container *container.Container) (int64, int64) {
|
|||
|
||||
// ConnectToNetwork connects a container to a network
|
||||
func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings) error {
|
||||
if endpointConfig == nil {
|
||||
endpointConfig = &networktypes.EndpointSettings{}
|
||||
}
|
||||
if !container.Running {
|
||||
if container.RemovalInProgress || container.Dead {
|
||||
return errRemovalContainer(container.ID)
|
||||
|
@ -108,9 +111,7 @@ func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName
|
|||
if _, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, true); err != nil {
|
||||
return err
|
||||
}
|
||||
if endpointConfig != nil {
|
||||
container.NetworkSettings.Networks[idOrName] = endpointConfig
|
||||
}
|
||||
container.NetworkSettings.Networks[idOrName] = endpointConfig
|
||||
} else {
|
||||
if err := daemon.connectToNetwork(container, idOrName, endpointConfig, true); err != nil {
|
||||
return err
|
||||
|
|
|
@ -14,6 +14,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/docker/docker/pkg/integration/checker"
|
||||
"github.com/docker/docker/pkg/stringid"
|
||||
"github.com/docker/docker/runconfig"
|
||||
"github.com/docker/engine-api/types"
|
||||
"github.com/docker/engine-api/types/versions/v1p20"
|
||||
|
@ -1348,7 +1349,7 @@ func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectAlias(c *check.C) {
|
|||
dockerCmd(c, "network", "create", "-d", "bridge", "net1")
|
||||
dockerCmd(c, "network", "create", "-d", "bridge", "net2")
|
||||
|
||||
dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo", "busybox", "top")
|
||||
cid, _ := dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo", "busybox", "top")
|
||||
c.Assert(waitRun("first"), check.IsNil)
|
||||
|
||||
dockerCmd(c, "run", "-d", "--net=net1", "--name=second", "busybox", "top")
|
||||
|
@ -1360,6 +1361,10 @@ func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectAlias(c *check.C) {
|
|||
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
// ping first container's short-id alias
|
||||
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", stringid.TruncateID(cid))
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
// connect first container to net2 network
|
||||
dockerCmd(c, "network", "connect", "--alias=bar", "net2", "first")
|
||||
// connect second container to foo2 network with a different alias for first container
|
||||
|
@ -1379,6 +1384,9 @@ func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectAlias(c *check.C) {
|
|||
// ping to net2 scoped alias "bar" must still succeed
|
||||
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "bar")
|
||||
c.Assert(err, check.IsNil)
|
||||
// ping to net2 scoped alias short-id must still succeed
|
||||
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", stringid.TruncateID(cid))
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
// verify the alias option is rejected when running on predefined network
|
||||
out, _, err := dockerCmdWithError("run", "--rm", "--name=any", "--net-alias=any", "busybox", "top")
|
||||
|
|
|
@ -20,6 +20,7 @@ import (
|
|||
|
||||
"github.com/docker/docker/pkg/integration/checker"
|
||||
"github.com/docker/docker/pkg/mount"
|
||||
"github.com/docker/docker/pkg/stringid"
|
||||
"github.com/docker/docker/pkg/stringutils"
|
||||
"github.com/docker/docker/runconfig"
|
||||
"github.com/docker/go-connections/nat"
|
||||
|
@ -285,12 +286,22 @@ func (s *DockerSuite) TestUserDefinedNetworkAlias(c *check.C) {
|
|||
testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
|
||||
dockerCmd(c, "network", "create", "-d", "bridge", "net1")
|
||||
|
||||
dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo1", "--net-alias=foo2", "busybox", "top")
|
||||
cid1, _ := dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo1", "--net-alias=foo2", "busybox", "top")
|
||||
c.Assert(waitRun("first"), check.IsNil)
|
||||
|
||||
dockerCmd(c, "run", "-d", "--net=net1", "--name=second", "busybox", "top")
|
||||
// Check if default short-id alias is added automatically
|
||||
id := strings.TrimSpace(cid1)
|
||||
aliases := inspectField(c, id, "NetworkSettings.Networks.net1.Aliases")
|
||||
c.Assert(aliases, checker.Contains, stringid.TruncateID(id))
|
||||
|
||||
cid2, _ := dockerCmd(c, "run", "-d", "--net=net1", "--name=second", "busybox", "top")
|
||||
c.Assert(waitRun("second"), check.IsNil)
|
||||
|
||||
// Check if default short-id alias is added automatically
|
||||
id = strings.TrimSpace(cid2)
|
||||
aliases = inspectField(c, id, "NetworkSettings.Networks.net1.Aliases")
|
||||
c.Assert(aliases, checker.Contains, stringid.TruncateID(id))
|
||||
|
||||
// ping to first and its network-scoped aliases
|
||||
_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
|
||||
c.Assert(err, check.IsNil)
|
||||
|
@ -298,6 +309,9 @@ func (s *DockerSuite) TestUserDefinedNetworkAlias(c *check.C) {
|
|||
c.Assert(err, check.IsNil)
|
||||
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo2")
|
||||
c.Assert(err, check.IsNil)
|
||||
// ping first container's short-id alias
|
||||
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", stringid.TruncateID(cid1))
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
// Restart first container
|
||||
dockerCmd(c, "restart", "first")
|
||||
|
@ -310,6 +324,9 @@ func (s *DockerSuite) TestUserDefinedNetworkAlias(c *check.C) {
|
|||
c.Assert(err, check.IsNil)
|
||||
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo2")
|
||||
c.Assert(err, check.IsNil)
|
||||
// ping first container's short-id alias
|
||||
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", stringid.TruncateID(cid1))
|
||||
c.Assert(err, check.IsNil)
|
||||
}
|
||||
|
||||
// Issue 9677.
|
||||
|
|
Loading…
Reference in a new issue