Skip to content

Commit

Permalink
lxd/network/ovn: Refactor validateUplinkNetwork
Browse files Browse the repository at this point in the history
This way we can easily fit an additional check for project uplink IP quotas on the end

Signed-off-by: hamistao <[email protected]>
  • Loading branch information
hamistao committed Dec 11, 2024
1 parent 0ec6e71 commit 66cfb8c
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions lxd/network/driver_ovn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2029,23 +2029,27 @@ func (n *ovn) validateUplinkNetwork(p *api.Project, uplinkNetworkName string) (s
return "", err
}

if uplinkNetworkName != "" {
if !shared.ValueInSlice(uplinkNetworkName, allowedUplinkNetworks) {
return "", fmt.Errorf(`Option "network" value %q is not one of the allowed uplink networks in project`, uplinkNetworkName)
// If provided uplink was invalid, return an error.
if uplinkNetworkName != "" && !shared.ValueInSlice(uplinkNetworkName, allowedUplinkNetworks) {
return "", fmt.Errorf(`Option "network" value %q is not one of the allowed uplink networks in project`, uplinkNetworkName)
}

// Derive uplink network if not provided and only one is valid.
if uplinkNetworkName == "" {
allowedNetworkCount := len(allowedUplinkNetworks)
if allowedNetworkCount == 0 {
return "", fmt.Errorf(`No allowed uplink networks in project`)
}

return uplinkNetworkName, nil
}
if allowedNetworkCount > 1 {
return "", fmt.Errorf(`Option "network" is required`)
}

allowedNetworkCount := len(allowedUplinkNetworks)
if allowedNetworkCount == 0 {
return "", fmt.Errorf(`No allowed uplink networks in project`)
} else if allowedNetworkCount == 1 {
// If there is only one allowed uplink network then use it if not specified by user.
return allowedUplinkNetworks[0], nil
uplinkNetworkName = allowedUplinkNetworks[0]
}

return "", fmt.Errorf(`Option "network" is required`)
return uplinkNetworkName, nil
}

// getDHCPv4Reservations returns list DHCP IPv4 reservations from NICs connected to this network.
Expand Down

0 comments on commit 66cfb8c

Please sign in to comment.