Skip to content

Commit 0628f19

Browse files
committed
fixup! fixup! feat: ipam sync routine
1 parent a29431a commit 0628f19

File tree

3 files changed

+12
-31
lines changed

3 files changed

+12
-31
lines changed

pkg/ipam/ips.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ type ipInfo struct {
2929
creationTimestamp time.Time
3030
}
3131

32-
func (i *ipInfo) String() string {
33-
return i.ipCidr.String()
34-
}
35-
3632
type ipCidr struct {
3733
ip string
3834
cidr string
@@ -54,18 +50,14 @@ func (lipam *LiqoIPAM) reserveIP(ip ipCidr) error {
5450
if lipam.cacheIPs == nil {
5551
lipam.cacheIPs = make(map[string]ipInfo)
5652
}
57-
lipam.cacheIPs[ipI.String()] = ipI
53+
lipam.cacheIPs[ip.String()] = ipI
5854

5955
klog.Infof("Reserved IP %q (network %q)", ip.ip, ip.cidr)
6056
return nil
6157
}
6258

6359
// freeIP frees an IP, removing it from the cache.
64-
func (lipam *LiqoIPAM) freeIP(ip *ipInfo) {
65-
if ip == nil {
66-
return
67-
}
68-
60+
func (lipam *LiqoIPAM) freeIP(ip ipCidr) {
6961
lipam.mutex.Lock()
7062
defer lipam.mutex.Unlock()
7163

pkg/ipam/networks.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ type networkInfo struct {
3030
creationTimestamp time.Time
3131
}
3232

33-
func (c *networkInfo) String() string {
34-
return c.cidr
35-
}
36-
3733
// reserveNetwork reserves a network, saving it in the cache.
3834
func (lipam *LiqoIPAM) reserveNetwork(cidr string) error {
3935
lipam.mutex.Lock()
@@ -46,23 +42,19 @@ func (lipam *LiqoIPAM) reserveNetwork(cidr string) error {
4642
if lipam.cacheNetworks == nil {
4743
lipam.cacheNetworks = make(map[string]networkInfo)
4844
}
49-
lipam.cacheNetworks[nwI.String()] = nwI
45+
lipam.cacheNetworks[cidr] = nwI
5046

5147
klog.Infof("Reserved network %q", cidr)
5248
return nil
5349
}
5450

5551
// freeNetwork frees a network, removing it from the cache.
56-
func (lipam *LiqoIPAM) freeNetwork(nw *networkInfo) {
57-
if nw == nil {
58-
return
59-
}
60-
52+
func (lipam *LiqoIPAM) freeNetwork(cidr string) {
6153
lipam.mutex.Lock()
6254
defer lipam.mutex.Unlock()
6355

64-
delete(lipam.cacheNetworks, nw.String())
65-
klog.Infof("Freed network %q", nw.cidr)
56+
delete(lipam.cacheNetworks, cidr)
57+
klog.Infof("Freed network %q", cidr)
6658
}
6759

6860
func listNetworksOnCluster(ctx context.Context, cl client.Client) ([]string, error) {

pkg/ipam/sync.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"k8s.io/apimachinery/pkg/util/wait"
2323
klog "k8s.io/klog/v2"
24-
"k8s.io/utils/ptr"
2524
)
2625

2726
// +kubebuilder:rbac:groups=ipam.liqo.io,resources=ips,verbs=get;list;watch
@@ -61,15 +60,14 @@ func (lipam *LiqoIPAM) syncNetworks(ctx context.Context, expiredThreshold time.T
6160

6261
// Create a set for faster lookup.
6362
nwSet := make(map[string]struct{})
64-
for i := range nets {
65-
nwI := networkInfo{cidr: nets[i]}
66-
nwSet[nwI.String()] = struct{}{}
63+
for _, net := range nets {
64+
nwSet[net] = struct{}{}
6765
}
6866

6967
// Remove networks that are present in the cache but not in the cluster, and were added before the threshold.
7068
for key := range lipam.cacheNetworks {
7169
if _, ok := nwSet[key]; !ok && lipam.cacheNetworks[key].creationTimestamp.Before(expiredThreshold) {
72-
lipam.freeNetwork(ptr.To(lipam.cacheNetworks[key]))
70+
lipam.freeNetwork(lipam.cacheNetworks[key].cidr)
7371
}
7472
}
7573

@@ -85,15 +83,14 @@ func (lipam *LiqoIPAM) syncIPs(ctx context.Context, expiredThreshold time.Time)
8583

8684
// Create a set for faster lookup.
8785
ipSet := make(map[string]struct{})
88-
for i := range ips {
89-
ipI := ipInfo{ipCidr: ips[i]}
90-
ipSet[ipI.String()] = struct{}{}
86+
for _, ip := range ips {
87+
ipSet[ip.String()] = struct{}{}
9188
}
9289

9390
// Remove IPs that are present in the cache but not in the cluster, and were added before the threshold.
9491
for key := range lipam.cacheIPs {
9592
if _, ok := ipSet[key]; !ok && lipam.cacheIPs[key].creationTimestamp.Before(expiredThreshold) {
96-
lipam.freeIP(ptr.To(lipam.cacheIPs[key]))
93+
lipam.freeIP(lipam.cacheIPs[key].ipCidr)
9794
}
9895
}
9996

0 commit comments

Comments
 (0)