Skip to content

Commit 41299a9

Browse files
committed
fixup! ipam core addition 2
1 parent 03e560a commit 41299a9

File tree

5 files changed

+54
-35
lines changed

5 files changed

+54
-35
lines changed

pkg/ipam/initialize.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ package ipam
1616

1717
import (
1818
"context"
19-
"fmt"
20-
"net/netip"
2119

2220
klog "k8s.io/klog/v2"
2321
)
@@ -26,13 +24,19 @@ import (
2624
// +kubebuilder:rbac:groups=ipam.liqo.io,resources=networks,verbs=get;list;watch
2725

2826
func (lipam *LiqoIPAM) initialize(ctx context.Context) error {
27+
klog.Info("Initializing IPAM")
28+
29+
klog.Info("Initializing networks")
2930
if err := lipam.initializeNetworks(ctx); err != nil {
3031
return err
3132
}
33+
klog.Info("Networks initialized")
3234

35+
klog.Info("Initializing IPs")
3336
if err := lipam.initializeIPs(ctx); err != nil {
3437
return err
3538
}
39+
klog.Info("IPs initialized")
3640

3741
klog.Info("IPAM initialized")
3842
return nil
@@ -47,11 +51,7 @@ func (lipam *LiqoIPAM) initializeNetworks(ctx context.Context) error {
4751

4852
// Initialize the networks.
4953
for _, net := range nets {
50-
prefix := netip.MustParsePrefix(net)
51-
52-
if result := lipam.IpamCore.AllocateNetworkWithPrefix(prefix); result == nil {
53-
err := fmt.Errorf("failed to reserve network %q", net)
54-
klog.Errorf("%v", err)
54+
if _, err := lipam.acquireSpecificNetwork(net); err != nil {
5555
return err
5656
}
5757
}
@@ -68,7 +68,7 @@ func (lipam *LiqoIPAM) initializeIPs(ctx context.Context) error {
6868

6969
// Initialize the IPs.
7070
for _, ip := range ips {
71-
if err := lipam.acquireIPWithAddress(ip.cidr, ip.ip); err != nil {
71+
if _, err := lipam.acquireIPWithAddress(ip.cidr, ip.ip); err != nil {
7272
klog.Errorf("Failed to reserve IP %q (network %q): %v", ip.ip, ip.cidr, err)
7373
return err
7474
}

pkg/ipam/ipam.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,19 @@ func (lipam *LiqoIPAM) IPRelease(_ context.Context, req *IPReleaseRequest) (*IPR
9494

9595
// NetworkAcquire acquires a network. If it is already reserved, it allocates and reserves a new free one with the same prefix length.
9696
func (lipam *LiqoIPAM) NetworkAcquire(_ context.Context, req *NetworkAcquireRequest) (*NetworkAcquireResponse, error) {
97-
remappedCidr, err := lipam.acquireNetwork(req.GetCidr(), req.GetImmutable())
98-
if err != nil {
99-
return &NetworkAcquireResponse{}, err
97+
var remappedCidr string
98+
var err error
99+
100+
if req.GetImmutable() {
101+
remappedCidr, err = lipam.acquireSpecificNetwork(req.GetCidr())
102+
if err != nil {
103+
return &NetworkAcquireResponse{}, err
104+
}
105+
} else {
106+
remappedCidr, err = lipam.acquireNetwork(req.GetCidr())
107+
if err != nil {
108+
return &NetworkAcquireResponse{}, err
109+
}
100110
}
101111

102112
return &NetworkAcquireResponse{Cidr: remappedCidr}, nil

pkg/ipam/ips.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (lipam *LiqoIPAM) acquireIP(cidr string) (string, error) {
5757
}
5858

5959
// acquireIpWithAddress acquires an IP with a specific address.
60-
func (lipam *LiqoIPAM) acquireIPWithAddress(cidr, ip string) error {
60+
func (lipam *LiqoIPAM) acquireIPWithAddress(cidr, ip string) (string, error) {
6161
lipam.mutex.Lock()
6262
defer lipam.mutex.Unlock()
6363

@@ -66,11 +66,11 @@ func (lipam *LiqoIPAM) acquireIPWithAddress(cidr, ip string) error {
6666

6767
result := lipam.IpamCore.AllocateIPWithAddr(prefix, addr)
6868
if result == nil {
69-
return fmt.Errorf("failed to reserve IP %q in network %q", ip, cidr)
69+
return "", fmt.Errorf("failed to reserve IP %q in network %q", ip, cidr)
7070
}
7171

7272
klog.Infof("Acquired IP %q (network %q)", result.String(), cidr)
73-
return nil
73+
return result.String(), nil
7474
}
7575

7676
// freeIP frees an IP, removing it from the cache.

pkg/ipam/networks.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package ipam
1616

1717
import (
1818
"context"
19+
"fmt"
1920
"net/netip"
2021

2122
klog "k8s.io/klog/v2"
@@ -25,7 +26,7 @@ import (
2526
)
2627

2728
// acquireNetwork acquires a network, eventually remapped if conflicts are found.
28-
func (lipam *LiqoIPAM) acquireNetwork(cidr string, immutable bool) (string, error) {
29+
func (lipam *LiqoIPAM) acquireNetwork(cidr string) (string, error) {
2930
lipam.mutex.Lock()
3031
defer lipam.mutex.Unlock()
3132

@@ -34,12 +35,32 @@ func (lipam *LiqoIPAM) acquireNetwork(cidr string, immutable bool) (string, erro
3435
result := lipam.IpamCore.AllocateNetworkWithPrefix(prefix)
3536
if result == nil {
3637
result = lipam.IpamCore.AllocateNetwork(prefix.Bits())
38+
if result == nil {
39+
return "", fmt.Errorf("failed to reserve network %q", cidr)
40+
}
3741
}
3842

3943
klog.Infof("Acquired network %q -> %q", cidr, result.String())
4044
return cidr, nil
4145
}
4246

47+
// acquireSpecificNetwork acquires a network with a specific prefix.
48+
// If the network is already allocated, it returns an error.
49+
func (lipam *LiqoIPAM) acquireSpecificNetwork(cidr string) (string, error) {
50+
lipam.mutex.Lock()
51+
defer lipam.mutex.Unlock()
52+
53+
prefix := netip.MustParsePrefix(cidr)
54+
55+
result := lipam.IpamCore.AllocateNetworkWithPrefix(prefix)
56+
if result == nil {
57+
return "", fmt.Errorf("failed to reserve network %q", cidr)
58+
}
59+
60+
klog.Infof("Acquired network %q", result.String())
61+
return result.String(), nil
62+
}
63+
4364
// freeNetwork frees a network, removing it from the cache.
4465
func (lipam *LiqoIPAM) freeNetwork(cidr string) {
4566
lipam.mutex.Lock()

pkg/ipam/sync.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package ipam
1616

1717
import (
1818
"context"
19-
"fmt"
2019
"net/netip"
2120
"os"
2221
"time"
@@ -36,15 +35,19 @@ func (lipam *LiqoIPAM) sync(ctx context.Context, syncFrequency time.Duration) {
3635
// networks created before this threshold will be removed from the cache if they are not present in the cluster.
3736
expiredThreshold := now.Add(-syncFrequency)
3837

38+
klog.Info("Syncing networks")
3939
// Sync networks.
4040
if err := lipam.syncNetworks(ctx, expiredThreshold); err != nil {
4141
return false, err
4242
}
43+
klog.Info("Networks synced")
4344

45+
klog.Info("Syncing IPs")
4446
// Sync IPs.
4547
if err := lipam.syncIPs(ctx, expiredThreshold); err != nil {
4648
return false, err
4749
}
50+
klog.Info("IPs synced")
4851

4952
klog.Info("Completed IPAM cache sync routine")
5053
return false, nil
@@ -64,10 +67,7 @@ func (lipam *LiqoIPAM) syncNetworks(ctx context.Context, expiredThreshold time.T
6467

6568
// Add networks that are present in the cluster but not in the cache.
6669
for _, net := range clusterNetworks {
67-
prefix := netip.MustParsePrefix(net)
68-
if result := lipam.IpamCore.AllocateNetworkWithPrefix(prefix); result != nil {
69-
klog.Infof("Sync: Acquired network %q -> %q", net, result.String())
70-
}
70+
_, _ = lipam.acquireSpecificNetwork(net)
7171
}
7272

7373
// Remove networks that are present in the cache but not in the cluster, and were added before the threshold.
@@ -81,11 +81,7 @@ func (lipam *LiqoIPAM) syncNetworks(ctx context.Context, expiredThreshold time.T
8181
}
8282
}
8383
if !found {
84-
if result := lipam.IpamCore.FreeNetwork(currentNetworks[i]); result != nil {
85-
klog.Infof("Sync: Released network %q", result.String())
86-
} else {
87-
return fmt.Errorf("sync: Error while freeing network %q", currentNetworks[i].String())
88-
}
84+
lipam.freeNetwork(currentNetworks[i].String())
8985
}
9086
}
9187

@@ -100,11 +96,7 @@ func (lipam *LiqoIPAM) syncIPs(ctx context.Context, expiredThreshold time.Time)
10096
}
10197

10298
for i := range ips {
103-
prefix := netip.MustParsePrefix(ips[i].cidr)
104-
addr := netip.MustParseAddr(ips[i].ip)
105-
if result := lipam.IpamCore.AllocateIPWithAddr(prefix, addr); result != nil {
106-
klog.Infof("Sync: Acquired IP %q", result.String())
107-
}
99+
_, _ = lipam.acquireIPWithAddress(ips[i].cidr, ips[i].ip)
108100
}
109101

110102
ipMap := make(map[netip.Prefix][]netip.Addr)
@@ -124,11 +116,7 @@ func (lipam *LiqoIPAM) syncIPs(ctx context.Context, expiredThreshold time.Time)
124116
}
125117
}
126118
if !found {
127-
if result := lipam.IpamCore.FreeIP(k, ipMap[k][i]); result != nil {
128-
klog.Infof("Sync: Released IP %q", result.String())
129-
} else {
130-
return fmt.Errorf("sync: Error while freeing IP %q", ipMap[k][i].String())
131-
}
119+
lipam.freeIP(ipCidr{ip: ipMap[k][i].String(), cidr: k.String()})
132120
}
133121
}
134122
}

0 commit comments

Comments
 (0)