Skip to content

Commit

Permalink
improve FieldIndexer setting in yurt-manager (openyurtio#1834)
Browse files Browse the repository at this point in the history
* improve FieldIndexer setting in yurt-manager
  • Loading branch information
2456868764 authored and zyjhtangtang committed Apr 16, 2024
1 parent cc3e3d7 commit e5a1ce4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
20 changes: 20 additions & 0 deletions pkg/yurtmanager/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit e5a1ce4

Please sign in to comment.