From 1edcfec26c03387e18f83004f0500db9b36a6edc Mon Sep 17 00:00:00 2001 From: Dharaneeshwaran Ravichandran <17947543+dharaneeshvrd@users.noreply.github.com> Date: Fri, 15 Nov 2024 13:22:52 +0530 Subject: [PATCH] Handle external managed cluster's image deletion (#2052) When an image is associated to a externally managed cluster, during deletion there might be a chance where cluster resource might have already cleaned up So get cluster only when image is not deleted --- controllers/ibmpowervsimage_controller.go | 24 ++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/controllers/ibmpowervsimage_controller.go b/controllers/ibmpowervsimage_controller.go index 58e097eb6..2ad94ac6f 100644 --- a/controllers/ibmpowervsimage_controller.go +++ b/controllers/ibmpowervsimage_controller.go @@ -66,19 +66,25 @@ func (r *IBMPowerVSImageReconciler) Reconcile(ctx context.Context, req ctrl.Requ return ctrl.Result{}, err } - cluster, err := scope.GetClusterByName(ctx, r.Client, ibmImage.Namespace, ibmImage.Spec.ClusterName) - if err != nil { - return ctrl.Result{}, err - } - - // Create the scope. - imageScope, err := scope.NewPowerVSImageScope(scope.PowerVSImageScopeParams{ + var cluster *infrav1beta2.IBMPowerVSCluster + scopeParams := scope.PowerVSImageScopeParams{ Client: r.Client, Logger: log, IBMPowerVSImage: ibmImage, - Zone: cluster.Spec.Zone, ServiceEndpoint: r.ServiceEndpoint, - }) + } + + // Externally managed clusters might not be available during image deletion. Get the cluster only when image is still not deleted. + if ibmImage.DeletionTimestamp.IsZero() { + cluster, err = scope.GetClusterByName(ctx, r.Client, ibmImage.Namespace, ibmImage.Spec.ClusterName) + if err != nil { + return ctrl.Result{}, err + } + scopeParams.Zone = cluster.Spec.Zone + } + + // Create the scope + imageScope, err := scope.NewPowerVSImageScope(scopeParams) if err != nil { return ctrl.Result{}, fmt.Errorf("failed to create scope: %w", err) }