@@ -256,16 +256,15 @@ func (ctrl *csiSnapshotController) syncUnreadySnapshot(snapshot *crdv1.VolumeSna
256256 if ! ok {
257257 return fmt .Errorf ("expected volume snapshot content, got %+v" , contentObj )
258258 }
259-
260- if err := ctrl . checkandBindSnapshotContent ( snapshot , content ); err != nil {
259+ contentBound , err := ctrl . checkandBindSnapshotContent ( snapshot , content )
260+ if err != nil {
261261 // snapshot is bound but content is not bound to snapshot correctly
262262 ctrl .updateSnapshotErrorStatusWithEvent (snapshot , v1 .EventTypeWarning , "SnapshotBindFailed" , fmt .Sprintf ("Snapshot failed to bind VolumeSnapshotContent, %v" , err ))
263263 return fmt .Errorf ("snapshot %s is bound, but VolumeSnapshotContent %s is not bound to the VolumeSnapshot correctly, %v" , uniqueSnapshotName , content .Name , err )
264264 }
265-
266265 // snapshot is already bound correctly, check the status and update if it is ready.
267266 klog .V (5 ).Infof ("Check and update snapshot %s status" , uniqueSnapshotName )
268- if err = ctrl .checkandUpdateBoundSnapshotStatus (snapshot , content ); err != nil {
267+ if err = ctrl .checkandUpdateBoundSnapshotStatus (snapshot , contentBound ); err != nil {
269268 return err
270269 }
271270 return nil
@@ -492,13 +491,13 @@ func (ctrl *csiSnapshotController) isVolumeBeingCreatedFromSnapshot(snapshot *cr
492491}
493492
494493// The function checks whether the volumeSnapshotRef in snapshot content matches the given snapshot. If match, it binds the content with the snapshot
495- func (ctrl * csiSnapshotController ) checkandBindSnapshotContent (snapshot * crdv1.VolumeSnapshot , content * crdv1.VolumeSnapshotContent ) error {
494+ func (ctrl * csiSnapshotController ) checkandBindSnapshotContent (snapshot * crdv1.VolumeSnapshot , content * crdv1.VolumeSnapshotContent ) ( * crdv1. VolumeSnapshotContent , error ) {
496495 if content .Spec .VolumeSnapshotRef == nil || content .Spec .VolumeSnapshotRef .Name != snapshot .Name {
497- return fmt .Errorf ("Could not bind snapshot %s and content %s, the VolumeSnapshotRef does not match" , snapshot .Name , content .Name )
496+ return nil , fmt .Errorf ("Could not bind snapshot %s and content %s, the VolumeSnapshotRef does not match" , snapshot .Name , content .Name )
498497 } else if content .Spec .VolumeSnapshotRef .UID != "" && content .Spec .VolumeSnapshotRef .UID != snapshot .UID {
499- return fmt .Errorf ("Could not bind snapshot %s and content %s, the VolumeSnapshotRef does not match" , snapshot .Name , content .Name )
498+ return nil , fmt .Errorf ("Could not bind snapshot %s and content %s, the VolumeSnapshotRef does not match" , snapshot .Name , content .Name )
500499 } else if content .Spec .VolumeSnapshotRef .UID != "" && content .Spec .VolumeSnapshotClassName != nil {
501- return nil
500+ return content , nil
502501 }
503502 contentClone := content .DeepCopy ()
504503 contentClone .Spec .VolumeSnapshotRef .UID = snapshot .UID
@@ -507,14 +506,14 @@ func (ctrl *csiSnapshotController) checkandBindSnapshotContent(snapshot *crdv1.V
507506 newContent , err := ctrl .clientset .VolumesnapshotV1alpha1 ().VolumeSnapshotContents ().Update (contentClone )
508507 if err != nil {
509508 klog .V (4 ).Infof ("updating VolumeSnapshotContent[%s] error status failed %v" , newContent .Name , err )
510- return err
509+ return nil , err
511510 }
512511 _ , err = ctrl .storeContentUpdate (newContent )
513512 if err != nil {
514513 klog .V (4 ).Infof ("updating VolumeSnapshotContent[%s] error status: cannot update internal cache %v" , newContent .Name , err )
515- return err
514+ return nil , err
516515 }
517- return nil
516+ return newContent , nil
518517}
519518
520519func (ctrl * csiSnapshotController ) getCreateSnapshotInput (snapshot * crdv1.VolumeSnapshot ) (* crdv1.VolumeSnapshotClass , * v1.PersistentVolume , string , map [string ]string , error ) {
@@ -560,14 +559,29 @@ func (ctrl *csiSnapshotController) checkandUpdateBoundSnapshotStatusOperation(sn
560559 var timestamp int64
561560 var size int64
562561 var readyToUse = false
563- class , volume , _ , snapshotterCredentials , err := ctrl .getCreateSnapshotInput (snapshot )
564- if err != nil {
565- return nil , fmt .Errorf ("failed to get input parameters to create snapshot %s: %q" , snapshot .Name , err )
566- }
567- driverName , snapshotID , timestamp , size , readyToUse , err := ctrl .handler .CreateSnapshot (snapshot , volume , class .Parameters , snapshotterCredentials )
568- if err != nil {
569- klog .Errorf ("checkandUpdateBoundSnapshotStatusOperation: failed to call create snapshot to check whether the snapshot is ready to use %q" , err )
570- return nil , err
562+ var driverName string
563+ var snapshotID string
564+
565+ if snapshot .Spec .Source == nil {
566+ klog .V (5 ).Infof ("checkandUpdateBoundSnapshotStatusOperation: checking whether snapshot [%s] is pre-bound to content [%s]" , snapshot .Name , content .Name )
567+ readyToUse , timestamp , size , err = ctrl .handler .GetSnapshotStatus (content )
568+ if err != nil {
569+ klog .Errorf ("checkandUpdateBoundSnapshotStatusOperation: failed to call get snapshot status to check whether snapshot is ready to use %q" , err )
570+ return nil , err
571+ }
572+ if content .Spec .CSI != nil {
573+ driverName , snapshotID = content .Spec .CSI .Driver , content .Spec .CSI .SnapshotHandle
574+ }
575+ } else {
576+ class , volume , _ , snapshotterCredentials , err := ctrl .getCreateSnapshotInput (snapshot )
577+ if err != nil {
578+ return nil , fmt .Errorf ("failed to get input parameters to create snapshot %s: %q" , snapshot .Name , err )
579+ }
580+ driverName , snapshotID , timestamp , size , readyToUse , err = ctrl .handler .CreateSnapshot (snapshot , volume , class .Parameters , snapshotterCredentials )
581+ if err != nil {
582+ klog .Errorf ("checkandUpdateBoundSnapshotStatusOperation: failed to call create snapshot to check whether the snapshot is ready to use %q" , err )
583+ return nil , err
584+ }
571585 }
572586 klog .V (5 ).Infof ("checkandUpdateBoundSnapshotStatusOperation: driver %s, snapshotId %s, timestamp %d, size %d, readyToUse %t" , driverName , snapshotID , timestamp , size , readyToUse )
573587
0 commit comments