@@ -23,13 +23,10 @@ import (
2323 "time"
2424
2525 v1 "k8s.io/api/core/v1"
26- "k8s.io/apimachinery/pkg/util/wait"
27- "k8s.io/klog/v2"
28- netutils "k8s.io/utils/net"
29-
3026 apierrors "k8s.io/apimachinery/pkg/api/errors"
3127 "k8s.io/apimachinery/pkg/types"
3228 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
29+ "k8s.io/apimachinery/pkg/util/wait"
3330 informers "k8s.io/client-go/informers/core/v1"
3431 clientset "k8s.io/client-go/kubernetes"
3532 "k8s.io/client-go/kubernetes/scheme"
@@ -39,8 +36,10 @@ import (
3936 "k8s.io/client-go/tools/record"
4037 "k8s.io/client-go/util/workqueue"
4138 nodeutil "k8s.io/component-helpers/node/util"
39+ "k8s.io/klog/v2"
4240 "k8s.io/kubernetes/pkg/controller/nodeipam/ipam/cidrset"
4341 controllerutil "k8s.io/kubernetes/pkg/controller/util/node"
42+ netutils "k8s.io/utils/net"
4443)
4544
4645type cloudAllocator struct {
@@ -58,11 +57,13 @@ type cloudAllocator struct {
5857
5958 // queues are where incoming work is placed to de-dup and to allow "easy"
6059 // rate limited requeues on errors
61- queue workqueue.RateLimitingInterface
60+ queue workqueue.TypedRateLimitingInterface [ any ]
6261}
6362
64- var _ CIDRAllocator = & cloudAllocator {}
65- var nodeCIDRMaskSizeIPv6 = 112
63+ var (
64+ _ CIDRAllocator = & cloudAllocator {}
65+ nodeCIDRMaskSizeIPv6 = 112
66+ )
6667
6768// NewLinodeCIDRAllocator returns a CIDRAllocator to allocate CIDRs for node
6869// Caller must ensure subNetMaskSize is not less than cluster CIDR mask size.
@@ -97,7 +98,7 @@ func NewLinodeCIDRAllocator(ctx context.Context, client clientset.Interface, nod
9798 nodesSynced : nodeInformer .Informer ().HasSynced ,
9899 broadcaster : eventBroadcaster ,
99100 recorder : recorder ,
100- queue : workqueue .NewNamedRateLimitingQueue (workqueue .DefaultControllerRateLimiter (), "cidrallocator_node" ),
101+ queue : workqueue .NewNamedRateLimitingQueue (workqueue .DefaultTypedControllerRateLimiter [ any ] (), "cidrallocator_node" ),
101102 }
102103
103104 if allocatorParams .ServiceCIDR != nil {
@@ -129,15 +130,15 @@ func NewLinodeCIDRAllocator(ctx context.Context, client clientset.Interface, nod
129130 }
130131 }
131132
132- nodeInformer .Informer ().AddEventHandler (cache.ResourceEventHandlerFuncs {
133+ if _ , err := nodeInformer .Informer ().AddEventHandler (cache.ResourceEventHandlerFuncs {
133134 AddFunc : func (obj interface {}) {
134135 key , err := cache .MetaNamespaceKeyFunc (obj )
135136 if err == nil {
136137 ca .queue .Add (key )
137138 }
138139 },
139- UpdateFunc : func (old , new interface {}) {
140- key , err := cache .MetaNamespaceKeyFunc (new )
140+ UpdateFunc : func (oldObj , newObj interface {}) {
141+ key , err := cache .MetaNamespaceKeyFunc (newObj )
141142 if err == nil {
142143 ca .queue .Add (key )
143144 }
@@ -160,10 +161,12 @@ func NewLinodeCIDRAllocator(ctx context.Context, client clientset.Interface, nod
160161 }
161162 if err := ca .ReleaseCIDR (logger , node ); err != nil {
162163 utilruntime .HandleError (fmt .Errorf ("error while processing CIDR Release: %w" , err ))
163-
164164 }
165165 },
166- })
166+ }); err != nil {
167+ logger .Error (err , "Failed to add event handler to node informer" )
168+ return nil , err
169+ }
167170
168171 return ca , nil
169172}
@@ -247,7 +250,6 @@ func (c *cloudAllocator) processNextNodeWorkItem(ctx context.Context) bool {
247250 logger .V (4 ).Info ("Successfully synced" , "key" , key )
248251 return nil
249252 }(klog .FromContext (ctx ), obj )
250-
251253 if err != nil {
252254 utilruntime .HandleError (err )
253255 return true
@@ -292,7 +294,7 @@ func (c *cloudAllocator) occupyCIDRs(node *v1.Node) error {
292294 return fmt .Errorf ("failed to parse node %s, CIDR %s" , node .Name , node .Spec .PodCIDR )
293295 }
294296 if podCIDR .IP .To4 () == nil {
295- fmt . Printf ("Nothing to do for ipv6 CIDR" )
297+ klog . Infof ("Nothing to occupy for ipv6 CIDR %v" , podCIDR )
296298 return nil
297299 }
298300 // If node has a pre allocate cidr that does not exist in our cidrs.
@@ -303,7 +305,7 @@ func (c *cloudAllocator) occupyCIDRs(node *v1.Node) error {
303305 }
304306
305307 if err := c .cidrSet .Occupy (podCIDR ); err != nil {
306- return fmt .Errorf ("failed to mark cidr[%v] at idx [%v] as occupied for node: %v: %v " , podCIDR , idx , node .Name , err )
308+ return fmt .Errorf ("failed to mark cidr[%v] at idx [%v] as occupied for node: %v: %w " , podCIDR , idx , node .Name , err )
307309 }
308310 }
309311 return nil
@@ -369,14 +371,14 @@ func (c *cloudAllocator) AllocateOrOccupyCIDR(ctx context.Context, node *v1.Node
369371 podCIDR , err := c .cidrSet .AllocateNext ()
370372 if err != nil {
371373 controllerutil .RecordNodeStatusChange (logger , c .recorder , node , "CIDRNotAvailable" )
372- return fmt .Errorf ("failed to allocate cidr from cluster cidr: %v " , err )
374+ return fmt .Errorf ("failed to allocate cidr from cluster cidr: %w " , err )
373375 }
374376 allocatedCIDRs [0 ] = podCIDR
375377 if allocatedCIDRs [1 ], err = c .createIPv6CIDR (node ); err != nil {
376- return fmt .Errorf ("failed to assign IPv6 CIDR: %v " , err )
378+ return fmt .Errorf ("failed to assign IPv6 CIDR: %w " , err )
377379 }
378380
379- //queue the assignment
381+ // queue the assignment
380382 logger .V (4 ).Info ("Putting node with CIDR into the work queue" , "node" , klog .KObj (node ), "CIDR" , allocatedCIDRs )
381383 return c .updateCIDRsAllocation (ctx , node .Name , allocatedCIDRs )
382384}
@@ -390,10 +392,10 @@ func (c *cloudAllocator) ReleaseCIDR(logger klog.Logger, node *v1.Node) error {
390392 for idx , cidr := range node .Spec .PodCIDRs {
391393 _ , podCIDR , err := netutils .ParseCIDRSloppy (cidr )
392394 if err != nil {
393- return fmt .Errorf ("failed to parse CIDR %s on Node %v: %v " , cidr , node .Name , err )
395+ return fmt .Errorf ("failed to parse CIDR %s on Node %v: %w " , cidr , node .Name , err )
394396 }
395397 if podCIDR .IP .To4 () == nil {
396- fmt . Printf ("Nothing to do for ipv6 CIDR" )
398+ klog . Infof ("Nothing to release for ipv6 CIDR %v" , podCIDR )
397399 continue
398400 }
399401
@@ -406,7 +408,7 @@ func (c *cloudAllocator) ReleaseCIDR(logger klog.Logger, node *v1.Node) error {
406408
407409 logger .V (4 ).Info ("Release CIDR for node" , "CIDR" , cidr , "node" , klog .KObj (node ))
408410 if err = c .cidrSet .Release (podCIDR ); err != nil {
409- return fmt .Errorf ("error when releasing CIDR %v: %v " , cidr , err )
411+ return fmt .Errorf ("error when releasing CIDR %v: %w " , cidr , err )
410412 }
411413 }
412414 return nil
0 commit comments