diff --git a/internal/server/network/driver_ovn.go b/internal/server/network/driver_ovn.go index 0afbfacea31..268d55987c9 100644 --- a/internal/server/network/driver_ovn.go +++ b/internal/server/network/driver_ovn.go @@ -19,7 +19,7 @@ import ( "github.com/mdlayher/netx/eui64" ovsClient "github.com/ovn-org/libovsdb/client" - "github.com/lxc/incus/v6/client" + incus "github.com/lxc/incus/v6/client" "github.com/lxc/incus/v6/internal/iprange" "github.com/lxc/incus/v6/internal/server/cluster" "github.com/lxc/incus/v6/internal/server/cluster/request" @@ -1978,19 +1978,15 @@ func (n *ovn) getDHCPv4Reservations() ([]iprange.Range, error) { if err != nil { return nil, err } - - prevIP, nextIP := startRangeAddr.Prev().String(), startRangeAddr.Next().String() - complementRange := iprange.Range{Start: lastIP, End: net.ParseIP(prevIP)} - - if dhcpRange.End != nil { - endRangeAddr, err := netip.ParseAddr(dhcpRange.End.String()) - if err != nil { - return nil, err - } - nextIP = endRangeAddr.Next().String() + endRangeAddr, err := netip.ParseAddr(dhcpRange.End.String()) + if err != nil { + return nil, err } - lastIP = net.ParseIP(nextIP) + prevStartRangeIP, nextEndRangeIP := startRangeAddr.Prev().String(), endRangeAddr.Next().String() + complementRange := iprange.Range{Start: lastIP, End: net.ParseIP(prevStartRangeIP)} + + lastIP = net.ParseIP(nextEndRangeIP) dhcpReserveIPv4s = append(dhcpReserveIPv4s, complementRange) } @@ -2000,7 +1996,15 @@ func (n *ovn) getDHCPv4Reservations() ([]iprange.Range, error) { err = UsedByInstanceDevices(n.state, n.Project(), n.Name(), n.Type(), func(inst db.InstanceArgs, nicName string, nicConfig map[string]string) error { ip := net.ParseIP(nicConfig["ipv4.address"]) if ip != nil { - dhcpReserveIPv4s = append(dhcpReserveIPv4s, iprange.Range{Start: ip}) + containsIP := false + for _, reservedDhcpRange := range dhcpReserveIPv4s { + if containsIP = reservedDhcpRange.ContainsIP(ip); containsIP { + break + } + } + if !containsIP { + dhcpReserveIPv4s = append(dhcpReserveIPv4s, iprange.Range{Start: ip}) + } } return nil