From 93b8978a960ba2c0cf8f5b7dd47906adf7ec665d 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 | 22 +++++++++++++++++++++- test/e2e/network_test.go | 16 ++++++++++++++++ 4 files changed, 44 insertions(+), 2 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..4b15093787 100644 --- a/pkg/specgen/namespaces_test.go +++ b/pkg/specgen/namespaces_test.go @@ -161,7 +161,14 @@ func TestParseNetworkFlag(t *testing.T) { name: "bridge mode with invalid 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 invalid ip", @@ -175,6 +182,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..259e54741c 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,21 @@ var _ = Describe("Podman network", func() { Expect(rmAll).Should(ExitCleanly()) }) + It("podman run container host interface name", func() { + ctrName := "testCtr" + vethName := "my_veth" + container := podmanTest.Podman([]string{"run", "-dt", "--network", "bridge:host_interface_name=" + vethName, "--name", ctrName, ALPINE, "top"}) + container.WaitWithDefaultTimeout() + Expect(container).Should(ExitCleanly()) + + if !isRootless() { + // make sure cni/netavark created bridge with expected name + veth, err := net.InterfaceByName(vethName) + Expect(err).ToNot(HaveOccurred()) + Expect(veth.Name).To(Equal(vethName)) + } + }) + It("podman inspect container two CNI networks (container not running)", func() { netName1 := "net1-" + stringid.GenerateRandomID() network1 := podmanTest.Podman([]string{"network", "create", netName1})