@@ -22,6 +22,7 @@ import (
22
22
"time"
23
23
24
24
crdv1 "github.com/kubernetes-csi/external-snapshotter/pkg/apis/volumesnapshot/v1alpha1"
25
+ "github.com/kubernetes-csi/external-snapshotter/pkg/snapshotter"
25
26
"k8s.io/api/core/v1"
26
27
storagev1 "k8s.io/api/storage/v1"
27
28
storage "k8s.io/api/storage/v1beta1"
@@ -361,20 +362,37 @@ func (ctrl *csiSnapshotController) storeContentUpdate(content interface{}) (bool
361
362
362
363
// createSnapshot starts new asynchronous operation to create snapshot
363
364
func (ctrl * csiSnapshotController ) createSnapshot (snapshot * crdv1.VolumeSnapshot ) error {
364
- klog .V (5 ).Infof ("createSnapshot[%s]: started" , snapshotKey (snapshot ))
365
- opName := fmt .Sprintf ("create-%s[%s]" , snapshotKey (snapshot ), string (snapshot .UID ))
365
+ key := snapshotKey (snapshot )
366
+ klog .V (5 ).Infof ("createSnapshot[%s]: started" , key )
367
+ opName := fmt .Sprintf ("create-%s[%s]" , key , string (snapshot .UID ))
366
368
ctrl .scheduleOperation (opName , func () error {
367
- snapshotObj , err := ctrl .createSnapshotOperation (snapshot )
369
+ snapshotObj , state , err := ctrl .createSnapshotOperation (snapshot )
368
370
if err != nil {
369
371
ctrl .updateSnapshotErrorStatusWithEvent (snapshot , v1 .EventTypeWarning , "SnapshotCreationFailed" , fmt .Sprintf ("Failed to create snapshot: %v" , err ))
370
372
klog .Errorf ("createSnapshot [%s]: error occurred in createSnapshotOperation: %v" , opName , err )
373
+
374
+ // Handle state:
375
+ if state == snapshotter .SnapshottingFinished {
376
+ // Snapshotting finished, remove obj from snapshotsInProgress.
377
+ ctrl .snapshotsInProgress .Delete (key )
378
+ } else if state == snapshotter .SnapshottingInBackground {
379
+ // Snapshotting still in progress.
380
+ klog .V (4 ).Infof ("createSnapshot [%s]: Temporary error received, adding Snapshot back in queue: %v" , key , err )
381
+ ctrl .snapshotsInProgress .Store (key , snapshotObj )
382
+ } else {
383
+ // State is SnapshottingNoChange. Don't change snapshotsInProgress.
384
+ }
385
+
371
386
return err
372
387
}
388
+
389
+ // If no errors, update the snapshot.
373
390
_ , updateErr := ctrl .storeSnapshotUpdate (snapshotObj )
374
391
if updateErr != nil {
375
392
// We will get an "snapshot update" event soon, this is not a big error
376
- klog .V (4 ).Infof ("createSnapshot [%s]: cannot update internal cache: %v" , snapshotKey ( snapshotObj ) , updateErr )
393
+ klog .V (4 ).Infof ("createSnapshot [%s]: cannot update internal cache: %v" , key , updateErr )
377
394
}
395
+
378
396
return nil
379
397
})
380
398
return nil
@@ -584,7 +602,7 @@ func (ctrl *csiSnapshotController) checkandUpdateBoundSnapshotStatusOperation(sn
584
602
if err != nil {
585
603
return nil , fmt .Errorf ("failed to get input parameters to create snapshot %s: %q" , snapshot .Name , err )
586
604
}
587
- driverName , snapshotID , creationTime , size , readyToUse , err = ctrl .handler .CreateSnapshot (snapshot , volume , class .Parameters , snapshotterCredentials )
605
+ driverName , snapshotID , creationTime , size , readyToUse , _ , err = ctrl .handler .CreateSnapshot (snapshot , volume , class .Parameters , snapshotterCredentials )
588
606
if err != nil {
589
607
klog .Errorf ("checkandUpdateBoundSnapshotStatusOperation: failed to call create snapshot to check whether the snapshot is ready to use %q" , err )
590
608
return nil , err
@@ -611,30 +629,30 @@ func (ctrl *csiSnapshotController) checkandUpdateBoundSnapshotStatusOperation(sn
611
629
// 2. Update VolumeSnapshot status with creationtimestamp information
612
630
// 3. Create the VolumeSnapshotContent object with the snapshot id information.
613
631
// 4. Bind the VolumeSnapshot and VolumeSnapshotContent object
614
- func (ctrl * csiSnapshotController ) createSnapshotOperation (snapshot * crdv1.VolumeSnapshot ) (* crdv1.VolumeSnapshot , error ) {
632
+ func (ctrl * csiSnapshotController ) createSnapshotOperation (snapshot * crdv1.VolumeSnapshot ) (* crdv1.VolumeSnapshot , snapshotter. SnapshottingState , error ) {
615
633
klog .Infof ("createSnapshot: Creating snapshot %s through the plugin ..." , snapshotKey (snapshot ))
616
634
617
635
if snapshot .Status .Error != nil && ! isControllerUpdateFailError (snapshot .Status .Error ) {
618
636
klog .V (4 ).Infof ("error is already set in snapshot, do not retry to create: %s" , snapshot .Status .Error .Message )
619
- return snapshot , nil
637
+ return snapshot , snapshotter . SnapshottingNoChange , nil
620
638
}
621
639
622
640
// If PVC is not being deleted and finalizer is not added yet, a finalizer should be added.
623
641
klog .V (5 ).Infof ("createSnapshotOperation: Check if PVC is not being deleted and add Finalizer for source of snapshot [%s] if needed" , snapshot .Name )
624
642
err := ctrl .ensureSnapshotSourceFinalizer (snapshot )
625
643
if err != nil {
626
644
klog .Errorf ("createSnapshotOperation failed to add finalizer for source of snapshot %s" , err )
627
- return nil , err
645
+ return nil , snapshotter . SnapshottingNoChange , err
628
646
}
629
647
630
648
class , volume , contentName , snapshotterCredentials , err := ctrl .getCreateSnapshotInput (snapshot )
631
649
if err != nil {
632
- return nil , fmt .Errorf ("failed to get input parameters to create snapshot %s: %q" , snapshot .Name , err )
650
+ return nil , snapshotter . SnapshottingNoChange , fmt .Errorf ("failed to get input parameters to create snapshot %s: %q" , snapshot .Name , err )
633
651
}
634
652
635
- driverName , snapshotID , creationTime , size , readyToUse , err := ctrl .handler .CreateSnapshot (snapshot , volume , class .Parameters , snapshotterCredentials )
653
+ driverName , snapshotID , creationTime , size , readyToUse , state , err := ctrl .handler .CreateSnapshot (snapshot , volume , class .Parameters , snapshotterCredentials )
636
654
if err != nil {
637
- return nil , fmt .Errorf ("failed to take snapshot of the volume, %s: %q" , volume .Name , err )
655
+ return nil , state , fmt .Errorf ("failed to take snapshot of the volume, %s: %q" , volume .Name , err )
638
656
}
639
657
640
658
klog .V (5 ).Infof ("Created snapshot: driver %s, snapshotId %s, creationTime %v, size %d, readyToUse %t" , driverName , snapshotID , creationTime , size , readyToUse )
@@ -651,16 +669,16 @@ func (ctrl *csiSnapshotController) createSnapshotOperation(snapshot *crdv1.Volum
651
669
}
652
670
653
671
if err != nil {
654
- return nil , err
672
+ return nil , snapshotter . SnapshottingInBackground , err
655
673
}
656
674
// Create VolumeSnapshotContent in the database
657
675
volumeRef , err := ref .GetReference (scheme .Scheme , volume )
658
676
if err != nil {
659
- return nil , err
677
+ return nil , snapshotter . SnapshottingInBackground , err
660
678
}
661
679
snapshotRef , err := ref .GetReference (scheme .Scheme , snapshot )
662
680
if err != nil {
663
- return nil , err
681
+ return nil , snapshotter . SnapshottingInBackground , err
664
682
}
665
683
666
684
if class .DeletionPolicy == nil {
@@ -713,15 +731,15 @@ func (ctrl *csiSnapshotController) createSnapshotOperation(snapshot *crdv1.Volum
713
731
strerr := fmt .Sprintf ("Error creating volume snapshot content object for snapshot %s: %v." , snapshotKey (snapshot ), err )
714
732
klog .Error (strerr )
715
733
ctrl .eventRecorder .Event (newSnapshot , v1 .EventTypeWarning , "CreateSnapshotContentFailed" , strerr )
716
- return nil , newControllerUpdateError (snapshotKey (snapshot ), err .Error ())
734
+ return nil , snapshotter . SnapshottingInBackground , newControllerUpdateError (snapshotKey (snapshot ), err .Error ())
717
735
}
718
736
719
737
// save succeeded, bind and update status for snapshot.
720
738
result , err := ctrl .bindandUpdateVolumeSnapshot (snapshotContent , newSnapshot )
721
739
if err != nil {
722
- return nil , err
740
+ return nil , snapshotter . SnapshottingInBackground , err
723
741
}
724
- return result , nil
742
+ return result , snapshotter . SnapshottingFinished , nil
725
743
}
726
744
727
745
// Delete a snapshot
0 commit comments