@@ -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