Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions cmd/controller-manager/app/controllermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
}
Comment on lines +931 to +940
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Chen-speculation, I don't get why moving the index registration earlier. Can you remind me?


if err := controllers.StartControllers(controllerContext, controllersDisabledByDefault); err != nil {
klog.Fatalf("error starting controllers: %v", err)
}
Expand Down
50 changes: 40 additions & 10 deletions pkg/controllers/cluster/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down