mirror of
https://github.com/containers/podman.git
synced 2025-06-18 15:39:08 +08:00
Honor network options for macvlan networks
when creating a macvlan network, we should honor gateway, subnet, and mtu as provided by the user. Fixes: #9167 Signed-off-by: baude <bbaude@redhat.com>
This commit is contained in:
@ -104,6 +104,8 @@ func (p PortMapConfig) Bytes() ([]byte, error) {
|
|||||||
// IPAMDHCP describes the ipamdhcp config
|
// IPAMDHCP describes the ipamdhcp config
|
||||||
type IPAMDHCP struct {
|
type IPAMDHCP struct {
|
||||||
DHCP string `json:"type"`
|
DHCP string `json:"type"`
|
||||||
|
Routes []IPAMRoute `json:"routes,omitempty"`
|
||||||
|
Ranges [][]IPAMLocalHostRangeConf `json:"ranges,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// MacVLANConfig describes the macvlan config
|
// MacVLANConfig describes the macvlan config
|
||||||
@ -111,6 +113,7 @@ type MacVLANConfig struct {
|
|||||||
PluginType string `json:"type"`
|
PluginType string `json:"type"`
|
||||||
Master string `json:"master"`
|
Master string `json:"master"`
|
||||||
IPAM IPAMDHCP `json:"ipam"`
|
IPAM IPAMDHCP `json:"ipam"`
|
||||||
|
MTU int `json:"mtu,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bytes outputs the configuration as []byte
|
// Bytes outputs the configuration as []byte
|
||||||
|
@ -249,6 +249,7 @@ func createBridge(name string, options entities.NetworkCreateOptions, runtimeCon
|
|||||||
|
|
||||||
func createMacVLAN(name string, options entities.NetworkCreateOptions, runtimeConfig *config.Config) (string, error) {
|
func createMacVLAN(name string, options entities.NetworkCreateOptions, runtimeConfig *config.Config) (string, error) {
|
||||||
var (
|
var (
|
||||||
|
mtu int
|
||||||
plugins []CNIPlugins
|
plugins []CNIPlugins
|
||||||
)
|
)
|
||||||
liveNetNames, err := GetLiveNetworkNames()
|
liveNetNames, err := GetLiveNetworkNames()
|
||||||
@ -283,7 +284,19 @@ func createMacVLAN(name string, options entities.NetworkCreateOptions, runtimeCo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ncList := NewNcList(name, version.Current(), options.Labels)
|
ncList := NewNcList(name, version.Current(), options.Labels)
|
||||||
macvlan := NewMacVLANPlugin(parentNetworkDevice)
|
if val, ok := options.Options["mtu"]; ok {
|
||||||
|
intVal, err := strconv.Atoi(val)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if intVal > 0 {
|
||||||
|
mtu = intVal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
macvlan, err := NewMacVLANPlugin(parentNetworkDevice, options.Gateway, &options.Range, &options.Subnet, mtu)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
plugins = append(plugins, macvlan)
|
plugins = append(plugins, macvlan)
|
||||||
ncList["plugins"] = plugins
|
ncList["plugins"] = plugins
|
||||||
b, err := json.MarshalIndent(ncList, "", " ")
|
b, err := json.MarshalIndent(ncList, "", " ")
|
||||||
|
@ -172,19 +172,31 @@ func HasDNSNamePlugin(paths []string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewMacVLANPlugin creates a macvlanconfig with a given device name
|
// NewMacVLANPlugin creates a macvlanconfig with a given device name
|
||||||
func NewMacVLANPlugin(device string) MacVLANConfig {
|
func NewMacVLANPlugin(device string, gateway net.IP, ipRange *net.IPNet, subnet *net.IPNet, mtu int) (MacVLANConfig, error) {
|
||||||
i := IPAMDHCP{DHCP: "dhcp"}
|
i := IPAMDHCP{DHCP: "dhcp"}
|
||||||
|
if gateway != nil || ipRange != nil || subnet != nil {
|
||||||
|
ipam, err := NewIPAMLocalHostRange(subnet, ipRange, gateway)
|
||||||
|
if err != nil {
|
||||||
|
return MacVLANConfig{}, err
|
||||||
|
}
|
||||||
|
ranges := make([][]IPAMLocalHostRangeConf, 0)
|
||||||
|
ranges = append(ranges, ipam)
|
||||||
|
i.Ranges = ranges
|
||||||
|
}
|
||||||
|
|
||||||
m := MacVLANConfig{
|
m := MacVLANConfig{
|
||||||
PluginType: "macvlan",
|
PluginType: "macvlan",
|
||||||
IPAM: i,
|
IPAM: i,
|
||||||
}
|
}
|
||||||
|
if mtu > 0 {
|
||||||
|
m.MTU = mtu
|
||||||
|
}
|
||||||
// CNI is supposed to use the default route if a
|
// CNI is supposed to use the default route if a
|
||||||
// parent device is not provided
|
// parent device is not provided
|
||||||
if len(device) > 0 {
|
if len(device) > 0 {
|
||||||
m.Master = device
|
m.Master = device
|
||||||
}
|
}
|
||||||
return m
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// IfPassesFilter filters NetworkListReport and returns true if the filter match the given config
|
// IfPassesFilter filters NetworkListReport and returns true if the filter match the given config
|
||||||
|
@ -487,7 +487,6 @@ var _ = Describe("Podman network", func() {
|
|||||||
inspect := podmanTest.Podman([]string{"network", "inspect", net})
|
inspect := podmanTest.Podman([]string{"network", "inspect", net})
|
||||||
inspect.WaitWithDefaultTimeout()
|
inspect.WaitWithDefaultTimeout()
|
||||||
Expect(inspect.ExitCode()).To(BeZero())
|
Expect(inspect.ExitCode()).To(BeZero())
|
||||||
fmt.Println(inspect.OutputToString())
|
|
||||||
|
|
||||||
out, err := inspect.jq(".[0].plugins[0].master")
|
out, err := inspect.jq(".[0].plugins[0].master")
|
||||||
Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
@ -513,4 +512,32 @@ var _ = Describe("Podman network", func() {
|
|||||||
session.WaitWithDefaultTimeout()
|
session.WaitWithDefaultTimeout()
|
||||||
Expect(session.ExitCode()).To(Equal(1))
|
Expect(session.ExitCode()).To(Equal(1))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("podman network create macvlan with network info and options", func() {
|
||||||
|
net := "macvlan" + stringid.GenerateNonCryptoID()
|
||||||
|
nc := podmanTest.Podman([]string{"network", "create", "-d", "macvlan", "-o", "parent=lo", "-o", "mtu=1500", "--gateway", "192.168.1.254", "--subnet", "192.168.1.0/24", net})
|
||||||
|
nc.WaitWithDefaultTimeout()
|
||||||
|
defer podmanTest.removeCNINetwork(net)
|
||||||
|
Expect(nc.ExitCode()).To(Equal(0))
|
||||||
|
|
||||||
|
inspect := podmanTest.Podman([]string{"network", "inspect", net})
|
||||||
|
inspect.WaitWithDefaultTimeout()
|
||||||
|
Expect(inspect.ExitCode()).To(BeZero())
|
||||||
|
|
||||||
|
mtu, err := inspect.jq(".[0].plugins[0].mtu")
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
Expect(mtu).To(Equal("1500"))
|
||||||
|
|
||||||
|
gw, err := inspect.jq(".[0].plugins[0].ipam.ranges[0][0].gateway")
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
Expect(gw).To(Equal("\"192.168.1.254\""))
|
||||||
|
|
||||||
|
subnet, err := inspect.jq(".[0].plugins[0].ipam.ranges[0][0].subnet")
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
Expect(subnet).To(Equal("\"192.168.1.0/24\""))
|
||||||
|
|
||||||
|
nc = podmanTest.Podman([]string{"network", "rm", net})
|
||||||
|
nc.WaitWithDefaultTimeout()
|
||||||
|
Expect(nc.ExitCode()).To(Equal(0))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user