@@ -228,6 +228,17 @@ func (r *GCPManagedClusterReconciler) reconcileDelete(ctx context.Context, clust
228
228
log := log .FromContext (ctx ).WithValues ("controller" , "gcpmanagedcluster" , "action" , "delete" )
229
229
log .Info ("Reconciling Delete GCPManagedCluster" )
230
230
231
+ numDependencies , err := r .dependencyCount (ctx , clusterScope )
232
+ if err != nil {
233
+ log .Error (err , "error getting cluster dependencies" , "namespace" , clusterScope .GCPManagedCluster .Namespace , "name" , clusterScope .GCPManagedCluster .Name )
234
+ return ctrl.Result {}, err
235
+ }
236
+ if numDependencies > 0 {
237
+ log .V (4 ).Info ("GKE cluster still has dependencies - requeue needed" , "dependencyCount" , numDependencies )
238
+ return ctrl.Result {RequeueAfter : reconciler .DefaultRetryTime }, nil
239
+ }
240
+ log .V (4 ).Info ("GKE cluster has no dependencies" )
241
+
231
242
if clusterScope .GCPManagedControlPlane != nil {
232
243
log .Info ("GCPManagedControlPlane not deleted yet, retry later" )
233
244
return ctrl.Result {RequeueAfter : reconciler .DefaultRetryTime }, nil
@@ -295,3 +306,22 @@ func (r *GCPManagedClusterReconciler) managedControlPlaneMapper() handler.MapFun
295
306
}
296
307
}
297
308
}
309
+
310
+ func (r * GCPManagedClusterReconciler ) dependencyCount (ctx context.Context , clusterScope * scope.ManagedClusterScope ) (int , error ) {
311
+ log := log .FromContext (ctx )
312
+
313
+ clusterName , clusterNamespace := clusterScope .GCPManagedCluster .Name , clusterScope .GCPManagedCluster .Namespace
314
+ log .Info ("looking for GKE cluster dependencies" , "cluster" , klog .KRef (clusterNamespace , clusterName ))
315
+
316
+ listOptions := []client.ListOption {
317
+ client .InNamespace (clusterNamespace ),
318
+ client .MatchingLabels (map [string ]string {clusterv1 .ClusterNameLabel : clusterName }),
319
+ }
320
+
321
+ managedMachinePools := & infrav1exp.GCPManagedMachinePoolList {}
322
+ if err := r .Client .List (ctx , managedMachinePools , listOptions ... ); err != nil {
323
+ return 0 , fmt .Errorf ("failed to list managed machine pools for cluster %s/%s: %w" , clusterNamespace , clusterName , err )
324
+ }
325
+
326
+ return len (managedMachinePools .Items ), nil
327
+ }
0 commit comments