Skip to content

Commit

Permalink
Merge pull request #234 from jingyuanliang/lint
Browse files Browse the repository at this point in the history
Handle potential error returned from nodeInformer.SetTransform()
  • Loading branch information
k8s-ci-robot authored Nov 15, 2024
2 parents c009822 + 9c47250 commit 9c84f0c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
14 changes: 10 additions & 4 deletions pkg/autoscaler/k8sclient/k8sclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ type k8sClient struct {
stopCh chan struct{}
}

func getTrimmedNodeClients(clientset kubernetes.Interface, labelOptions informers.SharedInformerOption) (informers.SharedInformerFactory, corelisters.NodeLister) {
func getTrimmedNodeClients(clientset kubernetes.Interface, labelOptions informers.SharedInformerOption) (informers.SharedInformerFactory, corelisters.NodeLister, error) {
factory := informers.NewSharedInformerFactoryWithOptions(clientset, 0, labelOptions)
nodeInformer := factory.Core().V1().Nodes().Informer()
nodeInformer.SetTransform(func(obj any) (any, error) {
err := nodeInformer.SetTransform(func(obj any) (any, error) {
// Trimming unneeded fields to reduce memory consumption under large-scale.
if node, ok := obj.(*v1.Node); ok {
node.ObjectMeta = metav1.ObjectMeta{
Expand All @@ -79,8 +79,11 @@ func getTrimmedNodeClients(clientset kubernetes.Interface, labelOptions informer
}
return obj, nil
})
if err != nil {
return nil, nil, err
}
nodeLister := factory.Core().V1().Nodes().Lister()
return factory, nodeLister
return factory, nodeLister, nil
}

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

Expand Down
5 changes: 4 additions & 1 deletion pkg/autoscaler/k8sclient/k8sclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@ func TestGetTrimmedNodeClients(t *testing.T) {

// Start the informer.
labelOptions := informers.WithTweakListOptions(func(opts *metav1.ListOptions) {})
factory, nodelister := getTrimmedNodeClients(client, labelOptions)
factory, nodelister, err := getTrimmedNodeClients(client, labelOptions)
if err != nil {
t.Fatal(err)
}
stopCh := make(chan struct{})
factory.Start(stopCh)
factory.WaitForCacheSync(stopCh)
Expand Down

0 comments on commit 9c84f0c

Please sign in to comment.