mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
update network settings on container creating
To make docker inspect return a consistent result of networksettings for created container and stopped container, it's bettew to update the network settings on container creating. Signed-off-by: Lei Jitang <leijitang@huawei.com>
This commit is contained in:
parent
94026969b4
commit
c427131c94
4 changed files with 55 additions and 14 deletions
|
@ -529,6 +529,29 @@ func (daemon *Daemon) updateNetwork(container *container.Container) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// updateContainerNetworkSettings update the network settings
|
||||
func (daemon *Daemon) updateContainerNetworkSettings(container *container.Container) error {
|
||||
mode := container.HostConfig.NetworkMode
|
||||
if container.Config.NetworkDisabled || mode.IsContainer() {
|
||||
return nil
|
||||
}
|
||||
|
||||
networkName := mode.NetworkName()
|
||||
if mode.IsDefault() {
|
||||
networkName = daemon.netController.Config().Daemon.DefaultNetwork
|
||||
}
|
||||
if mode.IsUserDefined() {
|
||||
n, err := daemon.FindNetwork(networkName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
networkName = n.Name()
|
||||
}
|
||||
container.NetworkSettings.Networks = make(map[string]*networktypes.EndpointSettings)
|
||||
container.NetworkSettings.Networks[networkName] = new(networktypes.EndpointSettings)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (daemon *Daemon) allocateNetwork(container *container.Container) error {
|
||||
controller := daemon.netController
|
||||
|
||||
|
@ -539,24 +562,14 @@ func (daemon *Daemon) allocateNetwork(container *container.Container) error {
|
|||
|
||||
updateSettings := false
|
||||
if len(container.NetworkSettings.Networks) == 0 {
|
||||
mode := container.HostConfig.NetworkMode
|
||||
if container.Config.NetworkDisabled || mode.IsContainer() {
|
||||
if container.Config.NetworkDisabled || container.HostConfig.NetworkMode.IsContainer() {
|
||||
return nil
|
||||
}
|
||||
|
||||
networkName := mode.NetworkName()
|
||||
if mode.IsDefault() {
|
||||
networkName = controller.Config().Daemon.DefaultNetwork
|
||||
err := daemon.updateContainerNetworkSettings(container)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if mode.IsUserDefined() {
|
||||
n, err := daemon.FindNetwork(networkName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
networkName = n.Name()
|
||||
}
|
||||
container.NetworkSettings.Networks = make(map[string]*networktypes.EndpointSettings)
|
||||
container.NetworkSettings.Networks[networkName] = new(networktypes.EndpointSettings)
|
||||
updateSettings = true
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,11 @@ func (daemon *Daemon) setupLinkedContainers(container *container.Container) ([]s
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
// updateContainerNetworkSettings update the network settings
|
||||
func (daemon *Daemon) updateContainerNetworkSettings(container *container.Container) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (daemon *Daemon) initializeNetworking(container *container.Container) error {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -106,6 +106,10 @@ func (daemon *Daemon) create(params *ContainerCreateConfig) (retC *container.Con
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if err := daemon.updateContainerNetworkSettings(container); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := container.ToDiskLocking(); err != nil {
|
||||
logrus.Errorf("Error saving new container to disk: %v", err)
|
||||
return nil, err
|
||||
|
|
|
@ -891,3 +891,22 @@ func (s *DockerNetworkSuite) TestDockerNetworkConnectWithMac(c *check.C) {
|
|||
c.Assert(err, checker.IsNil)
|
||||
c.Assert(strings.TrimSpace(mac2), checker.Not(checker.Equals), strings.TrimSpace(mac1))
|
||||
}
|
||||
|
||||
func (s *DockerNetworkSuite) TestDockerNetworkInspectCreatedContainer(c *check.C) {
|
||||
dockerCmd(c, "create", "--name", "test", "busybox")
|
||||
networks, err := inspectField("test", "NetworkSettings.Networks")
|
||||
c.Assert(err, checker.IsNil)
|
||||
c.Assert(networks, checker.Contains, "bridge", check.Commentf("Should return 'bridge' network"))
|
||||
}
|
||||
|
||||
func (s *DockerNetworkSuite) TestDockerNetworkRestartWithMulipleNetworks(c *check.C) {
|
||||
dockerCmd(c, "network", "create", "test")
|
||||
dockerCmd(c, "run", "--name=foo", "-d", "busybox", "top")
|
||||
c.Assert(waitRun("foo"), checker.IsNil)
|
||||
dockerCmd(c, "network", "connect", "test", "foo")
|
||||
dockerCmd(c, "restart", "foo")
|
||||
networks, err := inspectField("foo", "NetworkSettings.Networks")
|
||||
c.Assert(err, checker.IsNil)
|
||||
c.Assert(networks, checker.Contains, "bridge", check.Commentf("Should contain 'bridge' network"))
|
||||
c.Assert(networks, checker.Contains, "test", check.Commentf("Should contain 'test' netwokr"))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue