-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathcloud_allocator.go
More file actions
581 lines (517 loc) · 21.1 KB
/
Copy pathcloud_allocator.go
File metadata and controls
581 lines (517 loc) · 21.1 KB
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
/*
Copyright 2016 The Kubernetes 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 ipam
import (
"context"
"fmt"
"net"
"strconv"
"strings"
"time"
"github.com/linode/linodego"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
informers "k8s.io/client-go/informers/core/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
nodeutil "k8s.io/component-helpers/node/util"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/controller/nodeipam/ipam/cidrset"
controllerutil "k8s.io/kubernetes/pkg/controller/util/node"
netutils "k8s.io/utils/net"
linode "github.com/linode/linode-cloud-controller-manager/cloud/linode/client"
)
type cloudAllocator struct {
client clientset.Interface
linodeClient linode.Client
// cluster cidr as passed in during controller creation for ipv4 addresses
clusterCIDR *net.IPNet
// for clusterCIDR we maintain what is used and what is not
cidrSet *cidrset.CidrSet
// nodeLister is able to list/get nodes and is populated by the shared informer passed to controller
nodeLister corelisters.NodeLister
// nodesSynced returns true if the node shared informer has been synced at least once.
nodesSynced cache.InformerSynced
broadcaster record.EventBroadcaster
recorder record.EventRecorder
// queues are where incoming work is placed to de-dup and to allow "easy"
// rate limited requeues on errors
queue workqueue.TypedRateLimitingInterface[any]
// nodeCIDRMaskSizeIPv6 is the mask size for the IPv6 CIDR assigned to nodes.
nodeCIDRMaskSizeIPv6 int
// disableIPv6NodeCIDRAllocation is true if we should not allocate IPv6 CIDRs for nodes.
disableIPv6NodeCIDRAllocation bool
}
const providerIDPrefix = "linode://"
var _ CIDRAllocator = &cloudAllocator{}
// NewLinodeCIDRAllocator returns a CIDRAllocator to allocate CIDRs for node
// Caller must ensure subNetMaskSize is not less than cluster CIDR mask size.
// Caller must always pass in a list of existing nodes so the new allocator.
// Caller must ensure that ClusterCIDR is semantically correct
// can initialize its CIDR map. NodeList is only nil in testing.
func NewLinodeCIDRAllocator(ctx context.Context, linodeClient linode.Client, client clientset.Interface, nodeInformer informers.NodeInformer, allocatorParams CIDRAllocatorParams, nodeList *v1.NodeList) (CIDRAllocator, error) {
logger := klog.FromContext(ctx)
if client == nil {
logger.Error(nil, "kubeClient is nil when starting CIDRAllocator")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
eventBroadcaster := record.NewBroadcaster(record.WithContext(ctx))
recorder := eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "cidrAllocator"})
// create a cidrSet for ipv4 cidr we operate on
cidrSet, err := cidrset.NewCIDRSet(allocatorParams.ClusterCIDRs[0], allocatorParams.NodeCIDRMaskSizes[0])
if err != nil {
return nil, err
}
ca := &cloudAllocator{
client: client,
linodeClient: linodeClient,
clusterCIDR: allocatorParams.ClusterCIDRs[0],
cidrSet: cidrSet,
nodeLister: nodeInformer.Lister(),
nodesSynced: nodeInformer.Informer().HasSynced,
broadcaster: eventBroadcaster,
recorder: recorder,
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultTypedControllerRateLimiter[any](), "cidrallocator_node"),
nodeCIDRMaskSizeIPv6: allocatorParams.NodeCIDRMaskSizes[1],
disableIPv6NodeCIDRAllocation: allocatorParams.DisableIPv6NodeCIDRAllocation,
}
if allocatorParams.ServiceCIDR != nil {
ca.filterOutServiceRange(logger, allocatorParams.ServiceCIDR)
} else {
logger.Info("No Service CIDR provided. Skipping filtering out service addresses")
}
if allocatorParams.SecondaryServiceCIDR != nil {
ca.filterOutServiceRange(logger, allocatorParams.SecondaryServiceCIDR)
} else {
logger.Info("No Secondary Service CIDR provided. Skipping filtering out secondary service addresses")
}
if nodeList != nil {
for _, node := range nodeList.Items {
if len(node.Spec.PodCIDRs) == 0 {
logger.V(4).Info("Node has no CIDR, ignoring", "node", klog.KObj(&node))
continue
}
logger.V(4).Info("Node has CIDR, occupying it in CIDR map", "node", klog.KObj(&node), "podCIDR", node.Spec.PodCIDR)
if err := ca.occupyCIDRs(ctx, &node); err != nil {
// This will happen if:
// 1. We find garbage in the podCIDRs field. Retrying is useless.
// 2. CIDR out of range: This means a node CIDR has changed.
// This error will keep crashing controller-manager.
return nil, err
}
}
}
if _, err := nodeInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err == nil {
ca.queue.Add(key)
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(newObj)
if err == nil {
ca.queue.Add(key)
}
},
DeleteFunc: func(obj interface{}) {
// The informer cache no longer has the object, and since Node doesn't have a finalizer,
// we don't see the Update with DeletionTimestamp != 0.
node, ok := obj.(*v1.Node)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", obj))
return
}
node, ok = tombstone.Obj.(*v1.Node)
if !ok {
utilruntime.HandleError(fmt.Errorf("unexpected object types: %v", obj))
return
}
}
if err := ca.ReleaseCIDR(logger, node); err != nil {
utilruntime.HandleError(fmt.Errorf("error while processing CIDR Release: %w", err))
}
},
}); err != nil {
logger.Error(err, "Failed to add event handler to node informer")
return nil, err
}
return ca, nil
}
func (c *cloudAllocator) Run(ctx context.Context) {
defer utilruntime.HandleCrash()
// Start event processing pipeline.
c.broadcaster.StartStructuredLogging(3)
logger := klog.FromContext(ctx)
logger.Info("Sending events to api server")
c.broadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: c.client.CoreV1().Events("")})
defer c.broadcaster.Shutdown()
defer c.queue.ShutDown()
logger.Info("Starting linode's cloud CIDR allocator")
defer logger.Info("Shutting down linode's cloud CIDR allocator")
if !cache.WaitForNamedCacheSync("cidrallocator", ctx.Done(), c.nodesSynced) {
return
}
for i := 0; i < cidrUpdateWorkers; i++ {
go wait.UntilWithContext(ctx, c.runWorker, time.Second)
}
<-ctx.Done()
}
// runWorker is a long-running function that will continually call the
// processNextWorkItem function in order to read and process a message on the
// queue.
func (c *cloudAllocator) runWorker(ctx context.Context) {
for c.processNextNodeWorkItem(ctx) {
}
}
// processNextWorkItem will read a single work item off the queue and
// attempt to process it, by calling the syncHandler.
func (c *cloudAllocator) processNextNodeWorkItem(ctx context.Context) bool {
obj, shutdown := c.queue.Get()
if shutdown {
return false
}
// We wrap this block in a func so we can defer r.queue.Done.
err := func(logger klog.Logger, obj interface{}) error {
// We call Done here so the workNodeQueue knows we have finished
// processing this item. We also must remember to call Forget if we
// do not want this work item being re-queued. For example, we do
// not call Forget if a transient error occurs, instead the item is
// put back on the queue and attempted again after a back-off
// period.
defer c.queue.Done(obj)
var key string
var ok bool
// We expect strings to come off the workNodeQueue. These are of the
// form namespace/name. We do this as the delayed nature of the
// workNodeQueue means the items in the informer cache may actually be
// more up to date that when the item was initially put onto the
// workNodeQueue.
if key, ok = obj.(string); !ok {
// As the item in the workNodeQueue is actually invalid, we call
// Forget here else we'd go into a loop of attempting to
// process a work item that is invalid.
c.queue.Forget(obj)
utilruntime.HandleError(fmt.Errorf("expected string in workNodeQueue but got %#v", obj))
return nil
}
// Run the syncHandler, passing it the namespace/name string of the
// Foo resource to be synced.
if err := c.syncNode(ctx, key); err != nil {
// Put the item back on the queue to handle any transient errors.
c.queue.AddRateLimited(key)
return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error())
}
// Finally, if no error occurs we Forget this item so it does not
// get queue again until another change happens.
c.queue.Forget(obj)
logger.V(4).Info("Successfully synced", "key", key)
return nil
}(klog.FromContext(ctx), obj)
if err != nil {
utilruntime.HandleError(err)
return true
}
return true
}
func (c *cloudAllocator) syncNode(ctx context.Context, key string) error {
logger := klog.FromContext(ctx)
startTime := time.Now()
defer func() {
logger.V(4).Info("Finished syncing Node request", "node", key, "elapsed", time.Since(startTime))
}()
node, err := c.nodeLister.Get(key)
if apierrors.IsNotFound(err) {
logger.V(3).Info("node has been deleted", "node", key)
// TODO: obtain the node object information to call ReleaseCIDR from here
// and retry if there is an error.
return nil
}
if err != nil {
return err
}
// Check the DeletionTimestamp to determine if object is under deletion.
if !node.DeletionTimestamp.IsZero() {
logger.V(3).Info("node is being deleted", "node", key)
return nil
}
return c.AllocateOrOccupyCIDR(ctx, node)
}
// marks node.PodCIDRs[...] as used in allocator's tracked cidrSet
func (c *cloudAllocator) occupyCIDRs(ctx context.Context, node *v1.Node) error {
if len(node.Spec.PodCIDRs) == 0 {
return nil
}
logger := klog.FromContext(ctx)
for idx, cidr := range node.Spec.PodCIDRs {
_, podCIDR, err := netutils.ParseCIDRSloppy(cidr)
if err != nil {
return fmt.Errorf("failed to parse node %s, CIDR %s", node.Name, cidr)
}
// IPv6 CIDRs are allocated from node-specific ranges
// We don't track them in the cidrSet
if podCIDR.IP.To4() == nil {
logger.V(4).Info("Nothing to occupy for IPv6 CIDR", "cidr", podCIDR)
return nil
}
// If node has a pre allocate cidr that does not exist in our cidrs.
// This will happen if cluster went from dualstack(multi cidrs) to non-dualstack
// then we have now way of locking it
if idx >= 1 {
return fmt.Errorf("node:%s has an allocated cidr: %v at index:%v that does not exist in cluster cidrs configuration", node.Name, cidr, idx)
}
if err := c.cidrSet.Occupy(podCIDR); err != nil {
return fmt.Errorf("failed to mark cidr[%v] at idx [%v] as occupied for node: %v: %w", podCIDR, idx, node.Name, err)
}
}
return nil
}
// getIPv6RangeFromInterface extracts the IPv6 range from a Linode instance configuration interface.
func getIPv6RangeFromInterface(iface linodego.InstanceConfigInterface) string {
if ipv6 := iface.IPv6; ipv6 != nil {
if len(ipv6.SLAAC) > 0 {
return ipv6.SLAAC[0].Range
}
if len(ipv6.Ranges) > 0 {
return ipv6.Ranges[0].Range
}
}
return ""
}
func getIPv6RangeFromLinodeInterface(iface linodego.LinodeInterface) string {
if len(iface.VPC.IPv6.SLAAC) > 0 {
return iface.VPC.IPv6.SLAAC[0].Range
}
if len(iface.VPC.IPv6.Ranges) > 0 {
return iface.VPC.IPv6.Ranges[0].Range
}
return ""
}
// allocateIPv6CIDR allocates an IPv6 CIDR for the given node.
// It retrieves the instance configuration for the node and extracts the IPv6 range.
// It then creates a new net.IPNet with the IPv6 address and mask size defined
// by nodeCIDRMaskSizeIPv6. The function returns an error if it fails to retrieve
// the instance configuration or parse the IPv6 range.
func (c *cloudAllocator) allocateIPv6CIDR(ctx context.Context, node *v1.Node) (*net.IPNet, error) {
if node.Spec.ProviderID == "" {
return nil, fmt.Errorf("node %s has no ProviderID set, cannot calculate ipv6 range for it", node.Name)
}
// Extract the Linode ID from the ProviderID
if !strings.HasPrefix(node.Spec.ProviderID, providerIDPrefix) {
return nil, fmt.Errorf("node %s has invalid ProviderID %s, expected prefix '%s'", node.Name, node.Spec.ProviderID, providerIDPrefix)
}
// Parse the Linode ID from the ProviderID
id, err := strconv.Atoi(strings.TrimPrefix(node.Spec.ProviderID, providerIDPrefix))
if err != nil {
return nil, fmt.Errorf("failed to parse Linode ID from ProviderID %s: %w", node.Spec.ProviderID, err)
}
// fetch the instance so we can determine which interface generation to use
instance, err := c.linodeClient.GetInstance(ctx, id)
if err != nil {
return nil, fmt.Errorf("failed get linode with id %d: %w", id, err)
}
ipv6Range := ""
if instance.InterfaceGeneration == linodego.GenerationLinode {
ifaces, listErr := c.linodeClient.ListInterfaces(ctx, id, &linodego.ListOptions{})
if listErr != nil || len(ifaces) == 0 {
return nil, fmt.Errorf("failed to list interfaces: %w", listErr)
}
for _, iface := range ifaces {
if iface.VPC != nil {
ipv6Range = getIPv6RangeFromLinodeInterface(iface)
if ipv6Range != "" {
break
}
}
}
if ipv6Range == "" {
return nil, fmt.Errorf("failed to find ipv6 range in Linode interfaces: %v", ifaces)
}
} else {
// Retrieve the instance configuration for the Linode ID
configs, listErr := c.linodeClient.ListInstanceConfigs(ctx, id, &linodego.ListOptions{})
if listErr != nil || len(configs) == 0 {
return nil, fmt.Errorf("failed to list instance configs: %w", listErr)
}
for _, iface := range configs[0].Interfaces {
if iface.Purpose == linodego.InterfacePurposeVPC {
ipv6Range = getIPv6RangeFromInterface(iface)
if ipv6Range != "" {
break
}
}
}
if ipv6Range == "" {
return nil, fmt.Errorf("failed to find ipv6 range in instance config: %v", configs[0])
}
}
ip, _, err := net.ParseCIDR(ipv6Range)
if err != nil {
return nil, fmt.Errorf("failed parsing ipv6 range %s: %w", ipv6Range, err)
}
mask := net.CIDRMask(c.nodeCIDRMaskSizeIPv6, 128)
ipv6Embedded := &net.IPNet{
IP: ip.Mask(mask),
Mask: mask,
}
return ipv6Embedded, nil
}
// WARNING: If you're adding any return calls or defer any more work from this
// function you have to make sure to update nodesInProcessing properly with the
// disposition of the node when the work is done.
func (c *cloudAllocator) AllocateOrOccupyCIDR(ctx context.Context, node *v1.Node) error {
if node == nil {
return nil
}
if len(node.Spec.PodCIDRs) > 0 {
return c.occupyCIDRs(ctx, node)
}
logger := klog.FromContext(ctx)
allocatedCIDRs := make([]*net.IPNet, 2)
podCIDR, err := c.cidrSet.AllocateNext()
if err != nil {
controllerutil.RecordNodeStatusChange(logger, c.recorder, node, "CIDRNotAvailable")
return fmt.Errorf("failed to allocate cidr from cluster cidr: %w", err)
}
allocatedCIDRs[0] = podCIDR
// If IPv6 CIDR allocation is disabled, log and return early.
if c.disableIPv6NodeCIDRAllocation {
logger.V(4).Info("IPv6 CIDR allocation disabled; using only IPv4", "node", klog.KObj(node))
// remove the second CIDR from the allocatedCIDRs slice
// since we are not allocating IPv6 CIDR
allocatedCIDRs = allocatedCIDRs[:1]
return c.enqueueCIDRUpdate(ctx, node.Name, allocatedCIDRs)
}
// Allocate IPv6 CIDR for the node.
logger.V(4).Info("Allocating IPv6 CIDR", "node", klog.KObj(node))
if allocatedCIDRs[1], err = c.allocateIPv6CIDR(ctx, node); err != nil {
return fmt.Errorf("failed to assign IPv6 CIDR: %w", err)
}
return c.enqueueCIDRUpdate(ctx, node.Name, allocatedCIDRs)
}
// enqueueCIDRUpdate adds the node name and CIDRs to the work queue for processing.
func (c *cloudAllocator) enqueueCIDRUpdate(ctx context.Context, nodeName string, cidrs []*net.IPNet) error {
logger := klog.FromContext(ctx)
logger.V(4).Info("Putting node with CIDR into the work queue", "node", nodeName, "CIDR", cidrs)
return c.updateCIDRsAllocation(ctx, nodeName, cidrs)
}
// ReleaseCIDR marks node.podCIDRs[...] as unused in our tracked cidrSets
func (c *cloudAllocator) ReleaseCIDR(logger klog.Logger, node *v1.Node) error {
if node == nil || len(node.Spec.PodCIDRs) == 0 {
return nil
}
for idx, cidr := range node.Spec.PodCIDRs {
_, podCIDR, err := netutils.ParseCIDRSloppy(cidr)
if err != nil {
return fmt.Errorf("failed to parse CIDR %s on Node %v: %w", cidr, node.Name, err)
}
if podCIDR.IP.To4() == nil {
logger.V(4).Info("Nothing to release for IPv6 CIDR", "cidr", podCIDR)
continue
}
// If node has a pre allocate cidr that does not exist in our cidrs.
// This will happen if cluster went from dualstack(multi cidrs) to non-dualstack
// then we have now way of locking it
if idx >= 1 {
return fmt.Errorf("node:%s has an allocated cidr: %v at index:%v that does not exist in cluster cidrs configuration", node.Name, cidr, idx)
}
logger.V(4).Info("Release CIDR for node", "CIDR", cidr, "node", klog.KObj(node))
if err = c.cidrSet.Release(podCIDR); err != nil {
return fmt.Errorf("error when releasing CIDR %v: %w", cidr, err)
}
}
return nil
}
// Marks all CIDRs with subNetMaskSize that belongs to serviceCIDR as used across all cidrs
// so that they won't be assignable.
func (c *cloudAllocator) filterOutServiceRange(logger klog.Logger, serviceCIDR *net.IPNet) {
// Checks if service CIDR has a nonempty intersection with cluster
// CIDR. It is the case if either clusterCIDR contains serviceCIDR with
// clusterCIDR's Mask applied (this means that clusterCIDR contains
// serviceCIDR) or vice versa (which means that serviceCIDR contains
// clusterCIDR).
// if they don't overlap then ignore the filtering
if !c.clusterCIDR.Contains(serviceCIDR.IP.Mask(c.clusterCIDR.Mask)) && !serviceCIDR.Contains(c.clusterCIDR.IP.Mask(serviceCIDR.Mask)) {
return
}
// at this point, len(cidrSet) == len(clusterCidr)
if err := c.cidrSet.Occupy(serviceCIDR); err != nil {
logger.Error(err, "Error filtering out service cidr out cluster cidr", "CIDR", c.clusterCIDR, "serviceCIDR", serviceCIDR)
}
}
// updateCIDRsAllocation assigns CIDR to Node and sends an update to the API server.
func (c *cloudAllocator) updateCIDRsAllocation(ctx context.Context, nodeName string, allocatedCIDRs []*net.IPNet) error {
var err error
var node *v1.Node
logger := klog.FromContext(ctx)
cidrsString := ipnetToStringList(allocatedCIDRs)
node, err = c.nodeLister.Get(nodeName)
if err != nil {
logger.Error(err, "Failed while getting node for updating Node.Spec.PodCIDRs", "node", klog.KRef("", nodeName))
return err
}
// if cidr list matches the proposed.
// then we possibly updated this node
// and just failed to ack the success.
if len(node.Spec.PodCIDRs) == len(allocatedCIDRs) {
match := true
for idx, cidr := range cidrsString {
if node.Spec.PodCIDRs[idx] != cidr {
match = false
break
}
}
if match {
logger.V(4).Info("Node already has allocated CIDR. It matches the proposed one", "node", klog.KObj(node), "CIDRs", allocatedCIDRs)
return nil
}
}
// node has cidrs, release the reserved
if len(node.Spec.PodCIDRs) != 0 {
logger.Error(nil, "Node already has a CIDR allocated. Releasing the new one", "node", klog.KObj(node), "podCIDRs", node.Spec.PodCIDRs)
if releaseErr := c.cidrSet.Release(allocatedCIDRs[0]); releaseErr != nil {
logger.Error(releaseErr, "Error when releasing CIDR", "CIDR", allocatedCIDRs[0])
}
return nil
}
// If we reached here, it means that the node has no CIDR currently assigned. So we set it.
for i := 0; i < cidrUpdateRetries; i++ {
if err = nodeutil.PatchNodeCIDRs(ctx, c.client, types.NodeName(node.Name), cidrsString); err == nil {
logger.Info("Set node PodCIDR", "node", klog.KObj(node), "podCIDRs", cidrsString)
return nil
}
}
// failed release back to the pool
logger.Error(err, "Failed to update node PodCIDR after multiple attempts", "node", klog.KObj(node), "podCIDRs", cidrsString)
controllerutil.RecordNodeStatusChange(logger, c.recorder, node, "CIDRAssignmentFailed")
// We accept the fact that we may leak CIDRs here. This is safer than releasing
// them in case when we don't know if request went through.
// NodeController restart will return all falsely allocated CIDRs to the pool.
if !apierrors.IsServerTimeout(err) {
logger.Error(err, "CIDR assignment for node failed. Releasing allocated CIDR", "node", klog.KObj(node))
if releaseErr := c.cidrSet.Release(allocatedCIDRs[0]); releaseErr != nil {
logger.Error(releaseErr, "Error releasing allocated CIDR for node", "node", klog.KObj(node))
}
}
return err
}