From 315e7412e8026911cf70d32c869bce232ccd9c9f Mon Sep 17 00:00:00 2001 From: Michael Zimmermann Date: Mon, 11 Nov 2024 22:37:02 +0100 Subject: [PATCH] add support for driver-specific options during container creation This way has a huge disadvantage: The user will not see an error when he uses a non-existent option. Another disadvantage is, that if we add more options within podman, they might collide with the names chosen by plugins. Such issues might be hard to debug. The advantage is that the usage is very nice: --network bridge:opt1=val1,opt2=val2. Alternatively, we could put this behind `opt=`, which is harder to use, but would solve all issues above: --network bridge:opt=opt1=val1,opt=opt2=val2 Signed-off-by: Michael Zimmermann --- docs/source/markdown/options/network.md | 3 ++ pkg/specgen/namespaces.go | 5 +++- pkg/specgen/namespaces_test.go | 39 +++++++++++++++++++++++-- test/e2e/network_test.go | 22 ++++++++++++++ 4 files changed, 66 insertions(+), 3 deletions(-) diff --git a/docs/source/markdown/options/network.md b/docs/source/markdown/options/network.md index 2e67cc0df6..9738426f7e 100644 --- a/docs/source/markdown/options/network.md +++ b/docs/source/markdown/options/network.md @@ -14,6 +14,9 @@ Valid _mode_ values are: - **ip6=**_IPv6_: Specify a static IPv6 address for this container. - **mac=**_MAC_: Specify a static MAC address for this container. - **interface_name=**_name_: Specify a name for the created network interface inside the container. + - **host_interface_name=**_name_: Specify a name for the created network interface outside the container. + + Any other options will be passed through to netavark without validation. This can be useful to pass arguments to netavark plugins. For example, to set a static ipv4 address and a static mac address, use `--network bridge:ip=10.88.0.10,mac=44:33:22:11:00:99`. diff --git a/pkg/specgen/namespaces.go b/pkg/specgen/namespaces.go index 13376b3ba6..689d68110c 100644 --- a/pkg/specgen/namespaces.go +++ b/pkg/specgen/namespaces.go @@ -482,7 +482,10 @@ func parseBridgeNetworkOptions(opts string) (types.PerNetworkOptions, error) { netOpts.InterfaceName = value default: - return netOpts, fmt.Errorf("unknown bridge network option: %s", name) + if netOpts.Options == nil { + netOpts.Options = make(map[string]string) + } + netOpts.Options[name] = value } } return netOpts, nil diff --git a/pkg/specgen/namespaces_test.go b/pkg/specgen/namespaces_test.go index 03e51ab248..18784368d0 100644 --- a/pkg/specgen/namespaces_test.go +++ b/pkg/specgen/namespaces_test.go @@ -158,10 +158,32 @@ func TestParseNetworkFlag(t *testing.T) { }, }, { - name: "bridge mode with invalid option", + name: "bridge mode with unknown option", args: []string{"bridge:abc=123"}, nsmode: Namespace{NSMode: Bridge}, - err: "unknown bridge network option: abc", + networks: map[string]types.PerNetworkOptions{ + defaultNetName: { + InterfaceName: "", + Options: map[string]string{ + "abc": "123", + }, + }, + }, + }, + { + name: "bridge mode with multiple unknown options", + args: []string{"bridge:abc=123,xyz=789,other=a-much-longer-value"}, + nsmode: Namespace{NSMode: Bridge}, + networks: map[string]types.PerNetworkOptions{ + defaultNetName: { + InterfaceName: "", + Options: map[string]string{ + "abc": "123", + "xyz": "789", + "other": "a-much-longer-value", + }, + }, + }, }, { name: "bridge mode with invalid ip", @@ -175,6 +197,19 @@ func TestParseNetworkFlag(t *testing.T) { nsmode: Namespace{NSMode: Bridge}, err: "address 123: invalid MAC address", }, + { + name: "bridge mode with host interface name", + args: []string{"bridge:host_interface_name=my-veth"}, + nsmode: Namespace{NSMode: Bridge}, + networks: map[string]types.PerNetworkOptions{ + defaultNetName: { + InterfaceName: "", + Options: map[string]string{ + "host_interface_name": "my-veth", + }, + }, + }, + }, { name: "network name", args: []string{"someName"}, diff --git a/test/e2e/network_test.go b/test/e2e/network_test.go index 08813a85ce..69199c01b1 100644 --- a/test/e2e/network_test.go +++ b/test/e2e/network_test.go @@ -5,6 +5,7 @@ package integration import ( "encoding/json" "fmt" + "net" "path/filepath" "time" @@ -297,6 +298,27 @@ var _ = Describe("Podman network", func() { Expect(rmAll).Should(ExitCleanly()) }) + It("podman run container host interface name", func() { + Skip("FIXME: We need netavark >= v1.14 for host interface support") + + ctrName := "testCtr" + vethName := "my_veth" + stringid.GenerateRandomID()[:8] + container := podmanTest.Podman([]string{"run", "-dt", "--network", "bridge:host_interface_name=" + vethName, "--name", ctrName, ALPINE, "top"}) + container.WaitWithDefaultTimeout() + Expect(container).Should(ExitCleanly()) + + if !isRootless() { + veth, err := net.InterfaceByName(vethName) + Expect(err).ToNot(HaveOccurred()) + Expect(veth.Name).To(Equal(vethName)) + } else { + session := podmanTest.Podman([]string{"unshare", "--rootless-netns", "ip", "link", "show", vethName}) + session.WaitWithDefaultTimeout() + Expect(session).Should(ExitCleanly()) + Expect(session.OutputToString()).To(ContainSubstring(vethName)) + } + }) + It("podman inspect container two CNI networks (container not running)", func() { netName1 := "net1-" + stringid.GenerateRandomID() network1 := podmanTest.Podman([]string{"network", "create", netName1})