Skip to content

Commit

Permalink
fix: reserve first and last IP if ipv4.dhcp.ranges is empty
Browse files Browse the repository at this point in the history
Signed-off-by: Jonatas Ferreira <jonatas.lima@luizalabs.com>
  • Loading branch information
jonatas-lima committed Oct 30, 2024
1 parent 52cc374 commit 3e32557
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions internal/server/network/driver_ovn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3e32557

Please sign in to comment.