From 09308ea144958e5ddc110b1c611e3a444d35f82a Mon Sep 17 00:00:00 2001 From: Prajyot-Parab Date: Thu, 17 Feb 2022 09:35:16 +0000 Subject: [PATCH] Add DeletePolicy to IBMPowerVSImage Spec Signed-off-by: Prajyot-Parab --- api/v1beta1/ibmpowervsimage_types.go | 6 ++++++ ...ure.cluster.x-k8s.io_ibmpowervsimages.yaml | 8 ++++++++ controllers/ibmpowervsimage_controller.go | 20 +++++++++++-------- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/api/v1beta1/ibmpowervsimage_types.go b/api/v1beta1/ibmpowervsimage_types.go index a408f24e44..0893235e55 100644 --- a/api/v1beta1/ibmpowervsimage_types.go +++ b/api/v1beta1/ibmpowervsimage_types.go @@ -53,6 +53,12 @@ type IBMPowerVSImageSpec struct { // +kubebuilder:validation:Enum=tier1;tier3 // +optional StorageType string `json:"storageType,omitempty"` + + // DeletePolicy defines the policy used to identify images to be preserved beyond the lifecycle of associated cluster. + // +kubebuilder:default=delete + // +kubebuilder:validation:Enum=delete;retain + // +optional + DeletePolicy string `json:"deletePolicy,omitempty"` } // IBMPowerVSImageStatus defines the observed state of IBMPowerVSImage diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsimages.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsimages.yaml index 229dedb8b4..654ceba5d9 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsimages.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsimages.yaml @@ -52,6 +52,14 @@ spec: to. minLength: 1 type: string + deletePolicy: + default: delete + description: DeletePolicy defines the policy used to identify images + to be preserved beyond the lifecycle of associated cluster. + enum: + - delete + - retain + type: string object: description: Cloud Object Storage image filename type: string diff --git a/controllers/ibmpowervsimage_controller.go b/controllers/ibmpowervsimage_controller.go index 25405127fb..54b47ac8a1 100644 --- a/controllers/ibmpowervsimage_controller.go +++ b/controllers/ibmpowervsimage_controller.go @@ -41,6 +41,8 @@ import ( clusterv1util "sigs.k8s.io/cluster-api/util" ) +const deletePolicyRetain = "retain" + // IBMPowerVSImageReconciler reconciles a IBMPowerVSImage object type IBMPowerVSImageReconciler struct { client.Client @@ -121,7 +123,7 @@ func (r *IBMPowerVSImageReconciler) reconcile(ctx context.Context, cluster *v1be job, err := imageScope.IBMPowerVSClient.GetJob(jobID) if err != nil { imageScope.Info("Unable to get job details") - return ctrl.Result{RequeueAfter: 2 * time.Minute}, err + return ctrl.Result{RequeueAfter: 1 * time.Minute}, err } switch *job.Status.State { case "completed": @@ -130,17 +132,17 @@ func (r *IBMPowerVSImageReconciler) reconcile(ctx context.Context, cluster *v1be imageScope.SetNotReady() imageScope.SetImageState(string(v1beta1.PowerVSImageStateFailed)) conditions.MarkFalse(imageScope.IBMPowerVSImage, v1beta1.ImageImportedCondition, v1beta1.ImageImportFailedReason, clusterv1.ConditionSeverityError, job.Status.Message) - return ctrl.Result{RequeueAfter: 2 * time.Minute}, fmt.Errorf("failed to import image, message: %s", job.Status.Message) + return ctrl.Result{RequeueAfter: 1 * time.Minute}, fmt.Errorf("failed to import image, message: %s", job.Status.Message) case "queued": imageScope.SetNotReady() imageScope.SetImageState(string(v1beta1.PowerVSImageStateQue)) conditions.MarkFalse(imageScope.IBMPowerVSImage, v1beta1.ImageImportedCondition, string(v1beta1.PowerVSImageStateQue), clusterv1.ConditionSeverityInfo, job.Status.Message) - return ctrl.Result{RequeueAfter: 2 * time.Minute}, nil + return ctrl.Result{RequeueAfter: 1 * time.Minute}, nil default: imageScope.SetNotReady() imageScope.SetImageState(string(v1beta1.PowerVSImageStateImporting)) conditions.MarkFalse(imageScope.IBMPowerVSImage, v1beta1.ImageImportedCondition, *job.Status.State, clusterv1.ConditionSeverityInfo, job.Status.Message) - return ctrl.Result{RequeueAfter: 2 * time.Minute}, nil + return ctrl.Result{RequeueAfter: 1 * time.Minute}, nil } } @@ -217,11 +219,13 @@ func (r *IBMPowerVSImageReconciler) reconcileDelete(scope *scope.PowerVSImageSco } return ctrl.Result{}, nil } - if err := scope.DeleteImage(); err != nil { - scope.Info("error deleting IBMPowerVSImage") - return ctrl.Result{}, errors.Wrapf(err, "error deleting IBMPowerVSImage %s/%s", scope.IBMPowerVSImage.Namespace, scope.IBMPowerVSImage.Name) - } + if scope.IBMPowerVSImage.Spec.DeletePolicy != deletePolicyRetain { + if err := scope.DeleteImage(); err != nil { + scope.Info("error deleting IBMPowerVSImage") + return ctrl.Result{}, errors.Wrapf(err, "error deleting IBMPowerVSImage %s/%s", scope.IBMPowerVSImage.Namespace, scope.IBMPowerVSImage.Name) + } + } return ctrl.Result{}, nil }