Skip to content

Commit 84bdc0e

Browse files
committed
feat: implemented frontend ipam client functions
1 parent 5d2fc2e commit 84bdc0e

File tree

3 files changed

+119
-18
lines changed

3 files changed

+119
-18
lines changed

pkg/ipam/ipam.go

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,75 @@ func New(ctx context.Context, cl client.Client, opts *ServerOptions) (*LiqoIPAM,
7272
}
7373

7474
// IPAcquire acquires a free IP from a given CIDR.
75-
func (lipam *LiqoIPAM) IPAcquire(_ context.Context, _ *IPAcquireRequest) (*IPAcquireResponse, error) {
76-
panic("implement me")
75+
func (lipam *LiqoIPAM) IPAcquire(_ context.Context, req *IPAcquireRequest) (*IPAcquireResponse, error) {
76+
remappedIP, err := lipam.acquireIP(req.GetCidr())
77+
if err != nil {
78+
return &IPAcquireResponse{
79+
Result: &ResponseResult{
80+
Success: false,
81+
Error: err.Error(),
82+
},
83+
}, err
84+
}
85+
86+
return &IPAcquireResponse{
87+
Ip: remappedIP,
88+
Result: &ResponseResult{
89+
Success: true,
90+
},
91+
}, nil
7792
}
7893

7994
// IPRelease releases an IP from a given CIDR.
80-
func (lipam *LiqoIPAM) IPRelease(_ context.Context, _ *IPReleaseRequest) (*IPReleaseResponse, error) {
81-
panic("implement me")
95+
func (lipam *LiqoIPAM) IPRelease(_ context.Context, req *IPReleaseRequest) (*IPReleaseResponse, error) {
96+
lipam.freeIP(ipCidr{ip: req.GetIp(), cidr: req.GetCidr()})
97+
98+
return &IPReleaseResponse{
99+
Result: &ResponseResult{
100+
Success: true,
101+
},
102+
}, nil
82103
}
83104

84105
// NetworkAcquire acquires a network. If it is already reserved, it allocates and reserves a new free one with the same prefix length.
85-
func (lipam *LiqoIPAM) NetworkAcquire(_ context.Context, _ *NetworkAcquireRequest) (*NetworkAcquireResponse, error) {
86-
panic("implement me")
106+
func (lipam *LiqoIPAM) NetworkAcquire(_ context.Context, req *NetworkAcquireRequest) (*NetworkAcquireResponse, error) {
107+
remappedCidr, err := lipam.acquireNetwork(req.GetCidr(), req.GetImmutable())
108+
if err != nil {
109+
return &NetworkAcquireResponse{
110+
Result: &ResponseResult{
111+
Success: false,
112+
Error: err.Error(),
113+
},
114+
}, err
115+
}
116+
117+
return &NetworkAcquireResponse{
118+
Cidr: remappedCidr,
119+
Result: &ResponseResult{
120+
Success: true,
121+
},
122+
}, nil
87123
}
88124

89125
// NetworkRelease releases a network.
90-
func (lipam *LiqoIPAM) NetworkRelease(_ context.Context, _ *NetworkReleaseRequest) (*NetworkReleaseResponse, error) {
91-
panic("implement me")
126+
func (lipam *LiqoIPAM) NetworkRelease(_ context.Context, req *NetworkReleaseRequest) (*NetworkReleaseResponse, error) {
127+
lipam.freeNetwork(req.GetCidr())
128+
129+
return &NetworkReleaseResponse{
130+
Result: &ResponseResult{
131+
Success: true,
132+
},
133+
}, nil
92134
}
93135

94136
// NetworkIsAvailable checks if a network is available.
95-
func (lipam *LiqoIPAM) NetworkIsAvailable(_ context.Context, _ *NetworkAvailableRequest) (*NetworkAvailableResponse, error) {
96-
panic("implement me")
137+
func (lipam *LiqoIPAM) NetworkIsAvailable(_ context.Context, req *NetworkAvailableRequest) (*NetworkAvailableResponse, error) {
138+
available := lipam.isNetworkAvailable(req.GetCidr())
139+
140+
return &NetworkAvailableResponse{
141+
Available: available,
142+
Result: &ResponseResult{
143+
Success: true,
144+
},
145+
}, nil
97146
}

pkg/ipam/ips.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,38 @@ func (lipam *LiqoIPAM) reserveIP(ip ipCidr) error {
4343
lipam.mutex.Lock()
4444
defer lipam.mutex.Unlock()
4545

46-
ipI := ipInfo{
46+
if lipam.cacheIPs == nil {
47+
lipam.cacheIPs = make(map[string]ipInfo)
48+
}
49+
lipam.cacheIPs[ip.String()] = ipInfo{
4750
ipCidr: ip,
4851
creationTimestamp: time.Now(),
4952
}
53+
54+
klog.Infof("Reserved IP %q (network %q)", ip.ip, ip.cidr)
55+
return nil
56+
}
57+
58+
// acquireIP acquires an IP, eventually remapped if conflicts are found.
59+
func (lipam *LiqoIPAM) acquireIP(cidr string) (string, error) {
60+
lipam.mutex.Lock()
61+
defer lipam.mutex.Unlock()
62+
63+
// TODO: implement real IP acquire logic
5064
if lipam.cacheIPs == nil {
5165
lipam.cacheIPs = make(map[string]ipInfo)
5266
}
53-
lipam.cacheIPs[ip.String()] = ipI
67+
ip := ipCidr{
68+
ip: "",
69+
cidr: cidr,
70+
}
71+
lipam.cacheIPs[ip.String()] = ipInfo{
72+
ipCidr: ip,
73+
creationTimestamp: time.Now(),
74+
}
5475

55-
klog.Infof("Reserved IP %q (network %q)", ip.ip, ip.cidr)
56-
return nil
76+
klog.Infof("Acquired IP %q (network %q)", ip.ip, ip.cidr)
77+
return ip.ip, nil
5778
}
5879

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

pkg/ipam/networks.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,35 @@ func (lipam *LiqoIPAM) reserveNetwork(cidr string) error {
3434
lipam.mutex.Lock()
3535
defer lipam.mutex.Unlock()
3636

37-
nwI := networkInfo{
37+
if lipam.cacheNetworks == nil {
38+
lipam.cacheNetworks = make(map[string]networkInfo)
39+
}
40+
lipam.cacheNetworks[cidr] = networkInfo{
3841
cidr: cidr,
3942
creationTimestamp: time.Now(),
4043
}
44+
45+
klog.Infof("Reserved network %q", cidr)
46+
return nil
47+
}
48+
49+
// acquireNetwork acquires a network, eventually remapped if conflicts are found.
50+
func (lipam *LiqoIPAM) acquireNetwork(cidr string, immutable bool) (string, error) {
51+
lipam.mutex.Lock()
52+
defer lipam.mutex.Unlock()
53+
54+
// TODO: implement real network acquire logic
55+
_ = immutable
4156
if lipam.cacheNetworks == nil {
4257
lipam.cacheNetworks = make(map[string]networkInfo)
4358
}
44-
lipam.cacheNetworks[cidr] = nwI
59+
lipam.cacheNetworks[cidr] = networkInfo{
60+
cidr: cidr,
61+
creationTimestamp: time.Now(),
62+
}
4563

46-
klog.Infof("Reserved network %q", cidr)
47-
return nil
64+
klog.Infof("Acquired network %q", cidr)
65+
return cidr, nil
4866
}
4967

5068
// freeNetwork frees a network, removing it from the cache.
@@ -56,6 +74,19 @@ func (lipam *LiqoIPAM) freeNetwork(cidr string) {
5674
klog.Infof("Freed network %q", cidr)
5775
}
5876

77+
// isNetworkAvailable checks if a network is available.
78+
func (lipam *LiqoIPAM) isNetworkAvailable(cidr string) bool {
79+
lipam.mutex.Lock()
80+
defer lipam.mutex.Unlock()
81+
82+
if lipam.cacheNetworks == nil {
83+
return true
84+
}
85+
_, ok := lipam.cacheNetworks[cidr]
86+
87+
return ok
88+
}
89+
5990
func listNetworksOnCluster(ctx context.Context, cl client.Client) ([]string, error) {
6091
var nets []string
6192
var networks ipamv1alpha1.NetworkList

0 commit comments

Comments
 (0)