Skip to content

Commit

Permalink
Add DeletePolicy to IBMPowerVSImage Spec
Browse files Browse the repository at this point in the history
Signed-off-by: Prajyot-Parab <[email protected]>
  • Loading branch information
Prajyot-Parab committed Feb 17, 2022
1 parent 74042dc commit 09308ea
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
6 changes: 6 additions & 0 deletions api/v1beta1/ibmpowervsimage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions controllers/ibmpowervsimage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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":
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 09308ea

Please sign in to comment.