Skip to content

Commit adc7fd4

Browse files
committed
fix: function NetworkIsAvailable refactoring
1 parent 04efe0c commit adc7fd4

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-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: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,31 @@ 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 == nil {
126+
return true
127+
}
128+
129+
if node.prefix.Addr().Compare(prefix.Addr()) == 0 && node.prefix.Bits() == prefix.Bits() {
130+
if node.left != nil && (node.left.left != nil || node.left.right != nil) {
131+
return false
132+
}
133+
if node.right != nil && (node.right.left != nil || node.right.right != nil) {
134+
return false
135+
}
136+
return !node.acquired
137+
}
138+
139+
if node.left != nil && node.left.prefix.Overlaps(prefix) && !node.left.acquired {
140+
return networkIsAvailable(prefix, node.left)
141+
}
142+
if node.right != nil && node.right.prefix.Overlaps(prefix) && !node.right.acquired {
143+
return networkIsAvailable(prefix, node.right)
144+
}
145+
146+
return false
147+
}
148+
124149
func listNetworks(node *node) []netip.Prefix {
125150
if node == nil {
126151
return nil
@@ -308,13 +333,13 @@ func (n *node) toGraphviz() error {
308333
n.toGraphvizRecursive(&sb)
309334
sb.WriteString("}\n")
310335

311-
if _, err := os.Stat("/graphviz"); os.IsNotExist(err) {
312-
if err := os.Mkdir("/graphviz", 0o700); err != nil {
336+
if _, err := os.Stat("./graphviz"); os.IsNotExist(err) {
337+
if err := os.Mkdir("./graphviz", 0o700); err != nil {
313338
return err
314339
}
315340
}
316341

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

pkg/ipam/sync.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ 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")
4544

4645
// Sync networks.
4746
if err := lipam.syncNetworks(ctx); err != nil {
@@ -53,7 +52,6 @@ func (lipam *LiqoIPAM) sync(ctx context.Context, syncFrequency time.Duration) {
5352
return false, err
5453
}
5554

56-
klog.Info("Completed IPAM cache sync routine")
5755
return false, nil
5856
})
5957
if err != nil {

0 commit comments

Comments
 (0)