From ced2e475e61308b1dd64eaa3528568024f9cfd87 Mon Sep 17 00:00:00 2001 From: Dharaneeshwaran Ravichandran Date: Thu, 14 Nov 2024 15:58:45 +0530 Subject: [PATCH] Handle external managed cluster's image deletion 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) }