@@ -18,10 +18,8 @@ package controller
18
18
19
19
import (
20
20
"fmt"
21
- "strings"
22
- "time"
23
-
24
21
crdv1 "github.com/kubernetes-csi/external-snapshotter/pkg/apis/volumesnapshot/v1beta1"
22
+ "github.com/kubernetes-csi/external-snapshotter/pkg/snapshotter"
25
23
v1 "k8s.io/api/core/v1"
26
24
storagev1 "k8s.io/api/storage/v1"
27
25
apierrs "k8s.io/apimachinery/pkg/api/errors"
@@ -34,6 +32,8 @@ import (
34
32
"k8s.io/kubernetes/pkg/util/goroutinemap"
35
33
"k8s.io/kubernetes/pkg/util/goroutinemap/exponentialbackoff"
36
34
"k8s.io/kubernetes/pkg/util/slice"
35
+ "strings"
36
+ "time"
37
37
)
38
38
39
39
// ==================================================================
@@ -363,20 +363,37 @@ func (ctrl *csiSnapshotController) storeContentUpdate(content interface{}) (bool
363
363
364
364
// createSnapshot starts new asynchronous operation to create snapshot
365
365
func (ctrl * csiSnapshotController ) createSnapshot (snapshot * crdv1.VolumeSnapshot ) error {
366
- klog .V (5 ).Infof ("createSnapshot[%s]: started" , snapshotKey (snapshot ))
367
- opName := fmt .Sprintf ("create-%s[%s]" , snapshotKey (snapshot ), string (snapshot .UID ))
366
+ key := snapshotKey (snapshot )
367
+ klog .V (5 ).Infof ("createSnapshot[%s]: started" , key )
368
+ opName := fmt .Sprintf ("create-%s[%s]" , key , string (snapshot .UID ))
368
369
ctrl .scheduleOperation (opName , func () error {
369
- snapshotObj , err := ctrl .createSnapshotOperation (snapshot )
370
+ snapshotObj , state , err := ctrl .createSnapshotOperation (snapshot )
370
371
if err != nil {
371
372
ctrl .updateSnapshotErrorStatusWithEvent (snapshot , v1 .EventTypeWarning , "SnapshotCreationFailed" , fmt .Sprintf ("Failed to create snapshot: %v" , err ))
372
373
klog .Errorf ("createSnapshot [%s]: error occurred in createSnapshotOperation: %v" , opName , err )
374
+
375
+ // Handle state:
376
+ if state == snapshotter .SnapshottingFinished {
377
+ // Snapshotting finished, remove obj from snapshotsInProgress.
378
+ ctrl .snapshotsInProgress .Delete (key )
379
+ } else if state == snapshotter .SnapshottingInBackground {
380
+ // Snapshotting still in progress.
381
+ klog .V (4 ).Infof ("createSnapshot [%s]: Temporary error received, adding Snapshot back in queue: %v" , key , err )
382
+ ctrl .snapshotsInProgress .Store (key , snapshotObj )
383
+ } else {
384
+ // State is SnapshottingNoChange. Don't change snapshotsInProgress.
385
+ }
386
+
373
387
return err
374
388
}
389
+
390
+ // If no errors, update the snapshot.
375
391
_ , updateErr := ctrl .storeSnapshotUpdate (snapshotObj )
376
392
if updateErr != nil {
377
393
// We will get an "snapshot update" event soon, this is not a big error
378
- klog .V (4 ).Infof ("createSnapshot [%s]: cannot update internal cache: %v" , snapshotKey ( snapshotObj ) , updateErr )
394
+ klog .V (4 ).Infof ("createSnapshot [%s]: cannot update internal cache: %v" , key , updateErr )
379
395
}
396
+
380
397
return nil
381
398
})
382
399
return nil
@@ -588,7 +605,7 @@ func (ctrl *csiSnapshotController) checkandUpdateBoundSnapshotStatusOperation(sn
588
605
if err != nil {
589
606
return nil , err
590
607
}
591
- driverName , snapshotID , creationTime , size , readyToUse , err = ctrl .handler .CreateSnapshot (snapshot , volume , class .Parameters , snapshotterCredentials )
608
+ driverName , snapshotID , creationTime , size , readyToUse , _ , err = ctrl .handler .CreateSnapshot (snapshot , volume , class .Parameters , snapshotterCredentials )
592
609
if err != nil {
593
610
klog .Errorf ("checkandUpdateBoundSnapshotStatusOperation: failed to call create snapshot to check whether the snapshot is ready to use %q" , err )
594
611
return nil , err
@@ -622,35 +639,35 @@ func (ctrl *csiSnapshotController) checkandUpdateBoundSnapshotStatusOperation(sn
622
639
// 2. Update VolumeSnapshot status with creationtimestamp information
623
640
// 3. Create the VolumeSnapshotContent object with the snapshot id information.
624
641
// 4. Bind the VolumeSnapshot and VolumeSnapshotContent object
625
- func (ctrl * csiSnapshotController ) createSnapshotOperation (snapshot * crdv1.VolumeSnapshot ) (* crdv1.VolumeSnapshot , error ) {
642
+ func (ctrl * csiSnapshotController ) createSnapshotOperation (snapshot * crdv1.VolumeSnapshot ) (* crdv1.VolumeSnapshot , snapshotter. SnapshottingState , error ) {
626
643
klog .Infof ("createSnapshot: Creating snapshot %s through the plugin ..." , snapshotKey (snapshot ))
627
644
628
645
if snapshot .Status != nil && snapshot .Status .Error != nil && snapshot .Status .Error .Message != nil && ! isControllerUpdateFailError (snapshot .Status .Error ) {
629
646
klog .V (4 ).Infof ("error is already set in snapshot, do not retry to create: %s" , * snapshot .Status .Error .Message )
630
- return snapshot , nil
647
+ return snapshot , snapshotter . SnapshottingNoChange , nil
631
648
}
632
649
633
650
// If PVC is not being deleted and finalizer is not added yet, a finalizer should be added.
634
651
klog .V (5 ).Infof ("createSnapshotOperation: Check if PVC is not being deleted and add Finalizer for source of snapshot [%s] if needed" , snapshot .Name )
635
652
err := ctrl .ensureSnapshotSourceFinalizer (snapshot )
636
653
if err != nil {
637
654
klog .Errorf ("createSnapshotOperation failed to add finalizer for source of snapshot %s" , err )
638
- return nil , err
655
+ return nil , snapshotter . SnapshottingNoChange , err
639
656
}
640
657
641
658
class , volume , contentName , snapshotterSecretRef , err := ctrl .getCreateSnapshotInput (snapshot )
642
659
if err != nil {
643
- return nil , fmt .Errorf ("failed to get input parameters to create snapshot %s: %q" , snapshot .Name , err )
660
+ return nil , snapshotter . SnapshottingNoChange , fmt .Errorf ("failed to get input parameters to create snapshot %s: %q" , snapshot .Name , err )
644
661
}
645
662
646
663
snapshotterCredentials , err := getCredentials (ctrl .client , snapshotterSecretRef )
647
664
if err != nil {
648
- return nil , err
665
+ return nil , snapshotter . SnapshottingNoChange , err
649
666
}
650
667
651
- driverName , snapshotID , creationTime , size , readyToUse , err := ctrl .handler .CreateSnapshot (snapshot , volume , class .Parameters , snapshotterCredentials )
668
+ driverName , snapshotID , creationTime , size , readyToUse , state , err := ctrl .handler .CreateSnapshot (snapshot , volume , class .Parameters , snapshotterCredentials )
652
669
if err != nil {
653
- return nil , fmt .Errorf ("failed to take snapshot of the volume, %s: %q" , volume .Name , err )
670
+ return nil , state , fmt .Errorf ("failed to take snapshot of the volume, %s: %q" , volume .Name , err )
654
671
}
655
672
656
673
klog .V (5 ).Infof ("Created snapshot: driver %s, snapshotId %s, creationTime %v, size %d, readyToUse %t" , driverName , snapshotID , creationTime , size , readyToUse )
@@ -667,12 +684,12 @@ func (ctrl *csiSnapshotController) createSnapshotOperation(snapshot *crdv1.Volum
667
684
}
668
685
669
686
if err != nil {
670
- return nil , err
687
+ return nil , snapshotter . SnapshottingInBackground , err
671
688
}
672
689
// Create VolumeSnapshotContent in the database
673
690
snapshotRef , err := ref .GetReference (scheme .Scheme , snapshot )
674
691
if err != nil {
675
- return nil , err
692
+ return nil , snapshotter . SnapshottingInBackground , err
676
693
}
677
694
678
695
timestamp := creationTime .UnixNano ()
@@ -730,9 +747,10 @@ func (ctrl *csiSnapshotController) createSnapshotOperation(snapshot *crdv1.Volum
730
747
strerr := fmt .Sprintf ("Error creating volume snapshot content object for snapshot %s: %v." , snapshotKey (snapshot ), err )
731
748
klog .Error (strerr )
732
749
ctrl .eventRecorder .Event (newSnapshot , v1 .EventTypeWarning , "CreateSnapshotContentFailed" , strerr )
733
- return nil , newControllerUpdateError (snapshotKey (snapshot ), err .Error ())
750
+ return nil , snapshotter . SnapshottingInBackground , newControllerUpdateError (snapshotKey (snapshot ), err .Error ())
734
751
}
735
- return newSnapshot , nil
752
+
753
+ return newSnapshot , snapshotter .SnapshottingFinished , nil
736
754
}
737
755
738
756
// Delete a snapshot
0 commit comments