From 51d71d86455f15384d9f739f203b5049744897b7 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 10 Dec 2024 14:22:30 +0100 Subject: [PATCH] fix support for service.mac_address Signed-off-by: Nicolas De Loof --- pkg/compose/create.go | 23 +++++++---- pkg/compose/create_test.go | 41 ++++++++----------- .../fixtures/network-test/mac_address.yaml | 4 ++ pkg/e2e/networks_test.go | 12 ++++++ 4 files changed, 47 insertions(+), 33 deletions(-) create mode 100644 pkg/e2e/fixtures/network-test/mac_address.yaml diff --git a/pkg/compose/create.go b/pkg/compose/create.go index 0b8d92d071f..035492db6f4 100644 --- a/pkg/compose/create.go +++ b/pkg/compose/create.go @@ -545,23 +545,28 @@ func defaultNetworkSettings( primaryNetworkKey = "default" } primaryNetworkMobyNetworkName := project.Networks[primaryNetworkKey].Name - endpointsConfig := map[string]*network.EndpointSettings{ - primaryNetworkMobyNetworkName: createEndpointSettings(project, service, serviceIndex, primaryNetworkKey, links, useNetworkAliases), - } + primaryNetworkEndpoint := createEndpointSettings(project, service, serviceIndex, primaryNetworkKey, links, useNetworkAliases) + endpointsConfig := map[string]*network.EndpointSettings{} // Starting from API version 1.44, the Engine will take several EndpointsConfigs // so we can pass all the extra networks we want the container to be connected to // in the network configuration instead of connecting the container to each extra // network individually after creation. - if versions.GreaterThanOrEqualTo(version, "1.44") && len(service.Networks) > 1 { - serviceNetworks := service.NetworksByPriority() - for _, networkKey := range serviceNetworks[1:] { - mobyNetworkName := project.Networks[networkKey].Name - epSettings := createEndpointSettings(project, service, serviceIndex, networkKey, links, useNetworkAliases) - endpointsConfig[mobyNetworkName] = epSettings + if versions.GreaterThanOrEqualTo(version, "1.44") { + if len(service.Networks) > 1 { + serviceNetworks := service.NetworksByPriority() + for _, networkKey := range serviceNetworks[1:] { + mobyNetworkName := project.Networks[networkKey].Name + epSettings := createEndpointSettings(project, service, serviceIndex, networkKey, links, useNetworkAliases) + endpointsConfig[mobyNetworkName] = epSettings + } + } + if primaryNetworkEndpoint.MacAddress == "" { + primaryNetworkEndpoint.MacAddress = service.MacAddress } } + endpointsConfig[primaryNetworkMobyNetworkName] = primaryNetworkEndpoint networkConfig := &network.NetworkingConfig{ EndpointsConfig: endpointsConfig, } diff --git a/pkg/compose/create_test.go b/pkg/compose/create_test.go index 77e7b537810..eb44d0f93a4 100644 --- a/pkg/compose/create_test.go +++ b/pkg/compose/create_test.go @@ -278,33 +278,26 @@ func TestDefaultNetworkSettings(t *testing.T) { } func TestCreateEndpointSettings(t *testing.T) { - eps := createEndpointSettings( - &composetypes.Project{ - Name: "projName", - }, - composetypes.ServiceConfig{ - Name: "serviceName", - ContainerName: "containerName", - Networks: map[string]*composetypes.ServiceNetworkConfig{ - "netName": { - Priority: 100, - Aliases: []string{"alias1", "alias2"}, - Ipv4Address: "10.16.17.18", - Ipv6Address: "fdb4:7a7f:373a:3f0c::42", - LinkLocalIPs: []string{"169.254.10.20"}, - MacAddress: "10:00:00:00:01", - DriverOpts: composetypes.Options{ - "driverOpt1": "optval1", - "driverOpt2": "optval2", - }, + eps := createEndpointSettings(&composetypes.Project{ + Name: "projName", + }, composetypes.ServiceConfig{ + Name: "serviceName", + ContainerName: "containerName", + Networks: map[string]*composetypes.ServiceNetworkConfig{ + "netName": { + Priority: 100, + Aliases: []string{"alias1", "alias2"}, + Ipv4Address: "10.16.17.18", + Ipv6Address: "fdb4:7a7f:373a:3f0c::42", + LinkLocalIPs: []string{"169.254.10.20"}, + MacAddress: "10:00:00:00:01", + DriverOpts: composetypes.Options{ + "driverOpt1": "optval1", + "driverOpt2": "optval2", }, }, }, - 0, // serviceIndex - "netName", // networkKey - []string{"link1", "link2"}, // links - true, // useNetworkAliases - ) + }, 0, "netName", []string{"link1", "link2"}, true) assert.Check(t, cmp.DeepEqual(eps, &network.EndpointSettings{ IPAMConfig: &network.EndpointIPAMConfig{ IPv4Address: "10.16.17.18", diff --git a/pkg/e2e/fixtures/network-test/mac_address.yaml b/pkg/e2e/fixtures/network-test/mac_address.yaml new file mode 100644 index 00000000000..60e3861a2d8 --- /dev/null +++ b/pkg/e2e/fixtures/network-test/mac_address.yaml @@ -0,0 +1,4 @@ +services: + test: + image: nginx:alpine + mac_address: 00:e0:84:35:d0:e8 diff --git a/pkg/e2e/networks_test.go b/pkg/e2e/networks_test.go index f1d90883207..87495b783cf 100644 --- a/pkg/e2e/networks_test.go +++ b/pkg/e2e/networks_test.go @@ -172,3 +172,15 @@ func TestNetworkConfigChanged(t *testing.T) { res = c.RunDockerComposeCmd(t, "--project-name", projectName, "exec", "test", "hostname", "-i") res.Assert(t, icmd.Expected{Out: "192.168.0."}) } + +func TestMacAddress(t *testing.T) { + c := NewCLI(t) + const projectName = "network_mac_address" + c.RunDockerComposeCmd(t, "-f", "./fixtures/network-test/mac_address.yaml", "--project-name", projectName, "up", "-d") + t.Cleanup(func() { + c.cleanupWithDown(t, projectName) + }) + res := c.RunDockerCmd(t, "inspect", fmt.Sprintf("%s-test-1", projectName), "-f", "{{ (index .NetworkSettings.Networks \"network_mac_address_default\" ).MacAddress }}") + res.Assert(t, icmd.Expected{Out: "00:e0:84:35:d0:e8"}) + +}