diff --git a/internal/server/network/driver_ovn.go b/internal/server/network/driver_ovn.go index 74efb16688a..cf7059f3df6 100644 --- a/internal/server/network/driver_ovn.go +++ b/internal/server/network/driver_ovn.go @@ -1959,38 +1959,41 @@ func (n *ovn) getDHCPv4Reservations() ([]iprange.Range, error) { } var dhcpReserveIPv4s []iprange.Range + if routerIntPortIPv4 != nil { + if n.config["ipv4.dhcp.ranges"] == "" { + dhcpReserveIPv4s = []iprange.Range{{Start: routerIntPortIPv4}, {Start: dhcpalloc.GetIP(ipv4Net, -2)}} + } else { + allowedNets := []*net.IPNet{n.DHCPv4Subnet()} + dhcpRanges, err := parseIPRanges(n.config["ipv4.dhcp.ranges"], allowedNets...) + if err != nil { + return nil, err + } - if routerIntPortIPv4 != nil && n.config["ipv4.dhcp.ranges"] != "" { - allowedNets := []*net.IPNet{n.DHCPv4Subnet()} - dhcpRanges, err := parseIPRanges(n.config["ipv4.dhcp.ranges"], allowedNets...) - if err != nil { - return nil, err - } + lastIP := routerIntPortIPv4 - lastIP := routerIntPortIPv4 + sort.Slice(dhcpRanges, func(i, j int) bool { + return bytes.Compare(dhcpRanges[i].Start, dhcpRanges[j].Start) < 0 + }) - sort.Slice(dhcpRanges, func(i, j int) bool { - return bytes.Compare(dhcpRanges[i].Start, dhcpRanges[j].Start) < 0 - }) + for _, dhcpRange := range dhcpRanges { + startRangeAddr, err := netip.ParseAddr(dhcpRange.Start.String()) + if err != nil { + return nil, err + } + endRangeAddr, err := netip.ParseAddr(dhcpRange.End.String()) + if err != nil { + return nil, err + } - for _, dhcpRange := range dhcpRanges { - startRangeAddr, err := netip.ParseAddr(dhcpRange.Start.String()) - if err != nil { - return nil, err - } - endRangeAddr, err := netip.ParseAddr(dhcpRange.End.String()) - if err != nil { - return nil, err - } + prevStartRangeIP, nextEndRangeIP := startRangeAddr.Prev().String(), endRangeAddr.Next().String() + complementRange := iprange.Range{Start: lastIP, End: net.ParseIP(prevStartRangeIP)} - 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) + } - lastIP = net.ParseIP(nextEndRangeIP) - dhcpReserveIPv4s = append(dhcpReserveIPv4s, complementRange) + dhcpReserveIPv4s = append(dhcpReserveIPv4s, iprange.Range{Start: lastIP, End: dhcpalloc.GetIP(ipv4Net, -2)}) } - - dhcpReserveIPv4s = append(dhcpReserveIPv4s, iprange.Range{Start: lastIP, End: dhcpalloc.GetIP(ipv4Net, -2)}) } err = UsedByInstanceDevices(n.state, n.Project(), n.Name(), n.Type(), func(inst db.InstanceArgs, nicName string, nicConfig map[string]string) error {