diff --git a/cmd/controller-manager/app/controllermanager.go b/cmd/controller-manager/app/controllermanager.go index 5f68b6af963a..df82942e6a3a 100644 --- a/cmd/controller-manager/app/controllermanager.go +++ b/cmd/controller-manager/app/controllermanager.go @@ -267,15 +267,6 @@ func startClusterController(ctx controllerscontext.Context) (enabled bool, err e mgr := ctx.Mgr opts := ctx.Opts - // Indexes are added to help the cluster-controller and TaintManager quickly locate ResourceBinding - // and ClusterResourceBinding resources associated with a given cluster when eviction is needed. - if err := indexregistry.RegisterResourceBindingIndexByFieldCluster(ctx.Context, mgr); err != nil { - return false, err - } - if err := indexregistry.RegisterClusterResourceBindingIndexByFieldCluster(ctx.Context, mgr); err != nil { - return false, err - } - clusterController := &cluster.Controller{ Client: mgr.GetClient(), EventRecorder: mgr.GetEventRecorderFor(cluster.ControllerName), @@ -937,6 +928,17 @@ func setupControllers(ctx context.Context, mgr controllerruntime.Manager, opts * ClusterClientOption: clusterClientOption, } + // Register indexes that are used by multiple controllers to ensure they are available + // before any controller that depends on them starts. + // These indexes help cluster-controller and TaintManager quickly locate ResourceBinding + // and ClusterResourceBinding resources associated with a given cluster. + if err := indexregistry.RegisterResourceBindingIndexByFieldCluster(ctx, mgr); err != nil { + klog.Fatalf("Failed to register ResourceBinding index: %v", err) + } + if err := indexregistry.RegisterClusterResourceBindingIndexByFieldCluster(ctx, mgr); err != nil { + klog.Fatalf("Failed to register ClusterResourceBinding index: %v", err) + } + if err := controllers.StartControllers(controllerContext, controllersDisabledByDefault); err != nil { klog.Fatalf("error starting controllers: %v", err) } diff --git a/pkg/controllers/cluster/cluster_controller.go b/pkg/controllers/cluster/cluster_controller.go index fd1e734b958c..c8219dfbe775 100644 --- a/pkg/controllers/cluster/cluster_controller.go +++ b/pkg/controllers/cluster/cluster_controller.go @@ -255,23 +255,53 @@ func (c *Controller) isTargetClusterRemoved(ctx context.Context, cluster *cluste if err := c.List(ctx, rbList, client.MatchingFieldsSelector{ Selector: fields.OneTermEqualSelector(indexregistry.ResourceBindingIndexByFieldCluster, cluster.Name), }); err != nil { - klog.ErrorS(err, "Failed to list ResourceBindings", "cluster", cluster.Name) - return false, err - } - if len(rbList.Items) != 0 { - return false, nil + // If index query fails (e.g., index not registered), fallback to listing all ResourceBindings + // and filter manually to ensure cluster deletion can proceed even if index is missing. + klog.V(1).ErrorS(err, "Failed to list ResourceBindings using index, falling back to full list", "cluster", cluster.Name) + if err := c.List(ctx, rbList); err != nil { + klog.ErrorS(err, "Failed to list ResourceBindings", "cluster", cluster.Name) + return false, err + } + // Filter ResourceBindings that reference this cluster + for i := range rbList.Items { + for _, targetCluster := range rbList.Items[i].Spec.Clusters { + if targetCluster.Name == cluster.Name { + return false, nil + } + } + } + } else { + if len(rbList.Items) != 0 { + return false, nil + } } + // List all ClusterResourceBindings which are assigned to this cluster. crbList := &workv1alpha2.ClusterResourceBindingList{} if err := c.List(ctx, crbList, client.MatchingFieldsSelector{ Selector: fields.OneTermEqualSelector(indexregistry.ClusterResourceBindingIndexByFieldCluster, cluster.Name), }); err != nil { - klog.ErrorS(err, "Failed to list ClusterResourceBindings", "cluster", cluster.Name) - return false, err - } - if len(crbList.Items) != 0 { - return false, nil + // If index query fails (e.g., index not registered), fallback to listing all ClusterResourceBindings + // and filter manually to ensure cluster deletion can proceed even if index is missing. + klog.V(1).ErrorS(err, "Failed to list ClusterResourceBindings using index, falling back to full list", "cluster", cluster.Name) + if err := c.List(ctx, crbList); err != nil { + klog.ErrorS(err, "Failed to list ClusterResourceBindings", "cluster", cluster.Name) + return false, err + } + // Filter ClusterResourceBindings that reference this cluster + for i := range crbList.Items { + for _, targetCluster := range crbList.Items[i].Spec.Clusters { + if targetCluster.Name == cluster.Name { + return false, nil + } + } + } + } else { + if len(crbList.Items) != 0 { + return false, nil + } } + return true, nil }