/
allocator.go
610 lines (507 loc) · 19.9 KB
/
allocator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// Copyright 2021 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package poolallocator
import (
"context"
"fmt"
"net"
"reflect"
"antrea.io/antrea/pkg/apis/crd/v1alpha2"
crdclientset "antrea.io/antrea/pkg/client/clientset/versioned"
informers "antrea.io/antrea/pkg/client/listers/crd/v1alpha2"
"antrea.io/antrea/pkg/ipam/ipallocator"
iputil "antrea.io/antrea/pkg/util/ip"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
)
// IPPoolAllocator is responsible for allocating IPs from IP set defined in IPPool CRD.
// The will update CRD usage accordingly.
// Pool Allocator assumes that pool with allocated IPs can not be deleted. Pool ranges can
// only be extended.
type IPPoolAllocator struct {
// Name of IP Pool custom resource
ipPoolName string
// crd client to update the pool
crdClient crdclientset.Interface
// pool lister for reading the pool
ipPoolLister informers.IPPoolLister
}
// NewIPPoolAllocator creates an IPPoolAllocator based on the provided IP pool.
func NewIPPoolAllocator(poolName string, client crdclientset.Interface, poolLister informers.IPPoolLister) (*IPPoolAllocator, error) {
// Validate the pool exists
// This has an extra roundtrip cost, however this would allow fallback to
// default IPAM driver if needed
_, err := poolLister.Get(poolName)
if err != nil {
return nil, err
}
allocator := &IPPoolAllocator{
ipPoolName: poolName,
crdClient: client,
ipPoolLister: poolLister,
}
return allocator, nil
}
func (a *IPPoolAllocator) getPool() (*v1alpha2.IPPool, error) {
pool, err := a.ipPoolLister.Get(a.ipPoolName)
return pool, err
}
// initAllocatorList reads IP Pool status and initializes a list of allocators based on
// IP Pool spec and state of allocation recorded in the status
func (a *IPPoolAllocator) initIPAllocators(ipPool *v1alpha2.IPPool) (ipallocator.MultiIPAllocator, error) {
var allocators ipallocator.MultiIPAllocator
// Initialize a list of IP allocators based on pool spec
for _, ipRange := range ipPool.Spec.IPRanges {
if len(ipRange.CIDR) > 0 {
// Reserve gateway address and broadcast address
reservedIPs := []net.IP{net.ParseIP(ipRange.SubnetInfo.Gateway)}
_, ipNet, err := net.ParseCIDR(ipRange.CIDR)
if err != nil {
return nil, err
}
size, bits := ipNet.Mask.Size()
if int32(size) == ipRange.SubnetInfo.PrefixLength && bits == 32 {
// Allocation CIDR covers entire subnet, thus we need
// to reserve broadcast IP as well for IPv4
reservedIPs = append(reservedIPs, iputil.GetLocalBroadcastIP(ipNet))
}
allocator, err := ipallocator.NewCIDRAllocator(ipNet, reservedIPs)
if err != nil {
return nil, err
}
allocators = append(allocators, allocator)
} else {
allocator, err := ipallocator.NewIPRangeAllocator(net.ParseIP(ipRange.Start), net.ParseIP(ipRange.End))
if err != nil {
return allocators, err
}
allocators = append(allocators, allocator)
}
}
// Mark allocated IPs from pool status as unavailable
for _, ip := range ipPool.Status.IPAddresses {
err := allocators.AllocateIP(net.ParseIP(ip.IPAddress))
if err != nil {
// TODO - fix state if possible
return allocators, fmt.Errorf("inconsistent state for IP Pool %s with IP %s", ipPool.Name, ip.IPAddress)
}
}
return allocators, nil
}
func (a *IPPoolAllocator) getPoolAndInitIPAllocators() (*v1alpha2.IPPool, ipallocator.MultiIPAllocator, error) {
ipPool, err := a.getPool()
if err != nil {
return nil, ipallocator.MultiIPAllocator{}, err
}
allocators, err := a.initIPAllocators(ipPool)
if err != nil {
return nil, ipallocator.MultiIPAllocator{}, err
}
return ipPool, allocators, nil
}
func (a *IPPoolAllocator) appendPoolUsage(ipPool *v1alpha2.IPPool, ip net.IP, state v1alpha2.IPAddressPhase, owner v1alpha2.IPAddressOwner) error {
newPool := ipPool.DeepCopy()
usageEntry := v1alpha2.IPAddressState{
IPAddress: ip.String(),
Phase: state,
Owner: owner,
}
newPool.Status.IPAddresses = append(newPool.Status.IPAddresses, usageEntry)
_, err := a.crdClient.CrdV1alpha2().IPPools().UpdateStatus(context.TODO(), newPool, metav1.UpdateOptions{})
if err != nil {
klog.Warningf("IP Pool %s update with status %+v failed: %+v", newPool.Name, newPool.Status, err)
return err
}
klog.InfoS("IP Pool update succeeded", "pool", newPool.Name, "allocation", newPool.Status)
return nil
}
// updateIPAddressState updates the status of the specified IP in the provided IPPool. It requires the IP is already in the IPAddresses list of the IPPool's status.
func (a *IPPoolAllocator) updateIPAddressState(ipPool *v1alpha2.IPPool, ip net.IP, state v1alpha2.IPAddressPhase, owner v1alpha2.IPAddressOwner) error {
newPool := ipPool.DeepCopy()
ipString := ip.String()
found := false
for i, ipAddress := range newPool.Status.IPAddresses {
if ipAddress.IPAddress == ipString {
newPool.Status.IPAddresses[i].Phase = state
newPool.Status.IPAddresses[i].Owner = owner
found = true
break
}
}
if !found {
return fmt.Errorf("ip %s usage not found in pool %s", ipString, newPool.Name)
}
_, err := a.crdClient.CrdV1alpha2().IPPools().UpdateStatus(context.TODO(), newPool, metav1.UpdateOptions{})
if err != nil {
klog.Warningf("IP Pool %s update with status %+v failed: %+v", newPool.Name, newPool.Status, err)
return err
}
klog.InfoS("IP Pool update succeeded", "pool", newPool.Name, "allocation", newPool.Status)
return nil
}
func (a *IPPoolAllocator) appendPoolUsageForStatefulSet(ipPool *v1alpha2.IPPool, ips []net.IP, namespace, name string) error {
newPool := ipPool.DeepCopy()
for i, ip := range ips {
owner := v1alpha2.IPAddressOwner{
StatefulSet: &v1alpha2.StatefulSetOwner{
Namespace: namespace,
Name: name,
Index: i,
},
}
usageEntry := v1alpha2.IPAddressState{
IPAddress: ip.String(),
Phase: v1alpha2.IPAddressPhasePreallocated,
Owner: owner,
}
newPool.Status.IPAddresses = append(newPool.Status.IPAddresses, usageEntry)
}
_, err := a.crdClient.CrdV1alpha2().IPPools().UpdateStatus(context.TODO(), newPool, metav1.UpdateOptions{})
if err != nil {
klog.Warningf("IP Pool %s update with status %+v failed: %+v", newPool.Name, newPool.Status, err)
return err
}
klog.V(2).InfoS("IP Pool update succeeded", "pool", newPool.Name, "allocation", newPool.Status)
return nil
}
// removeIPAddressState updates ipPool status to delete released IP allocation, and keeps preallocation information
func (a *IPPoolAllocator) removeIPAddressState(ipPool *v1alpha2.IPPool, ip net.IP) error {
ipString := ip.String()
newPool := ipPool.DeepCopy()
var newList []v1alpha2.IPAddressState
allocated := false
for i := range ipPool.Status.IPAddresses {
entry := ipPool.Status.IPAddresses[i]
if entry.IPAddress != ipString {
newList = append(newList, entry)
} else {
allocated = true
if entry.Owner.StatefulSet != nil {
entry = *entry.DeepCopy()
entry.Owner.Pod = nil
entry.Phase = v1alpha2.IPAddressPhaseReserved
newList = append(newList, entry)
}
}
}
if !allocated {
return fmt.Errorf("IP address %s was not allocated from IP pool %s", ip, ipPool.Name)
}
newPool.Status.IPAddresses = newList
_, err := a.crdClient.CrdV1alpha2().IPPools().UpdateStatus(context.TODO(), newPool, metav1.UpdateOptions{})
if err != nil {
klog.Warningf("IP Pool %s update failed: %+v", newPool.Name, err)
return err
}
klog.InfoS("IP Pool update succeeded", "pool", newPool.Name, "allocation", newPool.Status)
return nil
}
// AllocateIP allocates the specified IP. It returns error if the IP is not in the range or already
// allocated, or in case CRD failed to update its state.
// In case of success, IP pool CRD status is updated with allocated IP/state/resource/container.
// AllocateIP returns subnet details for the requested IP, as defined in IP pool spec.
func (a *IPPoolAllocator) AllocateIP(ip net.IP, state v1alpha2.IPAddressPhase, owner v1alpha2.IPAddressOwner) (v1alpha2.SubnetInfo, error) {
var subnetSpec v1alpha2.SubnetInfo
// Retry on CRD update conflict which is caused by multiple agents updating a pool at same time.
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
ipPool, allocators, err := a.getPoolAndInitIPAllocators()
if err != nil {
return err
}
index := len(allocators)
for i, allocator := range allocators {
if allocator.Has(ip) {
err := allocator.AllocateIP(ip)
if err != nil {
return err
}
index = i
break
}
}
if index == len(allocators) {
// Failed to find matching range
return fmt.Errorf("IP %v does not belong to IP pool %s", ip, a.ipPoolName)
}
subnetSpec = ipPool.Spec.IPRanges[index].SubnetInfo
err = a.appendPoolUsage(ipPool, ip, state, owner)
return err
})
if err != nil {
klog.Errorf("Failed to allocate IP address %s from pool %s: %+v", ip, a.ipPoolName, err)
}
return subnetSpec, err
}
// AllocateNext allocates the next available IP. It returns error if pool is exausted,
// or in case CRD failed to update its state.
// In case of success, IP pool CRD status is updated with allocated IP/state/resource/container.
// AllocateIP returns subnet details for the requested IP, as defined in IP pool spec.
func (a *IPPoolAllocator) AllocateNext(state v1alpha2.IPAddressPhase, owner v1alpha2.IPAddressOwner) (net.IP, v1alpha2.SubnetInfo, error) {
var subnetSpec v1alpha2.SubnetInfo
var ip net.IP
// Same resource can not ask for allocation twice without release
// This needs to be verified even at the expense of another API call
exists, err := a.HasContainer(owner.Pod.ContainerID)
if err != nil {
return ip, subnetSpec, err
}
if exists {
return ip, subnetSpec, fmt.Errorf("container %s was already allocated an address from IP Pool %s", owner.Pod.ContainerID, a.ipPoolName)
}
// Retry on CRD update conflict which is caused by multiple agents updating a pool at same time.
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
ipPool, allocators, err := a.getPoolAndInitIPAllocators()
if err != nil {
return err
}
index := len(allocators)
for i, allocator := range allocators {
ip, err = allocator.AllocateNext()
if err == nil {
// successful allocation
index = i
break
}
}
if index == len(allocators) {
// Failed to find matching range
return fmt.Errorf("failed to allocate IP: Pool %s is exausted", a.ipPoolName)
}
subnetSpec = ipPool.Spec.IPRanges[index].SubnetInfo
return a.appendPoolUsage(ipPool, ip, state, owner)
})
if err != nil {
klog.ErrorS(err, "Failed to allocate from IPPool", "IPPool", a.ipPoolName)
}
return ip, subnetSpec, err
}
// AllocateReservedOrNext allocates the reserved IP if it exists, else allocates next available IP.
// It returns error if pool is exhausted, or in case it fails to update IPPool's state. In case of
// success, IP pool status is updated with allocated IP/state/resource/container.
// AllocateReservedOrNext returns subnet details for the requested IP, as defined in IP pool spec.
func (a *IPPoolAllocator) AllocateReservedOrNext(state v1alpha2.IPAddressPhase, owner v1alpha2.IPAddressOwner) (net.IP, v1alpha2.SubnetInfo, error) {
var subnetSpec v1alpha2.SubnetInfo
var ip net.IP
ip, err := a.getReservedIP(owner)
if err != nil {
return ip, subnetSpec, err
}
if ip == nil {
// ip is not reserved, allocate next available ip
return a.AllocateNext(state, owner)
}
// Same resource can not ask for allocation twice without release
// This needs to be verified even at the expense of another API call
exists, err := a.HasContainer(owner.Pod.ContainerID)
if err != nil {
return ip, subnetSpec, err
}
if exists {
return ip, subnetSpec, fmt.Errorf("container %s was already allocated an address from IP Pool %s", owner.Pod.ContainerID, a.ipPoolName)
}
// Retry on CRD update conflict which is caused by multiple agents updating a pool at same time.
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
ipPool, allocators, err := a.getPoolAndInitIPAllocators()
if err != nil {
return err
}
index := -1
for i, allocator := range allocators {
if allocator.Has(ip) {
index = i
break
}
}
if index == -1 {
// Failed to find matching range
return fmt.Errorf("IP %v does not belong to IP pool %s", ip, a.ipPoolName)
}
subnetSpec = ipPool.Spec.IPRanges[index].SubnetInfo
return a.updateIPAddressState(ipPool, ip, state, owner)
})
if err != nil {
klog.ErrorS(err, "Failed to allocate IP address", "ip", ip, "ipPool", a.ipPoolName)
}
return ip, subnetSpec, err
}
// AllocateStatefulSet pre-allocates continuous range of IPs for StatefulSet.
// This functionality is useful when StatefulSet does not have a dedicated IP Pool assigned.
// It returns error if such range is not available. In this case IPs for the StatefulSet will
// be allocated on the fly, and there is no guarantee for continuous IPs.
func (a *IPPoolAllocator) AllocateStatefulSet(namespace, name string, size int) error {
// Retry on CRD update conflict which is caused by multiple agents updating a pool at same time.
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
ipPool, allocators, err := a.getPoolAndInitIPAllocators()
if err != nil {
return err
}
ips, err := allocators.AllocateRange(size)
if err != nil {
return err
}
return a.appendPoolUsageForStatefulSet(ipPool, ips, namespace, name)
})
if err != nil {
klog.ErrorS(err, "Failed to allocate from IPPool", "IPPool", a.ipPoolName)
}
return err
}
// Release releases the provided IP. It returns error if the IP is not in the range or not allocated,
// or in case CRD failed to update its state.
// In case of success, IP pool CRD status is updated with released IP/state/resource.
func (a *IPPoolAllocator) Release(ip net.IP) error {
// Retry on CRD update conflict which is caused by multiple agents updating a pool at same time.
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
ipPool, allocators, err := a.getPoolAndInitIPAllocators()
if err != nil {
return err
}
err = allocators.Release(ip)
if err != nil {
// Failed to find matching range
return fmt.Errorf("IP %v does not belong to IP pool %s", ip, a.ipPoolName)
}
return a.removeIPAddressState(ipPool, ip)
})
if err != nil {
klog.ErrorS(err, "Failed to release IP address", "IPAddress", ip, "IPPool", a.ipPoolName)
}
return err
}
// ReleasePod releases the IP associated with specified Pod.
// It returns error if the pod is not present in state or in case CRD failed to update state.
// In case of success, IP pool CRD status is updated with released entry.
func (a *IPPoolAllocator) ReleasePod(namespace, podName string) error {
// Retry on CRD update conflict which is caused by multiple agents updating a pool at same time.
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
ipPool, err := a.getPool()
if err != nil {
return err
}
// Mark allocated IPs from pool status as unavailable
for _, ip := range ipPool.Status.IPAddresses {
if ip.Owner.Pod != nil && ip.Owner.Pod.Namespace == namespace && ip.Owner.Pod.Name == podName {
return a.removeIPAddressState(ipPool, net.ParseIP(ip.IPAddress))
}
}
klog.V(4).InfoS("IP Pool state:", "name", a.ipPoolName, "allocation", ipPool.Status.IPAddresses)
return fmt.Errorf("failed to find record of IP allocated to Pod:%s/%s in pool %s", namespace, podName, a.ipPoolName)
})
if err != nil {
klog.ErrorS(err, "Failed to release IP address", "Namespace", namespace, "Pod", podName, "IPPool", a.ipPoolName)
}
return err
}
// ReleaseStatefulSet releases all IPs associated with specified StatefulSet. It returns error
// in case CRD failed to update its state.
// In case of success, IP pool CRD status is updated with released entries.
func (a *IPPoolAllocator) ReleaseStatefulSet(namespace, name string) error {
// Retry on CRD update conflict which is caused by multiple agents updating a pool at same time.
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
ipPool, err := a.getPool()
if err != nil {
return err
}
var updatedAdresses []v1alpha2.IPAddressState
for _, ip := range ipPool.Status.IPAddresses {
if ip.Owner.StatefulSet == nil || ip.Owner.StatefulSet.Namespace != namespace || ip.Owner.StatefulSet.Name != name {
updatedAdresses = append(updatedAdresses, ip)
}
}
if len(ipPool.Status.IPAddresses) == len(updatedAdresses) {
// no change
klog.V(4).InfoS("No reserved IPs found", "pool", ipPool.Name, "Namespace", namespace, "StatefulSet", name)
return nil
}
newPool := ipPool.DeepCopy()
newPool.Status.IPAddresses = updatedAdresses
_, err = a.crdClient.CrdV1alpha2().IPPools().UpdateStatus(context.TODO(), newPool, metav1.UpdateOptions{})
if err != nil {
klog.Warningf("IP Pool %s update failed: %+v", newPool.Name, err)
return err
}
klog.V(2).InfoS("IP Pool update successful", "pool", newPool.Name, "allocation", newPool.Status)
return nil
})
if err != nil {
klog.ErrorS(err, "Failed to release IP addresses", "Namespace", namespace, "StatefulSet", name, "IPPool", a.ipPoolName)
}
return err
}
// ReleaseContainerIfPresent releases the IP associated with specified container ID if present in state.
// It returns error in case CRD failed to update its state, or if pool does not exist.
// In case of success, IP pool CRD status is updated with released entry.
func (a *IPPoolAllocator) ReleaseContainerIfPresent(containerID string) error {
// Retry on CRD update conflict which is caused by multiple agents updating a pool at same time.
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
ipPool, err := a.getPool()
if err != nil {
return err
}
// Mark allocated IPs from pool status as unavailable
for _, ip := range ipPool.Status.IPAddresses {
if ip.Owner.Pod != nil && ip.Owner.Pod.ContainerID == containerID {
return a.removeIPAddressState(ipPool, net.ParseIP(ip.IPAddress))
}
}
klog.V(4).InfoS("Failed to find allocation record in pool", "container", containerID, "pool", a.ipPoolName, "allocation", ipPool.Status.IPAddresses)
return nil
})
if err != nil {
klog.ErrorS(err, "Failed to release IP address", "Container", containerID, "IPPool", a.ipPoolName)
}
return err
}
// HasResource checks whether an IP was associated with specified pod. It returns error if the resource is crd fails to be retrieved.
func (a *IPPoolAllocator) HasPod(namespace, podName string) (bool, error) {
ipPool, err := a.getPool()
if err != nil {
return false, err
}
for _, ip := range ipPool.Status.IPAddresses {
if ip.Owner.Pod != nil && ip.Owner.Pod.Namespace == namespace && ip.Owner.Pod.Name == podName {
return true, nil
}
}
return false, nil
}
// HasContainer checks whether an IP was associated with specified container. It returns error if the resource crd fails to be retrieved.
func (a *IPPoolAllocator) HasContainer(containerID string) (bool, error) {
ipPool, err := a.getPool()
if err != nil {
return false, err
}
for _, ip := range ipPool.Status.IPAddresses {
if ip.Owner.Pod != nil && ip.Owner.Pod.ContainerID == containerID {
return true, nil
}
}
return false, nil
}
// getReservedIP checks whether an IP was reserved with specified owner. It returns error if the resource crd fails to be retrieved.
func (a *IPPoolAllocator) getReservedIP(reservedOwner v1alpha2.IPAddressOwner) (net.IP, error) {
ipPool, err := a.getPool()
if err != nil {
return nil, err
}
if reservedOwner.StatefulSet != nil {
for _, ip := range ipPool.Status.IPAddresses {
if reflect.DeepEqual(ip.Owner.StatefulSet, reservedOwner.StatefulSet) {
return net.ParseIP(ip.IPAddress), nil
}
}
}
return nil, nil
}