From 84fa4663d9ec0fbd09c0fb31f41cd67aa6851011 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 ++++++ api/v1beta1/types.go | 8 ++++++++ ...frastructure.cluster.x-k8s.io_ibmpowervsimages.yaml | 8 ++++++++ controllers/ibmpowervsimage_controller.go | 10 ++++++---- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/api/v1beta1/ibmpowervsimage_types.go b/api/v1beta1/ibmpowervsimage_types.go index a408f24e4..0893235e5 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/api/v1beta1/types.go b/api/v1beta1/types.go index d42d53ae2..76d96e82c 100644 --- a/api/v1beta1/types.go +++ b/api/v1beta1/types.go @@ -50,6 +50,14 @@ var ( PowerVSImageStateImporting = PowerVSImageState("importing") ) +// DeletePolicy defines the policy used to identify images to be preserved. +type DeletePolicy string + +var ( + // DeletePolicyRetain is the string representing an image to be retained. + DeletePolicyRetain = DeletePolicy("retain") +) + // NetworkInterface holds the network interface information like subnet id. type NetworkInterface struct { // Subnet ID of the network interface 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 229dedb8b..654ceba5d 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 25405127f..be8642b99 100644 --- a/controllers/ibmpowervsimage_controller.go +++ b/controllers/ibmpowervsimage_controller.go @@ -217,11 +217,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 != string(v1beta1.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 }