@@ -365,7 +365,6 @@ func CreateLVMVolume(ctx context.Context, req *csi.CreateVolumeRequest,
365365 }
366366 klog .Infof ("scheduling the volume %s/%s on node %s" ,
367367 params .VgPattern .String (), volName , owner )
368-
369368 volObj , err := volbuilder .NewBuilder ().
370369 WithName (volName ).
371370 WithCapacity (capacity ).
@@ -664,14 +663,19 @@ func (cs *controller) CreateSnapshot(
664663 }
665664
666665 snapTimeStamp := time .Now ().Unix ()
667- state , err := lvm .GetLVMSnapshotStatus (req .Name )
668-
666+ var lvmSnap * lvmapi.LVMSnapshot
667+ var restoreSize int64
668+ lvmSnap , err = waitForLVMSnapshotReady (req .Name )
669669 if err == nil {
670+ if lvmSnap .Spec .ThinProvision {
671+ restoreSize = lvmSnap .Status .LvSize .Value ()
672+ }
670673 return csipayload .NewCreateSnapshotResponseBuilder ().
671674 WithSourceVolumeID (req .SourceVolumeId ).
672675 WithSnapshotID (req .SourceVolumeId + "@" + req .Name ).
673676 WithCreationTime (snapTimeStamp , 0 ).
674- WithReadyToUse (state == lvm .LVMStatusReady ).
677+ WithReadyToUse (lvmSnap .Status .State == lvm .LVMStatusReady ).
678+ WithSize (restoreSize ).
675679 Build (), nil
676680 }
677681
@@ -751,16 +755,47 @@ func (cs *controller) CreateSnapshot(
751755 )
752756 }
753757
754- state , _ = lvm .GetLVMSnapshotStatus (req .Name )
758+ lvmSnap , err = waitForLVMSnapshotReady (req .Name )
759+ if err != nil {
760+ return nil , status .Errorf (
761+ codes .ResourceExhausted ,
762+ "failed to handle CreateSnapshotRequest for %s: %s, {%s}" ,
763+ req .SourceVolumeId , req .Name ,
764+ err .Error (),
765+ )
766+ }
767+ if lvmSnap .Spec .ThinProvision {
768+ restoreSize = lvmSnap .Status .LvSize .Value ()
769+ }
755770
756771 return csipayload .NewCreateSnapshotResponseBuilder ().
757772 WithSourceVolumeID (req .SourceVolumeId ).
758773 WithSnapshotID (req .SourceVolumeId + "@" + req .Name ).
759774 WithCreationTime (snapTimeStamp , 0 ).
760- WithReadyToUse (state == lvm .LVMStatusReady ).
775+ WithReadyToUse (lvmSnap .Status .State == lvm .LVMStatusReady ).
776+ WithSize (restoreSize ).
761777 Build (), nil
762778}
763779
780+ // waitForLVMSnapshotReady waits for lvm snapshot to be ready and returns
781+ // the lvm snapshot CR object
782+ func waitForLVMSnapshotReady (snapName string ) (* lvmapi.LVMSnapshot , error ) {
783+ var err error
784+ var snap * lvmapi.LVMSnapshot
785+ for i := 0 ; i < 20 ; i ++ {
786+ snap , err = lvm .GetLVMSnapshot (snapName )
787+ if k8serror .IsNotFound (err ) {
788+ return snap , err
789+ } else if err == nil {
790+ if snap .Status .State == lvm .LVMStatusReady {
791+ break
792+ }
793+ }
794+ time .Sleep (time .Duration (100 * time .Millisecond ))
795+ }
796+ return snap , err
797+ }
798+
764799func getSnapSize (params * SnapshotParams , capacity int64 ) int64 {
765800 var snapSize int64
766801 if ! params .AbsSnapSize {
0 commit comments