CNI: network create support macvlan modes

Support setting the macvlan mode with `podman network create -d macvlan
--opt mode=bridge`. This will correctly set the specified macvlan mode
in the cni conflist file.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger 2021-09-16 14:03:49 +02:00
parent 9119a578e7
commit c20f61148c
No known key found for this signature in database
GPG Key ID: EB145DD938A3CAF2
4 changed files with 60 additions and 7 deletions

View File

@ -32,9 +32,14 @@ As rootless the `macvlan` driver has no access to the host network interfaces be
Set driver specific options.
For the `bridge` driver the following options are supported: `mtu` and `vlan`.
The `mtu` option sets the Maximum Transmission Unit (MTU) and takes an integer value.
The `vlan` option assign VLAN tag and enables vlan\_filtering. Defaults to none.
All drivers accept the `mtu` option. The `mtu` option sets the Maximum Transmission Unit (MTU) and takes an integer value.
Additionally the `bridge` driver supports the following option:
- `vlan`: This option assign VLAN tag and enables vlan\_filtering. Defaults to none.
The `macvlan` driver supports the following options:
- `parent`: The host device which should be used for the macvlan interface. Defaults to the default route interface.
- `mode`: This options sets the specified macvlan mode on the interface. Supported values are `bridge`, `private`, `vepa`, `passthru`. Defaults to `bridge`.
#### **--gateway**

View File

@ -94,6 +94,10 @@ func createNetworkFromCNIConfigList(conf *libcni.NetworkConfigList, confPath str
network.Options["mtu"] = strconv.Itoa(macvlan.MTU)
}
if macvlan.Mode != "" {
network.Options["mode"] = macvlan.Mode
}
err = convertIPAMConfToNetwork(&network, macvlan.IPAM, confPath)
if err != nil {
return nil, err
@ -237,6 +241,7 @@ func (n *cniNetwork) createCNIConfigListFromNetwork(network *types.Network, writ
vlan := 0
mtu := 0
macvlanMode := ""
for k, v := range network.Options {
switch k {
case "mtu":
@ -251,6 +256,12 @@ func (n *cniNetwork) createCNIConfigListFromNetwork(network *types.Network, writ
return nil, "", err
}
case "mode":
if !pkgutil.StringInSlice(v, []string{"", "bridge", "private", "vepa", "passthru"}) {
return nil, "", errors.Errorf("unknown macvlan mode %q", v)
}
macvlanMode = v
default:
return nil, "", errors.Errorf("unsupported network option %s", k)
}
@ -281,7 +292,7 @@ func (n *cniNetwork) createCNIConfigListFromNetwork(network *types.Network, writ
}
case types.MacVLANNetworkDriver:
plugins = append(plugins, newMacVLANPlugin(network.NetworkInterface, mtu, ipamConf))
plugins = append(plugins, newMacVLANPlugin(network.NetworkInterface, macvlanMode, mtu, ipamConf))
default:
return nil, "", errors.Errorf("driver %q is not supported by cni", network.Driver)

View File

@ -50,7 +50,7 @@ type hostLocalBridge struct {
PromiscMode bool `json:"promiscMode,omitempty"`
Vlan int `json:"vlan,omitempty"`
IPAM ipamConfig `json:"ipam"`
Capabilities map[string]bool `json:"capabilities"`
Capabilities map[string]bool `json:"capabilities,omitempty"`
}
// ipamConfig describes an IPAM configuration
@ -88,7 +88,8 @@ type macVLANConfig struct {
Master string `json:"master"`
IPAM ipamConfig `json:"ipam"`
MTU int `json:"mtu,omitempty"`
Capabilities map[string]bool `json:"capabilities"`
Mode string `json:"mode,omitempty"`
Capabilities map[string]bool `json:"capabilities,omitempty"`
}
// firewallConfig describes the firewall plugin
@ -260,7 +261,7 @@ func hasDNSNamePlugin(paths []string) bool {
}
// newMacVLANPlugin creates a macvlanconfig with a given device name
func newMacVLANPlugin(device string, mtu int, ipam ipamConfig) macVLANConfig {
func newMacVLANPlugin(device, mode string, mtu int, ipam ipamConfig) macVLANConfig {
m := macVLANConfig{
PluginType: "macvlan",
IPAM: ipam,
@ -268,6 +269,9 @@ func newMacVLANPlugin(device string, mtu int, ipam ipamConfig) macVLANConfig {
if mtu > 0 {
m.MTU = mtu
}
if len(mode) > 0 {
m.Mode = mode
}
// CNI is supposed to use the default route if a
// parent device is not provided
if len(device) > 0 {

View File

@ -250,6 +250,39 @@ var _ = Describe("Config", func() {
grepInFile(path, `"type": "host-local"`)
})
It("create macvlan config with mode", func() {
for _, mode := range []string{"bridge", "private", "vepa", "passthru"} {
network := types.Network{
Driver: "macvlan",
Options: map[string]string{
"mode": mode,
},
}
network1, err := libpodNet.NetworkCreate(network)
Expect(err).To(BeNil())
Expect(network1.Name).ToNot(BeEmpty())
path := filepath.Join(cniConfDir, network1.Name+".conflist")
Expect(path).To(BeARegularFile())
Expect(network1.Driver).To(Equal("macvlan"))
Expect(network1.Options).To(HaveKeyWithValue("mode", mode))
Expect(network1.IPAMOptions).ToNot(BeEmpty())
Expect(network1.IPAMOptions).To(HaveKeyWithValue("driver", "dhcp"))
grepInFile(path, `"mode": "`+mode+`"`)
}
})
It("create macvlan config with invalid mode", func() {
network := types.Network{
Driver: "macvlan",
Options: map[string]string{
"mode": "test",
},
}
_, err := libpodNet.NetworkCreate(network)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(`unknown macvlan mode "test"`))
})
It("create macvlan config with invalid device", func() {
network := types.Network{
Driver: "macvlan",