@@ -255,23 +255,63 @@ 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+ filtered := make ([]workv1alpha2.ResourceBinding , 0 )
267+ for i := range rbList .Items {
268+ for _ , targetCluster := range rbList .Items [i ].Spec .Clusters {
269+ if targetCluster .Name == cluster .Name {
270+ filtered = append (filtered , rbList .Items [i ])
271+ break
272+ }
273+ }
274+ }
275+ if len (filtered ) != 0 {
276+ return false , nil
277+ }
278+ } else {
279+ if len (rbList .Items ) != 0 {
280+ return false , nil
281+ }
263282 }
283+
264284 // List all ClusterResourceBindings which are assigned to this cluster.
265285 crbList := & workv1alpha2.ClusterResourceBindingList {}
266286 if err := c .List (ctx , crbList , client.MatchingFieldsSelector {
267287 Selector : fields .OneTermEqualSelector (indexregistry .ClusterResourceBindingIndexByFieldCluster , cluster .Name ),
268288 }); 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
289+ // If index query fails (e.g., index not registered), fallback to listing all ClusterResourceBindings
290+ // and filter manually to ensure cluster deletion can proceed even if index is missing.
291+ klog .V (1 ).ErrorS (err , "Failed to list ClusterResourceBindings using index, falling back to full list" , "cluster" , cluster .Name )
292+ if err := c .List (ctx , crbList ); err != nil {
293+ klog .ErrorS (err , "Failed to list ClusterResourceBindings" , "cluster" , cluster .Name )
294+ return false , err
295+ }
296+ // Filter ClusterResourceBindings that reference this cluster
297+ filtered := make ([]workv1alpha2.ClusterResourceBinding , 0 )
298+ for i := range crbList .Items {
299+ for _ , targetCluster := range crbList .Items [i ].Spec .Clusters {
300+ if targetCluster .Name == cluster .Name {
301+ filtered = append (filtered , crbList .Items [i ])
302+ break
303+ }
304+ }
305+ }
306+ if len (filtered ) != 0 {
307+ return false , nil
308+ }
309+ } else {
310+ if len (crbList .Items ) != 0 {
311+ return false , nil
312+ }
274313 }
314+
275315 return true , nil
276316}
277317
0 commit comments