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,16 +529,8 @@ func (daemon *Daemon) updateNetwork(container *container.Container) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (daemon *Daemon) allocateNetwork(container *container.Container) error {
|
||||
controller := daemon.netController
|
||||
|
||||
// Cleanup any stale sandbox left over due to ungraceful daemon shutdown
|
||||
if err := controller.SandboxDestroy(container.ID); err != nil {
|
||||
logrus.Errorf("failed to cleanup up stale network sandbox for container %s", container.ID)
|
||||
}
|
||||
|
||||
updateSettings := false
|
||||
if len(container.NetworkSettings.Networks) == 0 {
|
||||
// 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
|
||||
|
@ -546,7 +538,7 @@ func (daemon *Daemon) allocateNetwork(container *container.Container) error {
|
|||
|
||||
networkName := mode.NetworkName()
|
||||
if mode.IsDefault() {
|
||||
networkName = controller.Config().Daemon.DefaultNetwork
|
||||
networkName = daemon.netController.Config().Daemon.DefaultNetwork
|
||||
}
|
||||
if mode.IsUserDefined() {
|
||||
n, err := daemon.FindNetwork(networkName)
|
||||
|
@ -557,6 +549,27 @@ func (daemon *Daemon) allocateNetwork(container *container.Container) error {
|
|||
}
|
||||
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
|
||||
|
||||
// Cleanup any stale sandbox left over due to ungraceful daemon shutdown
|
||||
if err := controller.SandboxDestroy(container.ID); err != nil {
|
||||
logrus.Errorf("failed to cleanup up stale network sandbox for container %s", container.ID)
|
||||
}
|
||||
|
||||
updateSettings := false
|
||||
if len(container.NetworkSettings.Networks) == 0 {
|
||||
if container.Config.NetworkDisabled || container.HostConfig.NetworkMode.IsContainer() {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := daemon.updateContainerNetworkSettings(container)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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