Skip to content

Commit

Permalink
chore: check if an instance ip is already in dhcp reserved ranges
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 28, 2024
1 parent 6447628 commit 5c9b36d
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions internal/server/network/driver_ovn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand All @@ -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
Expand Down

0 comments on commit 5c9b36d

Please sign in to comment.