Skip to content

Commit

Permalink
Merge pull request #480 from hrudaya21/issue-416
Browse files Browse the repository at this point in the history
fix(localpv): restore size to return as part of snapshotshot create response
  • Loading branch information
hrudaya21 authored Oct 18, 2023
2 parents 76753fe + ce8e8a7 commit 6881540
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
32 changes: 18 additions & 14 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions pkg/zfs/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 6881540

Please sign in to comment.