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