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 <sigmaepsilon92@gmail.com>
This commit is contained in:
Michael Zimmermann
2024-11-11 22:37:02 +01:00
parent 3e47e0bc8c
commit 315e7412e8
4 changed files with 66 additions and 3 deletions

View File

@ -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`.

View File

@ -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

View File

@ -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"},

View File

@ -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})