From ce8e8a78b0462af9d9683bde078c021484ef90c6 Mon Sep 17 00:00:00 2001 From: Hrudaya Date: Wed, 4 Oct 2023 04:13:59 +0000 Subject: [PATCH] fix(localpv): restore size to return as part of snapshotshot create response Signed-off-by: Hrudaya --- pkg/driver/controller.go | 32 ++++++++++++++++++-------------- pkg/zfs/volume.go | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/pkg/driver/controller.go b/pkg/driver/controller.go index ef9323e96..c873416dd 100644 --- a/pkg/driver/controller.go +++ b/pkg/driver/controller.go @@ -710,26 +710,28 @@ func (cs *controller) CreateSnapshot( ) (*csi.CreateSnapshotResponse, error) { snapName := strings.ToLower(req.GetName()) volumeID := strings.ToLower(req.GetSourceVolumeId()) - klog.Infof("CreateSnapshot volume %s@%s", volumeID, snapName) - err := verifySnapshotRequest(req) if err != nil { return nil, err } snapTimeStamp := time.Now().Unix() - state, err := zfs.GetZFSSnapshotStatus(snapName) - - if err == nil { + var state string + if snapObj, err := zfs.GetZFSSnapshot(snapName); err == nil { + state = snapObj.Status.State + size, err := zfs.GetZFSSnapshotCapacity(snapObj) + if err != nil { + return nil, fmt.Errorf("get zfssnapshot capacity failed: %v, capacity: %v", err, snapObj.Spec.Capacity) + } return csipayload.NewCreateSnapshotResponseBuilder(). WithSourceVolumeID(volumeID). WithSnapshotID(volumeID+"@"+snapName). + WithSize(size). WithCreationTime(snapTimeStamp, 0). WithReadyToUse(state == zfs.ZFSStatusReady). Build(), nil } - vol, err := zfs.GetZFSVolume(volumeID) if err != nil { return nil, status.Errorf( @@ -739,13 +741,10 @@ func (cs *controller) CreateSnapshot( err.Error(), ) } - labels := map[string]string{zfs.ZFSVolKey: vol.Name} - snapObj, err := snapbuilder.NewBuilder(). WithName(snapName). WithLabels(labels).Build() - if err != nil { return nil, status.Errorf( codes.Internal, @@ -754,10 +753,8 @@ func (cs *controller) CreateSnapshot( err.Error(), ) } - snapObj.Spec = vol.Spec snapObj.Status.State = zfs.ZFSStatusPending - if err := zfs.ProvisionSnapshot(snapObj); err != nil { return nil, status.Errorf( codes.Internal, @@ -766,21 +763,28 @@ func (cs *controller) CreateSnapshot( err.Error(), ) } - originalParams := req.GetParameters() parameters := helpers.GetCaseInsensitiveMap(&originalParams) - if _, ok := parameters["wait"]; ok { if err := waitForReadySnapshot(snapName); err != nil { return nil, err } } - state, _ = zfs.GetZFSSnapshotStatus(snapName) + snapObj, err = zfs.GetZFSSnapshot(snapName) + if err != nil { + return nil, fmt.Errorf("get zfssnapshot failed, err: %v", err) + } + state = snapObj.Status.State + size, err := zfs.GetZFSSnapshotCapacity(snapObj) + if err != nil { + return nil, fmt.Errorf("get zfssnapshot capacity failed: %v, capacity: %v", err, snapObj.Spec.Capacity) + } return csipayload.NewCreateSnapshotResponseBuilder(). WithSourceVolumeID(volumeID). WithSnapshotID(volumeID+"@"+snapName). + WithSize(size). WithCreationTime(snapTimeStamp, 0). WithReadyToUse(state == zfs.ZFSStatusReady). Build(), nil diff --git a/pkg/zfs/volume.go b/pkg/zfs/volume.go index 2ce19208a..0d262a067 100644 --- a/pkg/zfs/volume.go +++ b/pkg/zfs/volume.go @@ -313,6 +313,24 @@ func GetZFSSnapshotStatus(snapID string) (string, error) { return snap.Status.State, nil } +// GetZFSSnapshotCapacity return capacity converted to int64 +func GetZFSSnapshotCapacity(snap *apis.ZFSSnapshot) (int64, error) { + if snap == nil { + return 0, fmt.Errorf("expect non-nil snapshot") + } + + if snap.Spec.Capacity == "" { + return 0, nil + } + + capacity, err := strconv.ParseInt(snap.Spec.Capacity, 10, 64) + if err != nil { + return 0, fmt.Errorf("convert %s to integer failed", snap.Spec.Capacity) + } + + return capacity, nil +} + // UpdateSnapInfo updates ZFSSnapshot CR with node id and finalizer func UpdateSnapInfo(snap *apis.ZFSSnapshot) error { finalizers := []string{ZFSFinalizer}