Skip to content

Commit 9c84f0c

Browse files
authored
Merge pull request #234 from jingyuanliang/lint
Handle potential error returned from nodeInformer.SetTransform()
2 parents c009822 + 9c47250 commit 9c84f0c

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

pkg/autoscaler/k8sclient/k8sclient.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ type k8sClient struct {
6161
stopCh chan struct{}
6262
}
6363

64-
func getTrimmedNodeClients(clientset kubernetes.Interface, labelOptions informers.SharedInformerOption) (informers.SharedInformerFactory, corelisters.NodeLister) {
64+
func getTrimmedNodeClients(clientset kubernetes.Interface, labelOptions informers.SharedInformerOption) (informers.SharedInformerFactory, corelisters.NodeLister, error) {
6565
factory := informers.NewSharedInformerFactoryWithOptions(clientset, 0, labelOptions)
6666
nodeInformer := factory.Core().V1().Nodes().Informer()
67-
nodeInformer.SetTransform(func(obj any) (any, error) {
67+
err := nodeInformer.SetTransform(func(obj any) (any, error) {
6868
// Trimming unneeded fields to reduce memory consumption under large-scale.
6969
if node, ok := obj.(*v1.Node); ok {
7070
node.ObjectMeta = metav1.ObjectMeta{
@@ -79,8 +79,11 @@ func getTrimmedNodeClients(clientset kubernetes.Interface, labelOptions informer
7979
}
8080
return obj, nil
8181
})
82+
if err != nil {
83+
return nil, nil, err
84+
}
8285
nodeLister := factory.Core().V1().Nodes().Lister()
83-
return factory, nodeLister
86+
return factory, nodeLister, nil
8487
}
8588

8689
// NewK8sClient gives a k8sClient with the given dependencies.
@@ -90,7 +93,10 @@ func NewK8sClient(clientset kubernetes.Interface, namespace, target string, node
9093
labelOptions := informers.WithTweakListOptions(func(opts *metav1.ListOptions) {
9194
opts.LabelSelector = nodelabels
9295
})
93-
factory, nodeLister := getTrimmedNodeClients(clientset, labelOptions)
96+
factory, nodeLister, err := getTrimmedNodeClients(clientset, labelOptions)
97+
if err != nil {
98+
return nil, err
99+
}
94100
factory.Start(stopCh)
95101
factory.WaitForCacheSync(stopCh)
96102

pkg/autoscaler/k8sclient/k8sclient_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,10 @@ func TestGetTrimmedNodeClients(t *testing.T) {
248248

249249
// Start the informer.
250250
labelOptions := informers.WithTweakListOptions(func(opts *metav1.ListOptions) {})
251-
factory, nodelister := getTrimmedNodeClients(client, labelOptions)
251+
factory, nodelister, err := getTrimmedNodeClients(client, labelOptions)
252+
if err != nil {
253+
t.Fatal(err)
254+
}
252255
stopCh := make(chan struct{})
253256
factory.Start(stopCh)
254257
factory.WaitForCacheSync(stopCh)

0 commit comments

Comments
 (0)