Skip to content

Commit eb22eac

Browse files
author
止鹜
committed
Enhance cluster deletion logic and index registration
- Move index registration to setupControllers for early availability - Add fallback mechanism in cluster deletion when index queries fail - Improve system robustness and reliability Signed-off-by: 止鹜 <[email protected]>
1 parent 456e208 commit eb22eac

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

cmd/controller-manager/app/controllermanager.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,6 @@ func startClusterController(ctx controllerscontext.Context) (enabled bool, err e
267267
mgr := ctx.Mgr
268268
opts := ctx.Opts
269269

270-
// Indexes are added to help the cluster-controller and TaintManager quickly locate ResourceBinding
271-
// and ClusterResourceBinding resources associated with a given cluster when eviction is needed.
272-
if err := indexregistry.RegisterResourceBindingIndexByFieldCluster(ctx.Context, mgr); err != nil {
273-
return false, err
274-
}
275-
if err := indexregistry.RegisterClusterResourceBindingIndexByFieldCluster(ctx.Context, mgr); err != nil {
276-
return false, err
277-
}
278-
279270
clusterController := &cluster.Controller{
280271
Client: mgr.GetClient(),
281272
EventRecorder: mgr.GetEventRecorderFor(cluster.ControllerName),
@@ -937,6 +928,17 @@ func setupControllers(ctx context.Context, mgr controllerruntime.Manager, opts *
937928
ClusterClientOption: clusterClientOption,
938929
}
939930

931+
// Register indexes that are used by multiple controllers to ensure they are available
932+
// before any controller that depends on them starts.
933+
// These indexes help cluster-controller and TaintManager quickly locate ResourceBinding
934+
// and ClusterResourceBinding resources associated with a given cluster.
935+
if err := indexregistry.RegisterResourceBindingIndexByFieldCluster(ctx, mgr); err != nil {
936+
klog.Fatalf("Failed to register ResourceBinding index: %v", err)
937+
}
938+
if err := indexregistry.RegisterClusterResourceBindingIndexByFieldCluster(ctx, mgr); err != nil {
939+
klog.Fatalf("Failed to register ClusterResourceBinding index: %v", err)
940+
}
941+
940942
if err := controllers.StartControllers(controllerContext, controllersDisabledByDefault); err != nil {
941943
klog.Fatalf("error starting controllers: %v", err)
942944
}

pkg/controllers/cluster/cluster_controller.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,23 +255,53 @@ func (c *Controller) isTargetClusterRemoved(ctx context.Context, cluster *cluste
255255
if err := c.List(ctx, rbList, client.MatchingFieldsSelector{
256256
Selector: fields.OneTermEqualSelector(indexregistry.ResourceBindingIndexByFieldCluster, cluster.Name),
257257
}); err != nil {
258-
klog.ErrorS(err, "Failed to list ResourceBindings", "cluster", cluster.Name)
259-
return false, err
260-
}
261-
if len(rbList.Items) != 0 {
262-
return false, nil
258+
// If index query fails (e.g., index not registered), fallback to listing all ResourceBindings
259+
// and filter manually to ensure cluster deletion can proceed even if index is missing.
260+
klog.V(1).ErrorS(err, "Failed to list ResourceBindings using index, falling back to full list", "cluster", cluster.Name)
261+
if err := c.List(ctx, rbList); err != nil {
262+
klog.ErrorS(err, "Failed to list ResourceBindings", "cluster", cluster.Name)
263+
return false, err
264+
}
265+
// Filter ResourceBindings that reference this cluster
266+
for i := range rbList.Items {
267+
for _, targetCluster := range rbList.Items[i].Spec.Clusters {
268+
if targetCluster.Name == cluster.Name {
269+
return false, nil
270+
}
271+
}
272+
}
273+
} else {
274+
if len(rbList.Items) != 0 {
275+
return false, nil
276+
}
263277
}
278+
264279
// List all ClusterResourceBindings which are assigned to this cluster.
265280
crbList := &workv1alpha2.ClusterResourceBindingList{}
266281
if err := c.List(ctx, crbList, client.MatchingFieldsSelector{
267282
Selector: fields.OneTermEqualSelector(indexregistry.ClusterResourceBindingIndexByFieldCluster, cluster.Name),
268283
}); err != nil {
269-
klog.ErrorS(err, "Failed to list ClusterResourceBindings", "cluster", cluster.Name)
270-
return false, err
271-
}
272-
if len(crbList.Items) != 0 {
273-
return false, nil
284+
// If index query fails (e.g., index not registered), fallback to listing all ClusterResourceBindings
285+
// and filter manually to ensure cluster deletion can proceed even if index is missing.
286+
klog.V(1).ErrorS(err, "Failed to list ClusterResourceBindings using index, falling back to full list", "cluster", cluster.Name)
287+
if err := c.List(ctx, crbList); err != nil {
288+
klog.ErrorS(err, "Failed to list ClusterResourceBindings", "cluster", cluster.Name)
289+
return false, err
290+
}
291+
// Filter ClusterResourceBindings that reference this cluster
292+
for i := range crbList.Items {
293+
for _, targetCluster := range crbList.Items[i].Spec.Clusters {
294+
if targetCluster.Name == cluster.Name {
295+
return false, nil
296+
}
297+
}
298+
}
299+
} else {
300+
if len(crbList.Items) != 0 {
301+
return false, nil
302+
}
274303
}
304+
275305
return true, nil
276306
}
277307

0 commit comments

Comments
 (0)