Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IPAM] function NetworkIsAvailable refactoring #2841

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
cheina97 marked this conversation as resolved.
Show resolved Hide resolved
}

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")
cheina97 marked this conversation as resolved.
Show resolved Hide resolved
klog.V(3).Info("Completed IPAM cache sync routine")
return false, nil
})
if err != nil {
Expand Down
Loading