From e5a1ce461afaf858486a724e3cce9b2716beef3c Mon Sep 17 00:00:00 2001 From: Jun <108045855+2456868764@users.noreply.github.com> Date: Fri, 1 Dec 2023 11:03:17 +0800 Subject: [PATCH] improve FieldIndexer setting in yurt-manager (#1834) * improve FieldIndexer setting in yurt-manager --- pkg/yurtmanager/controller/controller.go | 20 +++++++++++++++++++ .../node_lifecycle_controller.go | 13 ------------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/pkg/yurtmanager/controller/controller.go b/pkg/yurtmanager/controller/controller.go index dff330ce2be..93dd693f468 100644 --- a/pkg/yurtmanager/controller/controller.go +++ b/pkg/yurtmanager/controller/controller.go @@ -20,10 +20,12 @@ import ( "context" "fmt" + v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/controller-manager/app" "k8s.io/klog/v2" + "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/manager" "github.com/openyurtio/openyurt/cmd/yurt-manager/app/config" @@ -120,5 +122,23 @@ func SetupWithManager(ctx context.Context, c *config.CompletedConfig, m manager. } } + if app.IsControllerEnabled(names.NodeLifeCycleController, ControllersDisabledByDefault, c.ComponentConfig.Generic.Controllers) || + app.IsControllerEnabled(names.PodBindingController, ControllersDisabledByDefault, c.ComponentConfig.Generic.Controllers) { + // Register spec.NodeName field indexers + if err := m.GetFieldIndexer().IndexField(context.TODO(), &v1.Pod{}, "spec.nodeName", func(rawObj client.Object) []string { + pod, ok := rawObj.(*v1.Pod) + if !ok { + return []string{} + } + if len(pod.Spec.NodeName) == 0 { + return []string{} + } + return []string{pod.Spec.NodeName} + }); err != nil { + klog.Errorf("could not register spec.NodeName field indexers %v", err) + return err + } + } + return nil } diff --git a/pkg/yurtmanager/controller/nodelifecycle/node_lifecycle_controller.go b/pkg/yurtmanager/controller/nodelifecycle/node_lifecycle_controller.go index 97031a1d718..22e6e81e41c 100644 --- a/pkg/yurtmanager/controller/nodelifecycle/node_lifecycle_controller.go +++ b/pkg/yurtmanager/controller/nodelifecycle/node_lifecycle_controller.go @@ -329,19 +329,6 @@ func Add(ctx context.Context, cfg *appconfig.CompletedConfig, mgr manager.Manage }, } c.Watch(&source.Kind{Type: &v1.Pod{}}, &handler.Funcs{}, podsPredicate) - if err := mgr.GetFieldIndexer().IndexField(context.TODO(), &v1.Pod{}, nodeNameKeyIndex, func(rawObj client.Object) []string { - pod, ok := rawObj.(*v1.Pod) - if !ok { - return []string{} - } - if len(pod.Spec.NodeName) == 0 { - return []string{} - } - return []string{pod.Spec.NodeName} - }); err != nil { - klog.Errorf("could not register spec.NodeName field indexers for nodelifecycle controller, %v", err) - return err - } nc.taintManager = scheduler.NewNoExecuteTaintManager(nc.recorder, nc.controllerRuntimeClient, nc.getPodsAssignedToNode) nodesTaintManagerPredicate := predicate.Funcs{