Merge pull request #10408 from Luap99/fix-10283

Fix network create macvlan with subnet option
This commit is contained in:
OpenShift Merge Robot
2021-05-25 12:42:07 -07:00
committed by GitHub
3 changed files with 54 additions and 33 deletions

View File

@ -54,7 +54,7 @@ type HostLocalBridge struct {
HairpinMode bool `json:"hairpinMode,omitempty"` HairpinMode bool `json:"hairpinMode,omitempty"`
PromiscMode bool `json:"promiscMode,omitempty"` PromiscMode bool `json:"promiscMode,omitempty"`
Vlan int `json:"vlan,omitempty"` Vlan int `json:"vlan,omitempty"`
IPAM IPAMHostLocalConf `json:"ipam"` IPAM IPAMConfig `json:"ipam"`
} }
// Bytes outputs []byte // Bytes outputs []byte
@ -62,9 +62,9 @@ func (h *HostLocalBridge) Bytes() ([]byte, error) {
return json.MarshalIndent(h, "", "\t") return json.MarshalIndent(h, "", "\t")
} }
// IPAMHostLocalConf describes an IPAM configuration // IPAMConfig describes an IPAM configuration
// https://github.com/containernetworking/plugins/tree/master/plugins/ipam/host-local#network-configuration-reference // https://github.com/containernetworking/plugins/tree/master/plugins/ipam/host-local#network-configuration-reference
type IPAMHostLocalConf struct { type IPAMConfig struct {
PluginType string `json:"type"` PluginType string `json:"type"`
Routes []IPAMRoute `json:"routes,omitempty"` Routes []IPAMRoute `json:"routes,omitempty"`
ResolveConf string `json:"resolveConf,omitempty"` ResolveConf string `json:"resolveConf,omitempty"`
@ -81,7 +81,7 @@ type IPAMLocalHostRangeConf struct {
} }
// Bytes outputs the configuration as []byte // Bytes outputs the configuration as []byte
func (i IPAMHostLocalConf) Bytes() ([]byte, error) { func (i IPAMConfig) Bytes() ([]byte, error) {
return json.MarshalIndent(i, "", "\t") return json.MarshalIndent(i, "", "\t")
} }
@ -101,18 +101,11 @@ func (p PortMapConfig) Bytes() ([]byte, error) {
return json.MarshalIndent(p, "", "\t") return json.MarshalIndent(p, "", "\t")
} }
// IPAMDHCP describes the ipamdhcp config
type IPAMDHCP struct {
DHCP string `json:"type"`
Routes []IPAMRoute `json:"routes,omitempty"`
Ranges [][]IPAMLocalHostRangeConf `json:"ranges,omitempty"`
}
// MacVLANConfig describes the macvlan config // MacVLANConfig describes the macvlan config
type MacVLANConfig struct { type MacVLANConfig struct {
PluginType string `json:"type"` PluginType string `json:"type"`
Master string `json:"master"` Master string `json:"master"`
IPAM IPAMDHCP `json:"ipam"` IPAM IPAMConfig `json:"ipam"`
MTU int `json:"mtu,omitempty"` MTU int `json:"mtu,omitempty"`
} }

View File

@ -45,7 +45,7 @@ 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, mtu int, vlan int, ipamConf IPAMHostLocalConf) *HostLocalBridge { func NewHostLocalBridge(name string, isGateWay, isDefaultGW, ipMasq bool, mtu int, vlan int, ipamConf IPAMConfig) *HostLocalBridge {
hostLocalBridge := HostLocalBridge{ hostLocalBridge := HostLocalBridge{
PluginType: "bridge", PluginType: "bridge",
BrName: name, BrName: name,
@ -65,8 +65,8 @@ func NewHostLocalBridge(name string, isGateWay, isDefaultGW, ipMasq bool, mtu in
} }
// NewIPAMHostLocalConf creates a new IPAMHostLocal configuration // NewIPAMHostLocalConf creates a new IPAMHostLocal configuration
func NewIPAMHostLocalConf(routes []IPAMRoute, ipamRanges [][]IPAMLocalHostRangeConf) (IPAMHostLocalConf, error) { func NewIPAMHostLocalConf(routes []IPAMRoute, ipamRanges [][]IPAMLocalHostRangeConf) (IPAMConfig, error) {
ipamConf := IPAMHostLocalConf{ ipamConf := IPAMConfig{
PluginType: "host-local", PluginType: "host-local",
Routes: routes, Routes: routes,
// Possible future support ? Leaving for clues // Possible future support ? Leaving for clues
@ -177,8 +177,10 @@ 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, gateway net.IP, ipRange *net.IPNet, subnet *net.IPNet, mtu int) (MacVLANConfig, error) { func NewMacVLANPlugin(device string, gateway net.IP, ipRange *net.IPNet, subnet *net.IPNet, mtu int) (MacVLANConfig, error) {
i := IPAMDHCP{DHCP: "dhcp"} i := IPAMConfig{PluginType: "dhcp"}
if gateway != nil || ipRange != nil || subnet != nil { if gateway != nil ||
(ipRange != nil && ipRange.IP != nil && ipRange.Mask != nil) ||
(subnet != nil && subnet.IP != nil && subnet.Mask != nil) {
ipam, err := NewIPAMLocalHostRange(subnet, ipRange, gateway) ipam, err := NewIPAMLocalHostRange(subnet, ipRange, gateway)
if err != nil { if err != nil {
return MacVLANConfig{}, err return MacVLANConfig{}, err
@ -186,6 +188,12 @@ func NewMacVLANPlugin(device string, gateway net.IP, ipRange *net.IPNet, subnet
ranges := make([][]IPAMLocalHostRangeConf, 0) ranges := make([][]IPAMLocalHostRangeConf, 0)
ranges = append(ranges, ipam) ranges = append(ranges, ipam)
i.Ranges = ranges i.Ranges = ranges
route, err := NewIPAMDefaultRoute(IsIPv6(subnet.IP))
if err != nil {
return MacVLANConfig{}, err
}
i.Routes = []IPAMRoute{route}
i.PluginType = "host-local"
} }
m := MacVLANConfig{ m := MacVLANConfig{

View File

@ -533,7 +533,11 @@ var _ = Describe("Podman network", func() {
out, err := inspect.jq(".[0].plugins[0].master") out, err := inspect.jq(".[0].plugins[0].master")
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(out).To(Equal("\"lo\"")) Expect(out).To(Equal(`"lo"`))
ipamType, err := inspect.jq(".[0].plugins[0].ipam.type")
Expect(err).To(BeNil())
Expect(ipamType).To(Equal(`"dhcp"`))
nc = podmanTest.Podman([]string{"network", "rm", net}) nc = podmanTest.Podman([]string{"network", "rm", net})
nc.WaitWithDefaultTimeout() nc.WaitWithDefaultTimeout()
@ -571,13 +575,29 @@ var _ = Describe("Podman network", func() {
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(mtu).To(Equal("1500")) Expect(mtu).To(Equal("1500"))
name, err := inspect.jq(".[0].plugins[0].type")
Expect(err).To(BeNil())
Expect(name).To(Equal(`"macvlan"`))
netInt, err := inspect.jq(".[0].plugins[0].master")
Expect(err).To(BeNil())
Expect(netInt).To(Equal(`"lo"`))
ipamType, err := inspect.jq(".[0].plugins[0].ipam.type")
Expect(err).To(BeNil())
Expect(ipamType).To(Equal(`"host-local"`))
gw, err := inspect.jq(".[0].plugins[0].ipam.ranges[0][0].gateway") gw, err := inspect.jq(".[0].plugins[0].ipam.ranges[0][0].gateway")
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(gw).To(Equal("\"192.168.1.254\"")) Expect(gw).To(Equal(`"192.168.1.254"`))
subnet, err := inspect.jq(".[0].plugins[0].ipam.ranges[0][0].subnet") subnet, err := inspect.jq(".[0].plugins[0].ipam.ranges[0][0].subnet")
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(subnet).To(Equal("\"192.168.1.0/24\"")) Expect(subnet).To(Equal(`"192.168.1.0/24"`))
routes, err := inspect.jq(".[0].plugins[0].ipam.routes[0].dst")
Expect(err).To(BeNil())
Expect(routes).To(Equal(`"0.0.0.0/0"`))
nc = podmanTest.Podman([]string{"network", "rm", net}) nc = podmanTest.Podman([]string{"network", "rm", net})
nc.WaitWithDefaultTimeout() nc.WaitWithDefaultTimeout()