Skip to content

Commit 59a24be

Browse files
committed
fix: make sure machine pools are deleted before gke cluster
Signed-off-by: Carlos Salas <[email protected]>
1 parent a500b0c commit 59a24be

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

exp/controllers/gcpmanagedcluster_controller.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,17 @@ func (r *GCPManagedClusterReconciler) reconcileDelete(ctx context.Context, clust
228228
log := log.FromContext(ctx).WithValues("controller", "gcpmanagedcluster", "action", "delete")
229229
log.Info("Reconciling Delete GCPManagedCluster")
230230

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+
231242
if clusterScope.GCPManagedControlPlane != nil {
232243
log.Info("GCPManagedControlPlane not deleted yet, retry later")
233244
return ctrl.Result{RequeueAfter: reconciler.DefaultRetryTime}, nil
@@ -295,3 +306,22 @@ func (r *GCPManagedClusterReconciler) managedControlPlaneMapper() handler.MapFun
295306
}
296307
}
297308
}
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

Comments
 (0)