Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[oadp-1.2] OADP-2144: Resize cloned pvc based on cmp between cloned vs size and source pvc size #261

33 changes: 28 additions & 5 deletions controllers/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"errors"
"fmt"
"os"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
"os"

"github.com/go-logr/logr"
volsnapmoverv1alpha1 "github.com/konveyor/volume-snapshot-mover/api/v1alpha1"
Expand Down Expand Up @@ -71,7 +71,7 @@ func (r *VolumeSnapshotBackupReconciler) MirrorPVC(log logr.Logger) (bool, error

op, err := controllerutil.CreateOrUpdate(r.Context, r.Client, pvcClone, func() error {

return r.buildPVCClone(pvcClone, &vsClone)
return r.buildPVCClone(pvcClone, &vsClone, &vscClone)
})
if err != nil {
r.Log.Info(fmt.Sprintf("err building pvc clone: %v", err))
Expand All @@ -89,7 +89,7 @@ func (r *VolumeSnapshotBackupReconciler) MirrorPVC(log logr.Logger) (bool, error
return true, nil
}

func (r *VolumeSnapshotBackupReconciler) buildPVCClone(pvcClone *corev1.PersistentVolumeClaim, vsClone *snapv1.VolumeSnapshot) error {
func (r *VolumeSnapshotBackupReconciler) buildPVCClone(pvcClone *corev1.PersistentVolumeClaim, vsClone *snapv1.VolumeSnapshot, vscClone *snapv1.VolumeSnapshotContent) error {
sourcePVC, err := r.getSourcePVC()
if err != nil {
return err
Expand All @@ -106,6 +106,20 @@ func (r *VolumeSnapshotBackupReconciler) buildPVCClone(pvcClone *corev1.Persiste
return err
}

// check the size of source pvc with that of the cloned volumesnapshotcontent
// The cloned pvc provisioning will fail if the source pvc spec.resources.requests.storage value is less than the volumesnapshotcontent.status.restorSize value
// In order to tackle this problem we compare both the values and use the one that is maximum so that provisioning of cloned pvc does not fail
// currently we are keeping this pvc resizing a default behavior but this can be put behind a datamover feature boolean flag

clonedVSCRestoreSize := vscClone.Status.RestoreSize
sourcePVCRequestSize := sourcePVC.Spec.Resources.Requests.Storage()

clonedPVCSize, _ := sourcePVCRequestSize.AsInt64()
sourcePVCRequestSizeInt, _ := sourcePVCRequestSize.AsInt64()
if *clonedVSCRestoreSize > sourcePVCRequestSizeInt {
clonedPVCSize = *clonedVSCRestoreSize
}

if pvcClone.CreationTimestamp.IsZero() {
apiGroup := "snapshot.storage.k8s.io"
pvcClone.Spec.DataSource = &corev1.TypedLocalObjectReference{
Expand Down Expand Up @@ -142,7 +156,14 @@ func (r *VolumeSnapshotBackupReconciler) buildPVCClone(pvcClone *corev1.Persiste
pvcClone.Spec.StorageClassName = sourcePVC.Spec.StorageClassName
}

pvcClone.Spec.Resources = sourcePVC.Spec.Resources
// use the clonedPVCSize that is computed earlier
storageRequestValue := resource.NewQuantity(clonedPVCSize, resource.BinarySI)
pvcClone.Spec.Resources = corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceStorage: *storageRequestValue,
},
}
r.Log.Info(fmt.Sprintf("buildPVCClone: updated size for pvcClone: %v", pvcClone.Spec.Resources.Requests.Storage().Value()))
}

return nil
Expand Down Expand Up @@ -175,6 +196,8 @@ func (r *VolumeSnapshotBackupReconciler) BindPVCToDummyPod(log logr.Logger) (boo
return false, err
}

r.Log.Info(fmt.Sprintf("BindPVCToDummyPod: cloned PVC after building storage value:%v ", clonedPVC.Spec.Resources.Requests.Storage().Value()))

// Bind the above cloned PVC to a dummy pod
dp := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down