Skip to content

Commit f7c2de7

Browse files
cheina97adamjensenbot
authored andcommitted
fix: function NetworkIsAvailable refactoring
1 parent 04efe0c commit f7c2de7

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

pkg/ipam/core/ipam.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ func (ipam *Ipam) NetworkAcquire(size int) *netip.Prefix {
6363
// It returns the allocated network or nil if the network is not available.
6464
func (ipam *Ipam) NetworkAcquireWithPrefix(prefix netip.Prefix) *netip.Prefix {
6565
for i := range ipam.roots {
66-
if result := allocateNetworkWithPrefix(prefix, &ipam.roots[i]); result != nil {
67-
return result
66+
if isPrefixChildOf(ipam.roots[i].prefix, prefix) {
67+
if result := allocateNetworkWithPrefix(prefix, &ipam.roots[i]); result != nil {
68+
return result
69+
}
6870
}
6971
}
7072
return nil
@@ -95,17 +97,12 @@ func (ipam *Ipam) ListNetworks() []netip.Prefix {
9597
// NetworkIsAvailable checks if the network with the given prefix is allocated.
9698
// It returns false if the network is allocated or there is no suitable pool, true otherwise.
9799
func (ipam *Ipam) NetworkIsAvailable(prefix netip.Prefix) bool {
98-
node, err := ipam.search(prefix)
99-
if err != nil {
100-
return false
101-
}
102-
if node == nil {
103-
return true
104-
}
105-
if node.left != nil || node.right != nil {
106-
return false
100+
for i := range ipam.roots {
101+
if isPrefixChildOf(ipam.roots[i].prefix, prefix) {
102+
return networkIsAvailable(prefix, &ipam.roots[i])
103+
}
107104
}
108-
return !node.acquired
105+
return false
109106
}
110107

111108
// IPAcquire allocates an IP address from the given prefix.

pkg/ipam/core/node.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,33 @@ func networkRelease(prefix netip.Prefix, node *node) *netip.Prefix {
121121
return result
122122
}
123123

124+
func networkIsAvailable(prefix netip.Prefix, node *node) bool {
125+
if node.prefix.Addr().Compare(prefix.Addr()) == 0 && node.prefix.Bits() == prefix.Bits() {
126+
if node.left != nil && node.left.left.isSplitted() {
127+
return false
128+
}
129+
if node.right != nil && node.right.isSplitted() {
130+
return false
131+
}
132+
133+
// If node children are not splitted and node is not acquired, then network is available
134+
return !node.acquired
135+
}
136+
137+
if node.left == nil && node.right == nil {
138+
return true
139+
}
140+
141+
if node.left != nil && node.left.prefix.Overlaps(prefix) && !node.left.acquired {
142+
return networkIsAvailable(prefix, node.left)
143+
}
144+
if node.right != nil && node.right.prefix.Overlaps(prefix) && !node.right.acquired {
145+
return networkIsAvailable(prefix, node.right)
146+
}
147+
148+
return false
149+
}
150+
124151
func listNetworks(node *node) []netip.Prefix {
125152
if node == nil {
126153
return nil
@@ -308,13 +335,13 @@ func (n *node) toGraphviz() error {
308335
n.toGraphvizRecursive(&sb)
309336
sb.WriteString("}\n")
310337

311-
if _, err := os.Stat("/graphviz"); os.IsNotExist(err) {
312-
if err := os.Mkdir("/graphviz", 0o700); err != nil {
338+
if _, err := os.Stat("./graphviz"); os.IsNotExist(err) {
339+
if err := os.Mkdir("./graphviz", 0o700); err != nil {
313340
return err
314341
}
315342
}
316343

317-
filePath := filepath.Clean("/graphviz/" + strings.NewReplacer("/", "_", ".", "_").Replace(n.prefix.String()) + ".dot")
344+
filePath := filepath.Clean("./graphviz/" + strings.NewReplacer("/", "_", ".", "_").Replace(n.prefix.String()) + ".dot")
318345
file, err := os.Create(filePath)
319346
if err != nil {
320347
return err

pkg/ipam/sync.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (lipam *LiqoIPAM) sync(ctx context.Context, syncFrequency time.Duration) {
4141
func(ctx context.Context) (done bool, err error) {
4242
lipam.mutex.Lock()
4343
defer lipam.mutex.Unlock()
44-
klog.Info("Started IPAM cache sync routine")
44+
klog.V(3).Info("Started IPAM cache sync routine")
4545

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

56-
klog.Info("Completed IPAM cache sync routine")
56+
klog.V(3).Info("Completed IPAM cache sync routine")
5757
return false, nil
5858
})
5959
if err != nil {

0 commit comments

Comments
 (0)