Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix network connect dulicate mac-address #17860

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions daemon/container_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ func (daemon *Daemon) updateNetwork(container *Container) error {
return nil
}

func (container *Container) buildCreateEndpointOptions(n libnetwork.Network) ([]libnetwork.EndpointOption, error) {
func (container *Container) buildCreateEndpointOptions(n libnetwork.Network, controller libnetwork.NetworkController) ([]libnetwork.EndpointOption, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid passing the libnetwork.controller and simply check for n.Name() == "bridge"
The bridge string has been anyway hard-coded all over this file.
Another PR should take care of replacing those with a daemon constant.

var (
portSpecs = make(nat.PortSet)
bindings = make(nat.PortMap)
Expand Down Expand Up @@ -867,12 +867,14 @@ func (container *Container) buildCreateEndpointOptions(n libnetwork.Network) ([]
if err != nil {
return nil, err
}
if container.hostConfig.NetworkMode.NetworkName() == n.Name() ||
container.hostConfig.NetworkMode.IsDefault() && n.Name() == controller.Config().Daemon.DefaultNetwork {
genericOption := options.Generic{
netlabel.MacAddress: mac,
}

genericOption := options.Generic{
netlabel.MacAddress: mac,
createOptions = append(createOptions, libnetwork.EndpointOptionGeneric(genericOption))
}

createOptions = append(createOptions, libnetwork.EndpointOptionGeneric(genericOption))
}

if n.Name() == "bridge" || container.NetworkSettings.IsAnonymousEndpoint {
Expand Down Expand Up @@ -934,7 +936,7 @@ func (daemon *Daemon) getNetworkSandbox(container *Container) libnetwork.Sandbox
return sb
}

// ConnectToNetwork connects a container to a netork
// ConnectToNetwork connects a container to a network
func (daemon *Daemon) ConnectToNetwork(container *Container, idOrName string) error {
if !container.Running {
return derr.ErrorCodeNotRunning.WithArgs(container.ID)
Expand Down Expand Up @@ -981,7 +983,7 @@ func (daemon *Daemon) connectToNetwork(container *Container, idOrName string, up
return err
}

createOptions, err := container.buildCreateEndpointOptions(n)
createOptions, err := container.buildCreateEndpointOptions(n, controller)
if err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions integration-cli/docker_cli_network_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,3 +764,18 @@ func (s *DockerNetworkSuite) TestDockerNetworkHostModeUngracefulDaemonRestart(c
c.Assert(strings.TrimSpace(runningOut), checker.Equals, "true")
}
}

func (s *DockerNetworkSuite) TestDockerNetworkConnectWithMac(c *check.C) {
macAddress := "02:42:ac:11:00:02"
dockerCmd(c, "network", "create", "mynetwork")
dockerCmd(c, "run", "--name=test", "-d", "--mac-address", macAddress, "busybox", "top")
c.Assert(waitRun("test"), check.IsNil)
mac1, err := inspectField("test", "NetworkSettings.Networks.bridge.MacAddress")
c.Assert(err, checker.IsNil)
c.Assert(strings.TrimSpace(mac1), checker.Equals, macAddress)
dockerCmd(c, "network", "connect", "mynetwork", "test")
mac2, err := inspectField("test", "NetworkSettings.Networks.mynetwork.MacAddress")
c.Assert(err, checker.IsNil)
c.Assert(strings.TrimSpace(mac2), checker.Not(checker.Equals), strings.TrimSpace(mac1))

}