From df65832d5eadcdcc572f5d07e3f51a483fbf6fba Mon Sep 17 00:00:00 2001 From: Matthew Endsley Date: Thu, 21 Nov 2024 21:13:39 -0800 Subject: [PATCH 1/2] Enable `--mac-address` for Windows Signed-off-by: Matthew Endsley --- .../container_run_network_windows_test.go | 75 +++++++++++++++++++ .../container_network_manager_windows.go | 5 +- 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/cmd/nerdctl/container_run_network_windows_test.go b/cmd/nerdctl/container_run_network_windows_test.go index 914627607ec..796300ccfa1 100644 --- a/cmd/nerdctl/container_run_network_windows_test.go +++ b/cmd/nerdctl/container_run_network_windows_test.go @@ -26,6 +26,7 @@ import ( "github.com/containerd/nerdctl/pkg/defaults" "github.com/containerd/nerdctl/pkg/netutil" "github.com/containerd/nerdctl/pkg/testutil" + "github.com/containerd/nerdctl/pkg/testutil/nettestutil" "gotest.tools/v3/assert" ) @@ -185,3 +186,77 @@ func TestHnsEndpointsRemovedAfterAttachedRun(t *testing.T) { assert.NilError(t, err) assert.Equal(t, originalEndpointsCount, len(existingEndpoints), "the number of HNS endpoints should equal pre-test amount") } + +func TestRunContainerWithMACAddress(t *testing.T) { + base := testutil.NewBase(t) + tID := testutil.Identifier(t) + networkNat := "testNetworkNat" + tID + //networkBridge := "testNetworkBridge" + tID + //networkMACvlan := "testNetworkMACvlan" + tID + //networkIPvlan := "testNetworkIPvlan" + tID + tearDown := func() { + base.Cmd("network", "rm", networkNat).Run() + //base.Cmd("network", "rm", networkBridge).Run() + //base.Cmd("network", "rm", networkMACvlan).Run() + //base.Cmd("network", "rm", networkIPvlan).Run() + } + + tearDown() + t.Cleanup(tearDown) + + base.Cmd("network", "create", networkNat, "--driver", "nat").AssertOK() + //base.Cmd("network", "create", networkBridge, "--driver", "bridge").AssertOK() + //base.Cmd("network", "create", networkMACvlan, "--driver", "macvlan").AssertOK() + //base.Cmd("network", "create", networkIPvlan, "--driver", "ipvlan").AssertOK() + + //defaultMac := base.Cmd("run", "--rm", "-i", "--network", networkNat, testutil.CommonImage). + // CmdOption(testutil.WithStdin(strings.NewReader("powershell -Command \"Get-NetAdapter | Select-Object -ExpandProperty MacAddress\""))). + // Run().Stdout() + + passedMac := "we expect the generated mac on the output" + + tests := []struct { + Network string + WantErr bool + Expect string + }{ + {"host", true, "cannot use"}, // anything but the actual address being passed + {"none", false, ""}, // nothing + {"container:whatever" + tID, true, "only supported on Linux"}, // "No such container" vs. "could not find container" + //{"bridge", false, passedMac}, + {networkNat, false, passedMac}, + //{networkBridge, false, passedMac}, + //{networkMACvlan, false, passedMac}, + //{networkIPvlan, true, "not support"}, + } + + for i, test := range tests { + containerName := fmt.Sprintf("%s_%d", tID, i) + testName := fmt.Sprintf("%s_container:%s_network:%s_expect:%s", tID, containerName, test.Network, test.Expect) + expect := test.Expect + network := test.Network + wantErr := test.WantErr + t.Run(testName, func(tt *testing.T) { + tt.Parallel() + + macAddress, err := nettestutil.GenerateMACAddress() + if err != nil { + t.Errorf("failed to generate MAC address: %s", err) + } + if expect == passedMac { + expect = strings.ToUpper(strings.Replace(macAddress, ":", "-", -1)) + } + + res := base.Cmd("run", "--rm", "-i", "--network", network, "--mac-address", macAddress, testutil.CommonImage, "ipconfig /all").Run() + + if wantErr { + assert.Assert(t, res.ExitCode != 0, "Command should have failed", res) + assert.Assert(t, strings.Contains(res.Combined(), expect), fmt.Sprintf("expected output to contain %q: %q", expect, res.Combined())) + } else { + assert.Assert(t, res.ExitCode == 0, "Command should have succeeded", res) + assert.Assert(t, strings.Contains(res.Stdout(), expect), fmt.Sprintf("expected output to contain %q: %q", expect, res.Stdout())) + } + }) + + } +} diff --git a/pkg/containerutil/container_network_manager_windows.go b/pkg/containerutil/container_network_manager_windows.go index 774c737ac82..58db9638042 100644 --- a/pkg/containerutil/container_network_manager_windows.go +++ b/pkg/containerutil/container_network_manager_windows.go @@ -50,9 +50,8 @@ func (m *cniNetworkManager) VerifyNetworkOptions(_ context.Context) error { nonZeroArgs := nonZeroMapValues(map[string]interface{}{ "--hostname": m.netOpts.Hostname, "--uts": m.netOpts.UTSNamespace, - // NOTE: IP and MAC settings are currently ignored on Windows. - "--ip-address": m.netOpts.IPAddress, - "--mac-address": m.netOpts.MACAddress, + // NOTE: IP setting is currently ignored on Windows. + "--ip-address": m.netOpts.IPAddress, // NOTE: zero-length slices count as a non-zero-value so we explicitly check length: "--dns-opt/--dns-option": len(m.netOpts.DNSResolvConfOptions) != 0, "--dns-servers": len(m.netOpts.DNSServers) != 0, From a74644c87ff4eac3f42748aa67105a4076304358 Mon Sep 17 00:00:00 2001 From: Matthew Endsley Date: Thu, 21 Nov 2024 23:18:12 -0800 Subject: [PATCH 2/2] Update containerd to use mac-address compatible CNI Signed-off-by: Matthew Endsley --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 675ed36ab89..69c58548880 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -320,8 +320,8 @@ jobs: - run: go install ./cmd/nerdctl - uses: actions/checkout@v4.1.1 with: - repository: containerd/containerd - ref: v1.7.22 + repository: PlayEveryWare/containerd + ref: mendsley/windows_cni_mac path: containerd fetch-depth: 1 - name: "Set up CNI"