Skip to content

Commit

Permalink
fix: function NetworkIsAvailable refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cheina97 authored and adamjensenbot committed Dec 4, 2024
1 parent 04efe0c commit f7c2de7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
21 changes: 9 additions & 12 deletions pkg/ipam/core/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ func (ipam *Ipam) NetworkAcquire(size int) *netip.Prefix {
// It returns the allocated network or nil if the network is not available.
func (ipam *Ipam) NetworkAcquireWithPrefix(prefix netip.Prefix) *netip.Prefix {
for i := range ipam.roots {
if result := allocateNetworkWithPrefix(prefix, &ipam.roots[i]); result != nil {
return result
if isPrefixChildOf(ipam.roots[i].prefix, prefix) {
if result := allocateNetworkWithPrefix(prefix, &ipam.roots[i]); result != nil {
return result
}
}
}
return nil
Expand Down Expand Up @@ -95,17 +97,12 @@ func (ipam *Ipam) ListNetworks() []netip.Prefix {
// NetworkIsAvailable checks if the network with the given prefix is allocated.
// It returns false if the network is allocated or there is no suitable pool, true otherwise.
func (ipam *Ipam) NetworkIsAvailable(prefix netip.Prefix) bool {
node, err := ipam.search(prefix)
if err != nil {
return false
}
if node == nil {
return true
}
if node.left != nil || node.right != nil {
return false
for i := range ipam.roots {
if isPrefixChildOf(ipam.roots[i].prefix, prefix) {
return networkIsAvailable(prefix, &ipam.roots[i])
}
}
return !node.acquired
return false
}

// IPAcquire allocates an IP address from the given prefix.
Expand Down
33 changes: 30 additions & 3 deletions pkg/ipam/core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,33 @@ func networkRelease(prefix netip.Prefix, node *node) *netip.Prefix {
return result
}

func networkIsAvailable(prefix netip.Prefix, node *node) bool {
if node.prefix.Addr().Compare(prefix.Addr()) == 0 && node.prefix.Bits() == prefix.Bits() {
if node.left != nil && node.left.left.isSplitted() {
return false
}
if node.right != nil && node.right.isSplitted() {
return false
}

// If node children are not splitted and node is not acquired, then network is available
return !node.acquired
}

if node.left == nil && node.right == nil {
return true
}

if node.left != nil && node.left.prefix.Overlaps(prefix) && !node.left.acquired {
return networkIsAvailable(prefix, node.left)
}
if node.right != nil && node.right.prefix.Overlaps(prefix) && !node.right.acquired {
return networkIsAvailable(prefix, node.right)
}

return false
}

func listNetworks(node *node) []netip.Prefix {
if node == nil {
return nil
Expand Down Expand Up @@ -308,13 +335,13 @@ func (n *node) toGraphviz() error {
n.toGraphvizRecursive(&sb)
sb.WriteString("}\n")

if _, err := os.Stat("/graphviz"); os.IsNotExist(err) {
if err := os.Mkdir("/graphviz", 0o700); err != nil {
if _, err := os.Stat("./graphviz"); os.IsNotExist(err) {
if err := os.Mkdir("./graphviz", 0o700); err != nil {
return err
}
}

filePath := filepath.Clean("/graphviz/" + strings.NewReplacer("/", "_", ".", "_").Replace(n.prefix.String()) + ".dot")
filePath := filepath.Clean("./graphviz/" + strings.NewReplacer("/", "_", ".", "_").Replace(n.prefix.String()) + ".dot")
file, err := os.Create(filePath)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions pkg/ipam/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (lipam *LiqoIPAM) sync(ctx context.Context, syncFrequency time.Duration) {
func(ctx context.Context) (done bool, err error) {
lipam.mutex.Lock()
defer lipam.mutex.Unlock()
klog.Info("Started IPAM cache sync routine")
klog.V(3).Info("Started IPAM cache sync routine")

// Sync networks.
if err := lipam.syncNetworks(ctx); err != nil {
Expand All @@ -53,7 +53,7 @@ func (lipam *LiqoIPAM) sync(ctx context.Context, syncFrequency time.Duration) {
return false, err
}

klog.Info("Completed IPAM cache sync routine")
klog.V(3).Info("Completed IPAM cache sync routine")
return false, nil
})
if err != nil {
Expand Down

0 comments on commit f7c2de7

Please sign in to comment.