Skip to content

Commit

Permalink
Merge pull request containers#881 from Luap99/libnetwork
Browse files Browse the repository at this point in the history
libnetwork: verify static ip only for host-local ipam and allow mtu option for netavark macvlan driver
  • Loading branch information
openshift-merge-robot authored Jan 10, 2022
2 parents 7b510ef + bf7bc2b commit 08c2c97
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 34 deletions.
14 changes: 8 additions & 6 deletions libnetwork/internal/util/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,16 @@ func validatePerNetworkOpts(network *types.Network, netOpts *types.PerNetworkOpt
if netOpts.InterfaceName == "" {
return errors.Errorf("interface name on network %s is empty", network.Name)
}
outer:
for _, ip := range netOpts.StaticIPs {
for _, s := range network.Subnets {
if s.Subnet.Contains(ip) {
continue outer
if network.IPAMOptions["driver"] == types.HostLocalIPAMDriver {
outer:
for _, ip := range netOpts.StaticIPs {
for _, s := range network.Subnets {
if s.Subnet.Contains(ip) {
continue outer
}
}
return errors.Errorf("requested static ip %s not in any subnet on network %s", ip.String(), network.Name)
}
return errors.Errorf("requested static ip %s not in any subnet on network %s", ip.String(), network.Name)
}
return nil
}
68 changes: 40 additions & 28 deletions libnetwork/netavark/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,35 +107,10 @@ func (n *netavarkNetwork) networkCreate(newNetwork *types.Network, defaultNet bo
}
}
case types.MacVLANNetworkDriver:
if newNetwork.Internal {
return nil, errors.New("internal is not supported with macvlan")
}
if newNetwork.NetworkInterface != "" {
interfaceNames, err := internalutil.GetLiveNetworkNames()
if err != nil {
return nil, err
}
if !util.StringInSlice(newNetwork.NetworkInterface, interfaceNames) {
return nil, errors.Errorf("parent interface %s does not exist", newNetwork.NetworkInterface)
}
}
if len(newNetwork.Subnets) == 0 {
return nil, errors.Errorf("macvlan driver needs at least one subnet specified, DHCP is not supported with netavark")
}
newNetwork.IPAMOptions["driver"] = types.HostLocalIPAMDriver

// validate the given options, we do not need them but just check to make sure they are valid
for key, value := range newNetwork.Options {
switch key {
case "mode":
if !util.StringInSlice(value, types.ValidMacVLANModes) {
return nil, errors.Errorf("unknown macvlan mode %q", value)
}
default:
return nil, errors.Errorf("unsupported macvlan network option %s", key)
}
err = createMacvlan(newNetwork)
if err != nil {
return nil, err
}

default:
return nil, errors.Wrapf(types.ErrInvalidArg, "unsupported driver %s", newNetwork.Driver)
}
Expand Down Expand Up @@ -169,6 +144,43 @@ func (n *netavarkNetwork) networkCreate(newNetwork *types.Network, defaultNet bo
return newNetwork, nil
}

func createMacvlan(network *types.Network) error {
if network.Internal {
return errors.New("internal is not supported with macvlan")
}
if network.NetworkInterface != "" {
interfaceNames, err := internalutil.GetLiveNetworkNames()
if err != nil {
return err
}
if !util.StringInSlice(network.NetworkInterface, interfaceNames) {
return errors.Errorf("parent interface %s does not exist", network.NetworkInterface)
}
}
if len(network.Subnets) == 0 {
return errors.Errorf("macvlan driver needs at least one subnet specified, DHCP is not supported with netavark")
}
network.IPAMOptions["driver"] = types.HostLocalIPAMDriver

// validate the given options, we do not need them but just check to make sure they are valid
for key, value := range network.Options {
switch key {
case "mode":
if !util.StringInSlice(value, types.ValidMacVLANModes) {
return errors.Errorf("unknown macvlan mode %q", value)
}
case "mtu":
_, err := internalutil.ParseMTU(value)
if err != nil {
return err
}
default:
return errors.Errorf("unsupported macvlan network option %s", key)
}
}
return nil
}

// NetworkRemove will remove the Network with the given name or ID.
// It does not ensure that the network is unused.
func (n *netavarkNetwork) NetworkRemove(nameOrID string) error {
Expand Down
18 changes: 18 additions & 0 deletions libnetwork/netavark/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,24 @@ var _ = Describe("Config", func() {
Expect(err.Error()).To(Equal("unsupported macvlan network option abc"))
})

It("create macvlan config with mtu", func() {
subnet := "10.1.0.0/24"
n, _ := types.ParseCIDR(subnet)
network := types.Network{
Driver: "macvlan",
Subnets: []types.Subnet{
{Subnet: n},
},
Options: map[string]string{
"mtu": "9000",
},
}
network1, err := libpodNet.NetworkCreate(network)
Expect(err).To(BeNil())
Expect(network1.Name).ToNot(BeEmpty())
Expect(network1.Options).To(HaveKeyWithValue("mtu", "9000"))
})

})

Context("network load valid existing ones", func() {
Expand Down

0 comments on commit 08c2c97

Please sign in to comment.