Skip to content

Commit

Permalink
fixup! feat: ipam sync routine
Browse files Browse the repository at this point in the history
  • Loading branch information
fra98 committed Nov 4, 2024
1 parent 1577c1e commit 0c37259
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
13 changes: 5 additions & 8 deletions pkg/ipam/ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,21 @@ func (i *ipCidr) String() string {
return i.ip + "-" + i.cidr
}

// reserveIP reserves an IP, saving it in the cache.
func (lipam *LiqoIPAM) reserveIP(ip, cidr string) error {
lipam.mutex.Lock()
defer lipam.mutex.Unlock()

if lipam.cacheIPs == nil {
lipam.cacheIPs = make(map[string]ipInfo)
}

// TODO: add correct logic.
ipI := ipInfo{
ipCidr: ipCidr{ip: ip, cidr: cidr},
creationTimestamp: time.Now(),
}

// Save IP in cache.
if lipam.cacheIPs == nil {
lipam.cacheIPs = make(map[string]ipInfo)
}
lipam.cacheIPs[ipI.String()] = ipI
klog.Infof("Reserved IP %s in network %s", ip, cidr)

klog.Infof("Reserved IP %s in network %s", ip, cidr)
return nil
}

Expand Down
13 changes: 5 additions & 8 deletions pkg/ipam/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,21 @@ func (c *networkInfo) String() string {
return c.cidr
}

// reserveNetwork reserves a network, saving it in the cache.
func (lipam *LiqoIPAM) reserveNetwork(cidr string) error {
lipam.mutex.Lock()
defer lipam.mutex.Unlock()

if lipam.cacheNetworks == nil {
lipam.cacheNetworks = make(map[string]networkInfo)
}

// TODO: add correct logic
nwI := networkInfo{
cidr: cidr,
creationTimestamp: time.Now(),
}

// Save network in cache.
if lipam.cacheNetworks == nil {
lipam.cacheNetworks = make(map[string]networkInfo)
}
lipam.cacheNetworks[nwI.String()] = nwI
klog.Infof("Reserved network %s", cidr)

klog.Infof("Reserved network %s", cidr)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/ipam/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (lipam *LiqoIPAM) syncNetworks(ctx context.Context, expiredThreshold time.T
nwSet[nwI.String()] = struct{}{}
}

// Remove networks that are not present in the cache and were added before the threshold.
// Remove networks that are present in the cache but not in the cluster, and were added before the threshold.
for key := range lipam.cacheNetworks {
if _, ok := nwSet[key]; !ok && lipam.cacheNetworks[key].creationTimestamp.Before(expiredThreshold) {
delete(lipam.cacheNetworks, key)
Expand All @@ -93,7 +93,7 @@ func (lipam *LiqoIPAM) syncIPs(ctx context.Context, expiredThreshold time.Time)
ipSet[ipI.String()] = struct{}{}
}

// Remove IPs that are not present in the cache and were added before the threshold.
// Remove IPs that are present in the cache but not in the cluster, and were added before the threshold.
for key := range lipam.cacheIPs {
if _, ok := ipSet[key]; !ok && lipam.cacheIPs[key].creationTimestamp.Before(expiredThreshold) {
delete(lipam.cacheIPs, key)
Expand Down

0 comments on commit 0c37259

Please sign in to comment.