From 7636b668256317bf85186193ac1a099f61257b23 Mon Sep 17 00:00:00 2001 From: jun <2456868764@qq.com> Date: Wed, 29 Nov 2023 16:28:01 +0800 Subject: [PATCH 1/3] improve FieldIndexer setting in yurt-manager --- pkg/yurtmanager/controller/controller.go | 16 ++++++++++++ .../node_lifecycle_controller.go | 26 +++++++++---------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/pkg/yurtmanager/controller/controller.go b/pkg/yurtmanager/controller/controller.go index dff330ce2be..9f36ca220a6 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,19 @@ func SetupWithManager(ctx context.Context, c *config.CompletedConfig, m manager. } } + 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..df656bd170d 100644 --- a/pkg/yurtmanager/controller/nodelifecycle/node_lifecycle_controller.go +++ b/pkg/yurtmanager/controller/nodelifecycle/node_lifecycle_controller.go @@ -329,19 +329,19 @@ 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 - } + //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{ From 2528befa8e2e15476f887a1d74888ae62d6cc532 Mon Sep 17 00:00:00 2001 From: jun <2456868764@qq.com> Date: Wed, 29 Nov 2023 19:19:04 +0800 Subject: [PATCH 2/3] remove comment --- .../nodelifecycle/node_lifecycle_controller.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/pkg/yurtmanager/controller/nodelifecycle/node_lifecycle_controller.go b/pkg/yurtmanager/controller/nodelifecycle/node_lifecycle_controller.go index df656bd170d..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{ From 2aee8dd7e1f608d0f8b213e3979058d99552ca4f Mon Sep 17 00:00:00 2001 From: jun <2456868764@qq.com> Date: Thu, 30 Nov 2023 13:31:14 +0800 Subject: [PATCH 3/3] add checking node life cycle controller and pod binding controller enabled or not when register field indexers --- pkg/yurtmanager/controller/controller.go | 26 ++++++++++++++---------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/pkg/yurtmanager/controller/controller.go b/pkg/yurtmanager/controller/controller.go index 9f36ca220a6..93dd693f468 100644 --- a/pkg/yurtmanager/controller/controller.go +++ b/pkg/yurtmanager/controller/controller.go @@ -122,18 +122,22 @@ func SetupWithManager(ctx context.Context, c *config.CompletedConfig, m manager. } } - 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{} + 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 []string{pod.Spec.NodeName} - }); err != nil { - klog.Errorf("could not register spec.NodeName field indexers %v", err) - return err } return nil