Skip to content

Commit 05da19f

Browse files
author
Rahul Sharma
committed
fix linting errors
1 parent b1ded13 commit 05da19f

4 files changed

Lines changed: 31 additions & 34 deletions

File tree

cloud/nodeipam/ipam/cidr_allocator.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import (
2222
"net"
2323
"time"
2424

25-
"k8s.io/kubernetes/pkg/controller/nodeipam/ipam/cidrset"
26-
2725
v1 "k8s.io/api/core/v1"
2826
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2927
"k8s.io/apimachinery/pkg/fields"
@@ -33,6 +31,7 @@ import (
3331
clientset "k8s.io/client-go/kubernetes"
3432
cloudprovider "k8s.io/cloud-provider"
3533
"k8s.io/klog/v2"
34+
"k8s.io/kubernetes/pkg/controller/nodeipam/ipam/cidrset"
3635
)
3736

3837
// CIDRAllocatorType is the type of the allocator to use.
@@ -104,6 +103,8 @@ func New(ctx context.Context, kubeClient clientset.Interface, cloud cloudprovide
104103
switch allocatorType {
105104
case CloudAllocatorType:
106105
return NewLinodeCIDRAllocator(ctx, kubeClient, nodeInformer, allocatorParams, nodeList)
106+
case RangeAllocatorType:
107+
return nil, fmt.Errorf("RangeAllocatorType is not supported")
107108
default:
108109
return nil, fmt.Errorf("invalid or unsupported CIDR allocator type: %v", allocatorType)
109110
}

cloud/nodeipam/ipam/cloud_allocator.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4645
type 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

cloud/nodeipam/ipam/controller_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package ipam
1818

1919
import (
20+
"errors"
2021
"net"
2122
"testing"
2223

@@ -49,7 +50,7 @@ TestCase:
4950
for {
5051
cidr, err := set.AllocateNext()
5152
if err != nil {
52-
if err == cidrset.ErrCIDRRangeNoCIDRsRemaining {
53+
if errors.Is(err, cidrset.ErrCIDRRangeNoCIDRsRemaining) {
5354
break
5455
}
5556
t.Errorf("set.AllocateNext() = %v, want %v", err, cidrset.ErrCIDRRangeNoCIDRsRemaining)

cloud/nodeipam/node_ipam_controller.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ import (
3535
"github.com/linode/linode-cloud-controller-manager/cloud/nodeipam/ipam"
3636
)
3737

38-
// ipamController is an interface abstracting an interface for
39-
// legacy mode.
40-
type ipamController interface {
41-
Run(ctx context.Context)
42-
}
43-
4438
// Controller is the controller that manages node ipam state.
4539
type Controller struct {
4640
allocatorType ipam.CIDRAllocatorType
@@ -55,7 +49,6 @@ type Controller struct {
5549
nodeLister corelisters.NodeLister
5650
nodeInformerSynced cache.InformerSynced
5751

58-
legacyIPAM ipamController
5952
cidrAllocator ipam.CIDRAllocator
6053
}
6154

@@ -73,8 +66,8 @@ func NewNodeIpamController(
7366
serviceCIDR *net.IPNet,
7467
secondaryServiceCIDR *net.IPNet,
7568
nodeCIDRMaskSizes []int,
76-
allocatorType ipam.CIDRAllocatorType) (*Controller, error) {
77-
69+
allocatorType ipam.CIDRAllocatorType,
70+
) (*Controller, error) {
7871
if kubeClient == nil {
7972
return nil, fmt.Errorf("kubeClient is nil when starting Controller")
8073
}

0 commit comments

Comments
 (0)