Skip to content

Commit

Permalink
fixup! fixup! feat: ipam sync routine
Browse files Browse the repository at this point in the history
  • Loading branch information
fra98 committed Nov 6, 2024
1 parent a29431a commit 0628f19
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 31 deletions.
12 changes: 2 additions & 10 deletions pkg/ipam/ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ type ipInfo struct {
creationTimestamp time.Time
}

func (i *ipInfo) String() string {
return i.ipCidr.String()
}

type ipCidr struct {
ip string
cidr string
Expand All @@ -54,18 +50,14 @@ func (lipam *LiqoIPAM) reserveIP(ip ipCidr) error {
if lipam.cacheIPs == nil {
lipam.cacheIPs = make(map[string]ipInfo)
}
lipam.cacheIPs[ipI.String()] = ipI
lipam.cacheIPs[ip.String()] = ipI

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

// freeIP frees an IP, removing it from the cache.
func (lipam *LiqoIPAM) freeIP(ip *ipInfo) {
if ip == nil {
return
}

func (lipam *LiqoIPAM) freeIP(ip ipCidr) {
lipam.mutex.Lock()
defer lipam.mutex.Unlock()

Expand Down
16 changes: 4 additions & 12 deletions pkg/ipam/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ type networkInfo struct {
creationTimestamp time.Time
}

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()
Expand All @@ -46,23 +42,19 @@ func (lipam *LiqoIPAM) reserveNetwork(cidr string) error {
if lipam.cacheNetworks == nil {
lipam.cacheNetworks = make(map[string]networkInfo)
}
lipam.cacheNetworks[nwI.String()] = nwI
lipam.cacheNetworks[cidr] = nwI

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

// freeNetwork frees a network, removing it from the cache.
func (lipam *LiqoIPAM) freeNetwork(nw *networkInfo) {
if nw == nil {
return
}

func (lipam *LiqoIPAM) freeNetwork(cidr string) {
lipam.mutex.Lock()
defer lipam.mutex.Unlock()

delete(lipam.cacheNetworks, nw.String())
klog.Infof("Freed network %q", nw.cidr)
delete(lipam.cacheNetworks, cidr)
klog.Infof("Freed network %q", cidr)
}

func listNetworksOnCluster(ctx context.Context, cl client.Client) ([]string, error) {
Expand Down
15 changes: 6 additions & 9 deletions pkg/ipam/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"k8s.io/apimachinery/pkg/util/wait"
klog "k8s.io/klog/v2"
"k8s.io/utils/ptr"
)

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

// Create a set for faster lookup.
nwSet := make(map[string]struct{})
for i := range nets {
nwI := networkInfo{cidr: nets[i]}
nwSet[nwI.String()] = struct{}{}
for _, net := range nets {
nwSet[net] = struct{}{}
}

// 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) {
lipam.freeNetwork(ptr.To(lipam.cacheNetworks[key]))
lipam.freeNetwork(lipam.cacheNetworks[key].cidr)
}
}

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

// Create a set for faster lookup.
ipSet := make(map[string]struct{})
for i := range ips {
ipI := ipInfo{ipCidr: ips[i]}
ipSet[ipI.String()] = struct{}{}
for _, ip := range ips {
ipSet[ip.String()] = struct{}{}
}

// 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) {
lipam.freeIP(ptr.To(lipam.cacheIPs[key]))
lipam.freeIP(lipam.cacheIPs[key].ipCidr)
}
}

Expand Down

0 comments on commit 0628f19

Please sign in to comment.