From 543392fb7baceb27053b20dbb6d0dad81c03e4b8 Mon Sep 17 00:00:00 2001 From: Karthik K N Date: Thu, 20 Oct 2022 16:25:53 +0530 Subject: [PATCH] Changes to support ibmpowervs image deletion without cluster reference --- cloud/scope/powervs_image.go | 22 +++++++------------- controllers/ibmpowervsimage_controller.go | 25 +++++++++-------------- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/cloud/scope/powervs_image.go b/cloud/scope/powervs_image.go index 2f55f4fd3..7b9987afe 100644 --- a/cloud/scope/powervs_image.go +++ b/cloud/scope/powervs_image.go @@ -46,11 +46,10 @@ const BucketAccess = "public" // PowerVSImageScopeParams defines the input parameters used to create a new PowerVSImageScope. type PowerVSImageScopeParams struct { - Client client.Client - Logger logr.Logger - IBMPowerVSImage *infrav1beta1.IBMPowerVSImage - IBMPowerVSCluster *infrav1beta1.IBMPowerVSCluster - ServiceEndpoint []endpoints.ServiceEndpoint + Client client.Client + Logger logr.Logger + IBMPowerVSImage *infrav1beta1.IBMPowerVSImage + ServiceEndpoint []endpoints.ServiceEndpoint } // PowerVSImageScope defines a scope defined around a Power VS Cluster. @@ -59,10 +58,9 @@ type PowerVSImageScope struct { Client client.Client patchHelper *patch.Helper - IBMPowerVSClient powervs.PowerVS - IBMPowerVSImage *infrav1beta1.IBMPowerVSImage - IBMPowerVSCluster *infrav1beta1.IBMPowerVSCluster - ServiceEndpoint []endpoints.ServiceEndpoint + IBMPowerVSClient powervs.PowerVS + IBMPowerVSImage *infrav1beta1.IBMPowerVSImage + ServiceEndpoint []endpoints.ServiceEndpoint } // NewPowerVSImageScope creates a new PowerVSImageScope from the supplied parameters. @@ -125,12 +123,6 @@ func NewPowerVSImageScope(params PowerVSImageScopeParams) (scope *PowerVSImageSc CloudInstanceID: spec.ServiceInstanceID, } - if params.IBMPowerVSCluster == nil { - err = errors.New("failed to generate new scope from nil IBMPowerVSCluster") - return - } - scope.IBMPowerVSCluster = params.IBMPowerVSCluster - // Fetch the service endpoint. if svcEndpoint := endpoints.FetchPVSEndpoint(endpoints.CostructRegionFromZone(*res.RegionID), params.ServiceEndpoint); svcEndpoint != "" { options.IBMPIOptions.URL = svcEndpoint diff --git a/controllers/ibmpowervsimage_controller.go b/controllers/ibmpowervsimage_controller.go index 077b3e08d..95a7bc2de 100644 --- a/controllers/ibmpowervsimage_controller.go +++ b/controllers/ibmpowervsimage_controller.go @@ -34,7 +34,6 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - "sigs.k8s.io/controller-runtime/pkg/reconcile" capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1" clusterv1util "sigs.k8s.io/cluster-api/util" @@ -70,22 +69,17 @@ func (r *IBMPowerVSImageReconciler) Reconcile(ctx context.Context, req ctrl.Requ return ctrl.Result{}, err } - cluster, err := util.GetClusterByName(ctx, r.Client, ibmImage.Namespace, ibmImage.Spec.ClusterName) - if err != nil { - return ctrl.Result{}, err - } - log = log.WithValues("cluster", cluster.Name) - // Create the scope. imageScope, err := scope.NewPowerVSImageScope(scope.PowerVSImageScopeParams{ - Client: r.Client, - Logger: log, - IBMPowerVSImage: ibmImage, - IBMPowerVSCluster: cluster, - ServiceEndpoint: r.ServiceEndpoint, + Client: r.Client, + Logger: log, + IBMPowerVSImage: ibmImage, + ServiceEndpoint: r.ServiceEndpoint, }) - - // Always close the scope when exiting this function so we can persist any GCPMachine changes. + if err != nil { + return ctrl.Result{}, errors.Errorf("failed to create scope: %+v", err) + } + // Always close the scope when exiting this function so we can persist any IBM PowerVS Image changes. defer func() { if err := imageScope.Close(); err != nil && reterr == nil { reterr = err @@ -97,8 +91,9 @@ func (r *IBMPowerVSImageReconciler) Reconcile(ctx context.Context, req ctrl.Requ return r.reconcileDelete(imageScope) } + cluster, err := util.GetClusterByName(ctx, r.Client, ibmImage.Namespace, ibmImage.Spec.ClusterName) if err != nil { - return reconcile.Result{}, errors.Errorf("failed to create scope: %+v", err) + return ctrl.Result{}, err } return r.reconcile(cluster, imageScope) }