Skip to content

Commit

Permalink
Support original accessModes for non-CephFS source PVCs
Browse files Browse the repository at this point in the history
For non-CephFS PVCs, temporary PVCs now inherit the accessModes of the
source PVC rather than using ReadOnlyMany.

Signed-off-by: Benamar Mekhissi <[email protected]>
  • Loading branch information
Benamar Mekhissi authored and ShyamsundarR committed Jan 7, 2025
1 parent e6dece7 commit ca28628
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 59 deletions.
51 changes: 0 additions & 51 deletions internal/controller/cephfscg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import (
"github.com/ramendr/ramen/internal/controller/volsync"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

// ------------- [Begin] Copied from existing code in Ramen ----
Expand Down Expand Up @@ -54,55 +52,6 @@ func getLocalServiceNameForRD(rdName string) string {
return fmt.Sprintf("volsync-rsync-tls-dst-%s", rdName)
}

// ------------- [End] Copied from existing code in Ramen ----

// ------------- [Begin] Edited from existing code in Ramen ----

// Copied from func (v *VSHandler) ModifyRSSpecForCephFS
func GetRestoreStorageClass(
ctx context.Context, k8sClient client.Client, storageClassName string,
defaultCephFSCSIDriverName string,
) (*storagev1.StorageClass, error) {
storageClass, err := GetStorageClass(ctx, k8sClient, &storageClassName)
if err != nil {
return nil, err
}

if storageClass.Provisioner != defaultCephFSCSIDriverName {
return storageClass, nil // No workaround required
}

// Create/update readOnlyPVCStorageClass
readOnlyPVCStorageClass := &storagev1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: storageClass.GetName() + "-vrg",
},
}

_, err = ctrlutil.CreateOrUpdate(ctx, k8sClient, readOnlyPVCStorageClass, func() error {
// Do not update the storageclass if it already exists - Provisioner and Parameters are immutable anyway
if readOnlyPVCStorageClass.CreationTimestamp.IsZero() {
readOnlyPVCStorageClass.Provisioner = storageClass.Provisioner

// Copy other parameters from the original storage class
readOnlyPVCStorageClass.Parameters = map[string]string{}
for k, v := range storageClass.Parameters {
readOnlyPVCStorageClass.Parameters[k] = v
}

// Set backingSnapshot parameter to true
readOnlyPVCStorageClass.Parameters["backingSnapshot"] = "true"
}

return nil
})
if err != nil {
return nil, fmt.Errorf("%w", err)
}

return readOnlyPVCStorageClass, nil
}

// Copied from func (v *VSHandler) getStorageClass(
func GetStorageClass(
ctx context.Context, k8sClient client.Client, storageClassName *string,
Expand Down
19 changes: 11 additions & 8 deletions internal/controller/cephfscg/volumegroupsourcehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,14 @@ func (h *volumeGroupSourceHandler) RestoreVolumesFromVolumeGroupSnapshot(
volumeGroupSnapshot.Namespace+"/"+pvcVSRef.PersistentVolumeClaimRef.Name, err)
}

restoreStorageClass, err := GetRestoreStorageClass(ctx, h.Client,
*pvc.Spec.StorageClassName, h.DefaultCephFSCSIDriverName)
storageClass, err := GetStorageClass(ctx, h.Client, pvc.Spec.StorageClassName)
if err != nil {
return nil, fmt.Errorf("failed to get Restore Storage Class from PVC %s: %w", pvc.Name+"/"+pvc.Namespace, err)
return nil, err
}

restoreAccessModes := []corev1.PersistentVolumeAccessMode{corev1.ReadOnlyMany}
if storageClass.Provisioner != h.DefaultCephFSCSIDriverName {
restoreAccessModes = pvc.Spec.AccessModes
}

RestoredPVCNamespacedName := types.NamespacedName{
Expand All @@ -258,7 +262,7 @@ func (h *volumeGroupSourceHandler) RestoreVolumesFromVolumeGroupSnapshot(
}
if err := h.RestoreVolumesFromSnapshot(
ctx, pvcVSRef.VolumeSnapshotRef.Name, pvc, RestoredPVCNamespacedName,
restoreStorageClass.GetName(), owner); err != nil {
restoreAccessModes, owner); err != nil {
return nil, fmt.Errorf("failed to restore volumes from snapshot %s: %w",
pvcVSRef.VolumeSnapshotRef.Name+"/"+pvc.Namespace, err)
}
Expand Down Expand Up @@ -286,7 +290,7 @@ func (h *volumeGroupSourceHandler) RestoreVolumesFromSnapshot(
vsName string,
pvc *corev1.PersistentVolumeClaim,
restoredPVCNamespacedname types.NamespacedName,
restoreStorageClassName string,
restoreAccessModes []corev1.PersistentVolumeAccessMode,
owner metav1.Object,
) error {
logger := h.Logger.WithName("RestoreVolumesFromSnapshot").
Expand Down Expand Up @@ -351,8 +355,8 @@ func (h *volumeGroupSourceHandler) RestoreVolumesFromSnapshot(
}

if restoredPVC.CreationTimestamp.IsZero() { // set immutable fields
restoredPVC.Spec.AccessModes = []corev1.PersistentVolumeAccessMode{corev1.ReadOnlyMany}
restoredPVC.Spec.StorageClassName = &restoreStorageClassName
restoredPVC.Spec.AccessModes = restoreAccessModes
restoredPVC.Spec.StorageClassName = pvc.Spec.StorageClassName
restoredPVC.Spec.DataSource = &snapshotRef
}

Expand Down Expand Up @@ -424,7 +428,6 @@ func (h *volumeGroupSourceHandler) CreateOrUpdateReplicationSourceForRestoredPVC
}
replicationSource.Spec.RsyncTLS = &volsyncv1alpha1.ReplicationSourceRsyncTLSSpec{
ReplicationSourceVolumeOptions: volsyncv1alpha1.ReplicationSourceVolumeOptions{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadOnlyMany},
CopyMethod: volsyncv1alpha1.CopyMethodDirect,
},

Expand Down

0 comments on commit ca28628

Please sign in to comment.