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 d7086b0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 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 delete when deleting 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
5 changes: 5 additions & 0 deletions api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ limitations under the License.

package v1beta1

const (
// DeletePolicyLabel is the label set on ibmpowervsimage to identify images to delete when deleting associated cluster.
DeletePolicyLabel = "infrastructure.cluster.x-k8s.io/delete-policy"
)

// PowerVSInstanceState describes the state of an IBM Power VS instance.
type PowerVSInstanceState string

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 delete when deleting associated cluster.
enum:
- delete
- retain
type: string
object:
description: Cloud Object Storage image filename
type: string
Expand Down
2 changes: 1 addition & 1 deletion controllers/ibmpowervscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (r *IBMPowerVSClusterReconciler) listDescendants(ctx context.Context, clust

listOptions := []client.ListOption{
client.InNamespace(cluster.Namespace),
client.MatchingLabels(map[string]string{clusterv1.ClusterLabelName: cluster.Name}),
client.MatchingLabels(map[string]string{clusterv1.ClusterLabelName: cluster.Name, v1beta1.DeletePolicyLabel: "delete"}),
}

if err := r.Client.List(ctx, &descendants.ibmPowerVSImages, listOptions...); err != nil {
Expand Down
15 changes: 11 additions & 4 deletions controllers/ibmpowervsimage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func (r *IBMPowerVSImageReconciler) Reconcile(ctx context.Context, req ctrl.Requ

cluster, err := util.GetClusterByName(ctx, r.Client, ibmImage.Namespace, ibmImage.Spec.ClusterName)
if err != nil {
if ibmImage.Spec.DeletePolicy == "retain" {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}

Expand Down Expand Up @@ -106,6 +109,10 @@ func (r *IBMPowerVSImageReconciler) reconcile(ctx context.Context, cluster *v1be
imageScope.IBMPowerVSImage.Labels[clusterv1.ClusterLabelName] = imageScope.IBMPowerVSImage.Spec.ClusterName
}

if _, ok := imageScope.IBMPowerVSImage.Labels[v1beta1.DeletePolicyLabel]; !ok {
imageScope.IBMPowerVSImage.Labels[v1beta1.DeletePolicyLabel] = imageScope.IBMPowerVSImage.Spec.DeletePolicy
}

if r.shouldAdopt(*imageScope.IBMPowerVSImage) {
imageScope.Info("Image Controller has not yet set OwnerRef")
imageScope.IBMPowerVSImage.OwnerReferences = clusterv1util.EnsureOwnerRef(imageScope.IBMPowerVSImage.OwnerReferences, metav1.OwnerReference{
Expand All @@ -121,7 +128,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 +137,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

0 comments on commit d7086b0

Please sign in to comment.