Merge pull request #8457 from afbjorklund/bridge-mtu
Add podman network create flag for bridge mtu
This commit is contained in:
commit
c585012db3
|
|
@ -30,6 +30,7 @@ var (
|
||||||
var (
|
var (
|
||||||
networkCreateOptions entities.NetworkCreateOptions
|
networkCreateOptions entities.NetworkCreateOptions
|
||||||
labels []string
|
labels []string
|
||||||
|
opts []string
|
||||||
)
|
)
|
||||||
|
|
||||||
func networkCreateFlags(cmd *cobra.Command) {
|
func networkCreateFlags(cmd *cobra.Command) {
|
||||||
|
|
@ -39,6 +40,10 @@ func networkCreateFlags(cmd *cobra.Command) {
|
||||||
flags.StringVarP(&networkCreateOptions.Driver, driverFlagName, "d", "bridge", "driver to manage the network")
|
flags.StringVarP(&networkCreateOptions.Driver, driverFlagName, "d", "bridge", "driver to manage the network")
|
||||||
_ = cmd.RegisterFlagCompletionFunc(driverFlagName, common.AutocompleteNetworkDriver)
|
_ = cmd.RegisterFlagCompletionFunc(driverFlagName, common.AutocompleteNetworkDriver)
|
||||||
|
|
||||||
|
optFlagName := "opt"
|
||||||
|
flags.StringArrayVarP(&opts, optFlagName, "o", []string{}, "Set driver specific options (default [])")
|
||||||
|
_ = cmd.RegisterFlagCompletionFunc(optFlagName, completion.AutocompleteNone)
|
||||||
|
|
||||||
gatewayFlagName := "gateway"
|
gatewayFlagName := "gateway"
|
||||||
flags.IPVar(&networkCreateOptions.Gateway, gatewayFlagName, nil, "IPv4 or IPv6 gateway for the subnet")
|
flags.IPVar(&networkCreateOptions.Gateway, gatewayFlagName, nil, "IPv4 or IPv6 gateway for the subnet")
|
||||||
_ = cmd.RegisterFlagCompletionFunc(gatewayFlagName, completion.AutocompleteNone)
|
_ = cmd.RegisterFlagCompletionFunc(gatewayFlagName, completion.AutocompleteNone)
|
||||||
|
|
@ -93,6 +98,10 @@ func networkCreate(cmd *cobra.Command, args []string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "failed to parse labels")
|
return errors.Wrap(err, "failed to parse labels")
|
||||||
}
|
}
|
||||||
|
networkCreateOptions.Options, err = parse.GetAllLabels([]string{}, opts)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "unable to process options")
|
||||||
|
}
|
||||||
response, err := registry.ContainerEngine().NetworkCreate(registry.Context(), name, networkCreateOptions)
|
response, err := registry.ContainerEngine().NetworkCreate(registry.Context(), name, networkCreateOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,14 @@ resolution.
|
||||||
|
|
||||||
Driver to manage the network (default "bridge"). Currently only `bridge` is supported.
|
Driver to manage the network (default "bridge"). Currently only `bridge` is supported.
|
||||||
|
|
||||||
|
#### **--opt**=*option*, **-o**
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
#### **--gateway**
|
#### **--gateway**
|
||||||
|
|
||||||
Define a gateway for the subnet. If you want to provide a gateway address, you must also provide a
|
Define a gateway for the subnet. If you want to provide a gateway address, you must also provide a
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/containernetworking/cni/pkg/version"
|
"github.com/containernetworking/cni/pkg/version"
|
||||||
"github.com/containers/common/pkg/config"
|
"github.com/containers/common/pkg/config"
|
||||||
|
|
@ -76,6 +77,29 @@ func validateBridgeOptions(options entities.NetworkCreateOptions) error {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseMTU parses the mtu option
|
||||||
|
func parseMTU(mtu string) (int, error) {
|
||||||
|
if mtu == "" {
|
||||||
|
return 0, nil // default
|
||||||
|
}
|
||||||
|
m, err := strconv.Atoi(mtu)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if m < 0 {
|
||||||
|
return 0, errors.Errorf("the value %d for mtu is less than zero", m)
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseVlan parses the vlan option
|
||||||
|
func parseVlan(vlan string) (int, error) {
|
||||||
|
if vlan == "" {
|
||||||
|
return 0, nil // default
|
||||||
|
}
|
||||||
|
return strconv.Atoi(vlan)
|
||||||
|
}
|
||||||
|
|
||||||
// createBridge creates a CNI network
|
// createBridge creates a CNI network
|
||||||
func createBridge(name string, options entities.NetworkCreateOptions, runtimeConfig *config.Config) (string, error) {
|
func createBridge(name string, options entities.NetworkCreateOptions, runtimeConfig *config.Config) (string, error) {
|
||||||
var (
|
var (
|
||||||
|
|
@ -149,6 +173,28 @@ func createBridge(name string, options entities.NetworkCreateOptions, runtimeCon
|
||||||
ipMasq = false
|
ipMasq = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var mtu int
|
||||||
|
var vlan int
|
||||||
|
for k, v := range options.Options {
|
||||||
|
var err error
|
||||||
|
switch k {
|
||||||
|
case "mtu":
|
||||||
|
mtu, err = parseMTU(v)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
case "vlan":
|
||||||
|
vlan, err = parseVlan(v)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return "", errors.Errorf("unsupported option %s", k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// obtain host bridge name
|
// obtain host bridge name
|
||||||
bridgeDeviceName, err := GetFreeDeviceName(runtimeConfig)
|
bridgeDeviceName, err := GetFreeDeviceName(runtimeConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -172,7 +218,7 @@ func createBridge(name string, options entities.NetworkCreateOptions, runtimeCon
|
||||||
ncList := NewNcList(name, version.Current(), options.Labels)
|
ncList := NewNcList(name, version.Current(), options.Labels)
|
||||||
var plugins []CNIPlugins
|
var plugins []CNIPlugins
|
||||||
// TODO need to iron out the role of isDefaultGW and IPMasq
|
// TODO need to iron out the role of isDefaultGW and IPMasq
|
||||||
bridge := NewHostLocalBridge(bridgeDeviceName, isGateway, false, ipMasq, ipamConfig)
|
bridge := NewHostLocalBridge(bridgeDeviceName, isGateway, false, ipMasq, mtu, vlan, ipamConfig)
|
||||||
plugins = append(plugins, bridge)
|
plugins = append(plugins, bridge)
|
||||||
plugins = append(plugins, NewPortMapPlugin())
|
plugins = append(plugins, NewPortMapPlugin())
|
||||||
plugins = append(plugins, NewFirewallPlugin())
|
plugins = append(plugins, NewFirewallPlugin())
|
||||||
|
|
|
||||||
|
|
@ -41,12 +41,14 @@ func NewNcList(name, version string, labels NcLabels) NcList {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHostLocalBridge creates a new LocalBridge for host-local
|
// NewHostLocalBridge creates a new LocalBridge for host-local
|
||||||
func NewHostLocalBridge(name string, isGateWay, isDefaultGW, ipMasq bool, ipamConf IPAMHostLocalConf) *HostLocalBridge {
|
func NewHostLocalBridge(name string, isGateWay, isDefaultGW, ipMasq bool, mtu int, vlan int, ipamConf IPAMHostLocalConf) *HostLocalBridge {
|
||||||
hostLocalBridge := HostLocalBridge{
|
hostLocalBridge := HostLocalBridge{
|
||||||
PluginType: "bridge",
|
PluginType: "bridge",
|
||||||
BrName: name,
|
BrName: name,
|
||||||
IPMasq: ipMasq,
|
IPMasq: ipMasq,
|
||||||
|
MTU: mtu,
|
||||||
HairpinMode: true,
|
HairpinMode: true,
|
||||||
|
Vlan: vlan,
|
||||||
IPAM: ipamConf,
|
IPAM: ipamConf,
|
||||||
}
|
}
|
||||||
if isGateWay {
|
if isGateWay {
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,8 @@ type NetworkCreateOptions struct {
|
||||||
Range net.IPNet
|
Range net.IPNet
|
||||||
Subnet net.IPNet
|
Subnet net.IPNet
|
||||||
IPv6 bool
|
IPv6 bool
|
||||||
|
// Mapping of driver options and values.
|
||||||
|
Options map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NetworkCreateReport describes a created network for the cli
|
// NetworkCreateReport describes a created network for the cli
|
||||||
|
|
|
||||||
|
|
@ -329,4 +329,37 @@ var _ = Describe("Podman network create", func() {
|
||||||
Expect(nc).To(ExitWithError())
|
Expect(nc).To(ExitWithError())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("podman network create with mtu option", func() {
|
||||||
|
net := "mtu-test"
|
||||||
|
nc := podmanTest.Podman([]string{"network", "create", "--opt", "mtu=9000", net})
|
||||||
|
nc.WaitWithDefaultTimeout()
|
||||||
|
Expect(nc.ExitCode()).To(BeZero())
|
||||||
|
defer podmanTest.removeCNINetwork(net)
|
||||||
|
|
||||||
|
nc = podmanTest.Podman([]string{"network", "inspect", net})
|
||||||
|
nc.WaitWithDefaultTimeout()
|
||||||
|
Expect(nc.ExitCode()).To(BeZero())
|
||||||
|
Expect(nc.OutputToString()).To(ContainSubstring(`"mtu": 9000,`))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("podman network create with vlan option", func() {
|
||||||
|
net := "vlan-test"
|
||||||
|
nc := podmanTest.Podman([]string{"network", "create", "--opt", "vlan=9", net})
|
||||||
|
nc.WaitWithDefaultTimeout()
|
||||||
|
Expect(nc.ExitCode()).To(BeZero())
|
||||||
|
defer podmanTest.removeCNINetwork(net)
|
||||||
|
|
||||||
|
nc = podmanTest.Podman([]string{"network", "inspect", net})
|
||||||
|
nc.WaitWithDefaultTimeout()
|
||||||
|
Expect(nc.ExitCode()).To(BeZero())
|
||||||
|
Expect(nc.OutputToString()).To(ContainSubstring(`"vlan": 9`))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("podman network create with invalid option", func() {
|
||||||
|
net := "invalid-test"
|
||||||
|
nc := podmanTest.Podman([]string{"network", "create", "--opt", "foo=bar", net})
|
||||||
|
nc.WaitWithDefaultTimeout()
|
||||||
|
Expect(nc).To(ExitWithError())
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue