Skip to content

Commit

Permalink
add support for driver-specific options during container creation
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
M1cha committed Nov 13, 2024
1 parent 3e47e0b commit 93b8978
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs/source/markdown/options/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
5 changes: 4 additions & 1 deletion pkg/specgen/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion pkg/specgen/namespaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"},
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package integration
import (
"encoding/json"
"fmt"
"net"
"path/filepath"
"time"

Expand Down Expand Up @@ -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})
Expand Down

0 comments on commit 93b8978

Please sign in to comment.