Skip to content

Commit b66690d

Browse files
committed
fixup! feature: reserve n IPs on Networks
1 parent 60930fb commit b66690d

File tree

5 files changed

+47
-29
lines changed

5 files changed

+47
-29
lines changed

pkg/ipam/initialize.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (lipam *LiqoIPAM) initializeNetworks(ctx context.Context) error {
4545

4646
// Initialize the networks.
4747
for _, net := range nets {
48-
if err := lipam.reserveNetwork(net); err != nil {
48+
if err := lipam.reserveNetwork(&net); err != nil {
4949
klog.Errorf("Failed to reserve network %q: %v", net, err)
5050
return err
5151
}

pkg/ipam/ipam.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (lipam *LiqoIPAM) IPRelease(_ context.Context, req *IPReleaseRequest) (*IPR
9090

9191
// NetworkAcquire acquires a network. If it is already reserved, it allocates and reserves a new free one with the same prefix length.
9292
func (lipam *LiqoIPAM) NetworkAcquire(_ context.Context, req *NetworkAcquireRequest) (*NetworkAcquireResponse, error) {
93-
remappedCidr, err := lipam.acquireNetwork(req.GetCidr(), req.GetImmutable(), uint(req.GetPreAllocated()))
93+
remappedCidr, err := lipam.acquireNetwork(req.GetCidr(), uint(req.GetPreAllocated()), req.GetImmutable())
9494
if err != nil {
9595
return &NetworkAcquireResponse{}, err
9696
}
@@ -100,14 +100,14 @@ func (lipam *LiqoIPAM) NetworkAcquire(_ context.Context, req *NetworkAcquireRequ
100100

101101
// NetworkRelease releases a network.
102102
func (lipam *LiqoIPAM) NetworkRelease(_ context.Context, req *NetworkReleaseRequest) (*NetworkReleaseResponse, error) {
103-
lipam.freeNetwork(req.GetCidr())
103+
lipam.freeNetwork(&network{cidr: req.GetCidr()})
104104

105105
return &NetworkReleaseResponse{}, nil
106106
}
107107

108108
// NetworkIsAvailable checks if a network is available.
109109
func (lipam *LiqoIPAM) NetworkIsAvailable(_ context.Context, req *NetworkAvailableRequest) (*NetworkAvailableResponse, error) {
110-
available := lipam.isNetworkAvailable(req.GetCidr())
110+
available := lipam.isNetworkAvailable(&network{cidr: req.GetCidr()})
111111

112112
return &NetworkAvailableResponse{Available: available}, nil
113113
}

pkg/ipam/networks.go

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,75 +25,86 @@ import (
2525
)
2626

2727
type networkInfo struct {
28-
cidr string
28+
network
2929
creationTimestamp time.Time
3030
}
3131

32+
type network struct {
33+
cidr string
34+
preAllocated uint
35+
}
36+
37+
func (n network) String() string {
38+
return n.cidr
39+
}
40+
3241
// reserveNetwork reserves a network, saving it in the cache.
33-
func (lipam *LiqoIPAM) reserveNetwork(cidr string, preAllocated uint) error {
42+
func (lipam *LiqoIPAM) reserveNetwork(nw *network) error {
3443
lipam.mutex.Lock()
3544
defer lipam.mutex.Unlock()
3645

3746
// TODO: implement real network reserve logic
38-
_ = preAllocated
3947
if lipam.cacheNetworks == nil {
4048
lipam.cacheNetworks = make(map[string]networkInfo)
4149
}
42-
lipam.cacheNetworks[cidr] = networkInfo{
43-
cidr: cidr,
50+
lipam.cacheNetworks[nw.String()] = networkInfo{
51+
network: *nw,
4452
creationTimestamp: time.Now(),
4553
}
4654

47-
klog.Infof("Reserved network %q", cidr)
55+
klog.Infof("Reserved network %q", nw)
4856
return nil
4957
}
5058

5159
// acquireNetwork acquires a network, eventually remapped if conflicts are found.
52-
func (lipam *LiqoIPAM) acquireNetwork(cidr string, immutable bool, preAllocated uint) (string, error) {
60+
func (lipam *LiqoIPAM) acquireNetwork(cidr string, preAllocated uint, immutable bool) (string, error) {
5361
lipam.mutex.Lock()
5462
defer lipam.mutex.Unlock()
5563

5664
// TODO: implement real network acquire logic
5765
_ = immutable
58-
_ = preAllocated
5966
if lipam.cacheNetworks == nil {
6067
lipam.cacheNetworks = make(map[string]networkInfo)
6168
}
62-
lipam.cacheNetworks[cidr] = networkInfo{
63-
cidr: cidr,
69+
nw := network{
70+
cidr: cidr,
71+
preAllocated: preAllocated,
72+
}
73+
lipam.cacheNetworks[nw.String()] = networkInfo{
74+
network: nw,
6475
creationTimestamp: time.Now(),
6576
}
6677

67-
klog.Infof("Acquired network %q", cidr)
68-
return cidr, nil
78+
klog.Infof("Acquired network %q", nw)
79+
return nw.cidr, nil
6980
}
7081

7182
// freeNetwork frees a network, removing it from the cache.
72-
func (lipam *LiqoIPAM) freeNetwork(cidr string) {
83+
func (lipam *LiqoIPAM) freeNetwork(nw *network) {
7384
lipam.mutex.Lock()
7485
defer lipam.mutex.Unlock()
7586

7687
// TODO: implement real network free logic
77-
delete(lipam.cacheNetworks, cidr)
78-
klog.Infof("Freed network %q", cidr)
88+
delete(lipam.cacheNetworks, nw.String())
89+
klog.Infof("Freed network %q", nw.cidr)
7990
}
8091

8192
// isNetworkAvailable checks if a network is available.
82-
func (lipam *LiqoIPAM) isNetworkAvailable(cidr string) bool {
93+
func (lipam *LiqoIPAM) isNetworkAvailable(nw *network) bool {
8394
lipam.mutex.Lock()
8495
defer lipam.mutex.Unlock()
8596

8697
// TODO: implement real network availability check logic
8798
if lipam.cacheNetworks == nil {
8899
return true
89100
}
90-
_, ok := lipam.cacheNetworks[cidr]
101+
_, ok := lipam.cacheNetworks[nw.String()]
91102

92103
return ok
93104
}
94105

95-
func listNetworksOnCluster(ctx context.Context, cl client.Client) ([]string, error) {
96-
var nets []string
106+
func listNetworksOnCluster(ctx context.Context, cl client.Client) ([]network, error) {
107+
var nets []network
97108
var networks ipamv1alpha1.NetworkList
98109
if err := cl.List(ctx, &networks); err != nil {
99110
return nil, err
@@ -108,7 +119,10 @@ func listNetworksOnCluster(ctx context.Context, cl client.Client) ([]string, err
108119
continue
109120
}
110121

111-
nets = append(nets, cidr)
122+
nets = append(nets, network{
123+
cidr: cidr,
124+
preAllocated: net.Spec.PreAllocated,
125+
})
112126
}
113127

114128
return nets, nil

pkg/ipam/sync.go

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

2222
"k8s.io/apimachinery/pkg/util/wait"
2323
klog "k8s.io/klog/v2"
24+
"k8s.io/utils/ptr"
2425
)
2526

2627
// +kubebuilder:rbac:groups=ipam.liqo.io,resources=ips,verbs=get;list;watch
@@ -64,19 +65,20 @@ func (lipam *LiqoIPAM) syncNetworks(ctx context.Context, expiredThreshold time.T
6465
setClusterNetworks := make(map[string]struct{})
6566

6667
// Add networks that are present in the cluster but not in the cache.
67-
for _, net := range clusterNetworks {
68-
if _, inCache := lipam.cacheNetworks[net]; !inCache {
68+
for i := range clusterNetworks {
69+
net := &clusterNetworks[i]
70+
if _, inCache := lipam.cacheNetworks[net.String()]; !inCache {
6971
if err := lipam.reserveNetwork(net); err != nil {
7072
return err
7173
}
7274
}
73-
setClusterNetworks[net] = struct{}{} // add network to the set
75+
setClusterNetworks[net.String()] = struct{}{} // add network to the set
7476
}
7577

7678
// Remove networks that are present in the cache but not in the cluster, and were added before the threshold.
7779
for key := range lipam.cacheNetworks {
7880
if _, inCluster := setClusterNetworks[key]; !inCluster && lipam.cacheNetworks[key].creationTimestamp.Before(expiredThreshold) {
79-
lipam.freeNetwork(lipam.cacheNetworks[key].cidr)
81+
lipam.freeNetwork(ptr.To(lipam.cacheNetworks[key].network))
8082
}
8183
}
8284

pkg/ipam/sync_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ var _ = Describe("Sync routine tests", func() {
4545

4646
addNetowrkToCache = func(ipamServer *LiqoIPAM, cidr string, creationTimestamp time.Time) {
4747
ipamServer.cacheNetworks[cidr] = networkInfo{
48-
cidr: cidr,
48+
network: network{
49+
cidr: cidr,
50+
},
4951
creationTimestamp: creationTimestamp,
5052
}
5153
}

0 commit comments

Comments
 (0)