From 869aaced7d76a110421314e2b09a9e0d9f49223e Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Thu, 8 Aug 2024 11:03:29 +0200 Subject: [PATCH] rbd: convert rbdVolume to rbdSnapshot After cloning the RBD snapshot, an rbdVolume is returned for the CSI.Snapshot object. In order to use the rbdSnapshot.ToCSI() function, the rbdVolume needs to be converted (back) to an rbdSnaphot. Signed-off-by: Niels de Vos --- internal/rbd/controllerserver.go | 27 ++++++++++++--------------- internal/rbd/snapshot.go | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/internal/rbd/controllerserver.go b/internal/rbd/controllerserver.go index b020bdef018..f0ac0a47956 100644 --- a/internal/rbd/controllerserver.go +++ b/internal/rbd/controllerserver.go @@ -32,7 +32,6 @@ import ( "github.com/kubernetes-csi/csi-lib-utils/protosanitizer" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "google.golang.org/protobuf/types/known/timestamppb" ) const ( @@ -1236,14 +1235,13 @@ func (cs *ControllerServer) CreateSnapshot( return nil, status.Error(codes.Internal, err.Error()) } + csiSnap, err := vol.toSnapshot().ToCSI(ctx) + if err != nil { + return nil, status.Error(codes.Internal, err.Error()) + } + return &csi.CreateSnapshotResponse{ - Snapshot: &csi.Snapshot{ - SizeBytes: vol.VolSize, - SnapshotId: vol.VolID, - SourceVolumeId: req.GetSourceVolumeId(), - CreationTime: timestamppb.New(*vol.CreatedAt), - ReadyToUse: true, - }, + Snapshot: csiSnap, }, nil } @@ -1296,14 +1294,13 @@ func cloneFromSnapshot( } } + csiSnap, err := rbdSnap.ToCSI(ctx) + if err != nil { + return nil, status.Error(codes.Internal, err.Error()) + } + return &csi.CreateSnapshotResponse{ - Snapshot: &csi.Snapshot{ - SizeBytes: rbdSnap.VolSize, - SnapshotId: rbdSnap.VolID, - SourceVolumeId: rbdSnap.SourceVolumeID, - CreationTime: timestamppb.New(*rbdSnap.CreatedAt), - ReadyToUse: true, - }, + Snapshot: csiSnap, }, nil } diff --git a/internal/rbd/snapshot.go b/internal/rbd/snapshot.go index 15f1db5cb97..fd7badb41e6 100644 --- a/internal/rbd/snapshot.go +++ b/internal/rbd/snapshot.go @@ -101,6 +101,27 @@ func cleanUpSnapshot( return nil } +func (rv *rbdVolume) toSnapshot() *rbdSnapshot { + return &rbdSnapshot{ + rbdImage: rbdImage{ + ClusterID: rv.ClusterID, + VolID: rv.VolID, + Monitors: rv.Monitors, + Pool: rv.Pool, + JournalPool: rv.JournalPool, + RadosNamespace: rv.RadosNamespace, + RbdImageName: rv.RbdImageName, + ImageID: rv.ImageID, + CreatedAt: rv.CreatedAt, + // copyEncryptionConfig cannot be used here because the volume and the + // snapshot will have the same volumeID which cases the panic in + // copyEncryptionConfig function. + blockEncryption: rv.blockEncryption, + fileEncryption: rv.fileEncryption, + }, + } +} + func (rbdSnap *rbdSnapshot) toVolume() *rbdVolume { return &rbdVolume{ rbdImage: rbdImage{ @@ -112,6 +133,7 @@ func (rbdSnap *rbdSnapshot) toVolume() *rbdVolume { RadosNamespace: rbdSnap.RadosNamespace, RbdImageName: rbdSnap.RbdSnapName, ImageID: rbdSnap.ImageID, + CreatedAt: rbdSnap.CreatedAt, // copyEncryptionConfig cannot be used here because the volume and the // snapshot will have the same volumeID which cases the panic in // copyEncryptionConfig function.