Skip to content

Commit

Permalink
libnetwork: export "driver" as constant
Browse files Browse the repository at this point in the history
It is better to use a global const than having to type "driver" at every
place.

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Mar 17, 2022
1 parent 9c31ab7 commit 324a8d2
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions libnetwork/cni/cni_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ func findPluginByName(plugins []*libcni.NetworkConfig, name string) bool {
// It returns an array of subnets and an extra bool if dhcp is configured.
func convertIPAMConfToNetwork(network *types.Network, ipam *ipamConfig, confPath string) error {
if ipam.PluginType == types.DHCPIPAMDriver {
network.IPAMOptions["driver"] = types.DHCPIPAMDriver
network.IPAMOptions[types.Driver] = types.DHCPIPAMDriver
return nil
}

if ipam.PluginType != types.HostLocalIPAMDriver {
return errors.Errorf("unsupported ipam plugin %s in %s", ipam.PluginType, confPath)
}

network.IPAMOptions["driver"] = types.HostLocalIPAMDriver
network.IPAMOptions[types.Driver] = types.HostLocalIPAMDriver
for _, r := range ipam.Ranges {
for _, ipam := range r {
s := types.Subnet{}
Expand Down
4 changes: 2 additions & 2 deletions libnetwork/cni/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ func createIPMACVLAN(network *types.Network) error {
}
}
if len(network.Subnets) == 0 {
network.IPAMOptions["driver"] = types.DHCPIPAMDriver
network.IPAMOptions[types.Driver] = types.DHCPIPAMDriver
if network.Internal {
return errors.New("internal is not supported with macvlan and dhcp ipam driver")
}
} else {
network.IPAMOptions["driver"] = types.HostLocalIPAMDriver
network.IPAMOptions[types.Driver] = types.HostLocalIPAMDriver
}
return nil
}
4 changes: 2 additions & 2 deletions libnetwork/internal/util/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func CreateBridge(n NetUtil, network *types.Network, usedNetworks []*net.IPNet,
}
}

if network.IPAMOptions["driver"] != types.DHCPIPAMDriver {
if network.IPAMOptions[types.Driver] != types.DHCPIPAMDriver {
if len(network.Subnets) == 0 {
freeSubnet, err := GetFreeIPv4NetworkSubnet(usedNetworks, subnetPools)
if err != nil {
Expand Down Expand Up @@ -63,7 +63,7 @@ func CreateBridge(n NetUtil, network *types.Network, usedNetworks []*net.IPNet,
network.Subnets = append(network.Subnets, *freeSubnet)
}
}
network.IPAMOptions["driver"] = types.HostLocalIPAMDriver
network.IPAMOptions[types.Driver] = types.HostLocalIPAMDriver
}
return nil
}
2 changes: 1 addition & 1 deletion libnetwork/internal/util/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func validatePerNetworkOpts(network *types.Network, netOpts *types.PerNetworkOpt
if netOpts.InterfaceName == "" {
return errors.Errorf("interface name on network %s is empty", network.Name)
}
if network.IPAMOptions["driver"] == types.HostLocalIPAMDriver {
if network.IPAMOptions[types.Driver] == types.HostLocalIPAMDriver {
outer:
for _, ip := range netOpts.StaticIPs {
for _, s := range network.Subnets {
Expand Down
2 changes: 1 addition & 1 deletion libnetwork/netavark/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func createMacvlan(network *types.Network) error {
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
network.IPAMOptions[types.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 {
Expand Down
2 changes: 1 addition & 1 deletion libnetwork/netavark/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func (n *netavarkNetwork) deallocIPs(opts *types.NetworkOptions) error {
// it checks the ipam driver and if subnets are set
func requiresIPAMAlloc(network *types.Network) bool {
// only do host allocation when driver is set to HostLocalIPAMDriver or unset
switch network.IPAMOptions["driver"] {
switch network.IPAMOptions[types.Driver] {
case "", types.HostLocalIPAMDriver:
default:
return false
Expand Down
1 change: 1 addition & 0 deletions libnetwork/types/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
IPVLANNetworkDriver = "ipvlan"

// IPAM drivers
Driver = "driver"
// HostLocalIPAMDriver store the ip
HostLocalIPAMDriver = "host-local"
// DHCPIPAMDriver get subnet and ip from dhcp server
Expand Down
2 changes: 1 addition & 1 deletion libnetwork/util/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func createFilterFuncs(key string, filterValues []string) (types.FilterFunc, err
return util.StringMatchRegexSlice(net.Name, filterValues)
}, nil

case "driver":
case types.Driver:
// matches network driver
return func(net types.Network) bool {
return util.StringInSlice(net.Driver, filterValues)
Expand Down

0 comments on commit 324a8d2

Please sign in to comment.