@@ -332,25 +332,25 @@ func (ctrl *csiSnapshotSideCarController) createSnapshotOperation(content *crdv1
332
332
}
333
333
334
334
// NOTE(xyang): handle create timeout
335
- // Add annotation and set to Yes to indicate create snapshot request is
335
+ // Add annotation to indicate create snapshot request is
336
336
// sent to the storage system and wait for a response of success or failure.
337
- // Annotation will be set to No only after storage system has responded
337
+ // Annotation will be removed only after storage system has responded
338
338
// with success or failure. If the request times out, retry will happen
339
- // and annotation will stay as Yes to avoid leaking of snapshot
339
+ // and annotation will remain to avoid leaking of snapshot
340
340
// resources on the storage system
341
- err = ctrl .setAnnVolumeSnapshotBeingCreated (content , utils . AnnVolumeSnapshotBeingCreated_Yes )
341
+ err = ctrl .setAnnVolumeSnapshotBeingCreated (content )
342
342
if err != nil {
343
343
return nil , fmt .Errorf ("failed to set VolumeSnapshotBeingCreated annotation to Yes on the content %s: %q" , content .Name , err )
344
344
}
345
345
346
346
driverName , snapshotID , creationTime , size , readyToUse , err := ctrl .handler .CreateSnapshot (content , class .Parameters , snapshotterCredentials )
347
347
if err != nil {
348
348
// NOTE(xyang): handle create timeout
349
- // If it is not a timeout error, set annotation to No to indicate
349
+ // If it is not a timeout error, remove annotation to indicate
350
350
// storage system has responded with an error
351
351
errStr := fmt .Sprintf ("%q" , err )
352
352
if ! strings .Contains (errStr , "DeadlineExceeded" ) {
353
- err = ctrl .setAnnVolumeSnapshotBeingCreated (content , utils . AnnVolumeSnapshotBeingCreated_No )
353
+ err = ctrl .removeAnnVolumeSnapshotBeingCreated (content )
354
354
if err != nil {
355
355
return nil , fmt .Errorf ("failed to set VolumeSnapshotBeingCreated annotation to No on the content %s: %q" , content .Name , err )
356
356
}
@@ -362,14 +362,6 @@ func (ctrl *csiSnapshotSideCarController) createSnapshotOperation(content *crdv1
362
362
return nil , fmt .Errorf ("failed to take snapshot of the volume, %s: driver name %s returned from the driver is different from driver %s in snapshot class" , * content .Spec .Source .VolumeHandle , driverName , class .Driver )
363
363
}
364
364
365
- // NOTE(xyang): handle create timeout
366
- // Set annotation to No to indicate storage system has successfully
367
- // cut the snapshot
368
- err = ctrl .setAnnVolumeSnapshotBeingCreated (content , utils .AnnVolumeSnapshotBeingCreated_No )
369
- if err != nil {
370
- return nil , fmt .Errorf ("failed to set VolumeSnapshotBeingCreated annotation to No on the content %s: %q" , content .Name , err )
371
- }
372
-
373
365
klog .V (5 ).Infof ("Created snapshot: driver %s, snapshotId %s, creationTime %v, size %d, readyToUse %t" , driverName , snapshotID , creationTime , size , readyToUse )
374
366
375
367
timestamp := creationTime .UnixNano ()
@@ -381,6 +373,14 @@ func (ctrl *csiSnapshotSideCarController) createSnapshotOperation(content *crdv1
381
373
content = newContent
382
374
}
383
375
376
+ // NOTE(xyang): handle create timeout
377
+ // Remove annotation to indicate storage system has successfully
378
+ // cut the snapshot
379
+ err = ctrl .removeAnnVolumeSnapshotBeingCreated (content )
380
+ if err != nil {
381
+ return nil , fmt .Errorf ("failed to set VolumeSnapshotBeingCreated annotation to No on the content %s: %q" , content .Name , err )
382
+ }
383
+
384
384
// Update content in the cache store
385
385
_ , err = ctrl .storeContentUpdate (content )
386
386
if err != nil {
@@ -607,10 +607,10 @@ func (ctrl *csiSnapshotSideCarController) shouldDelete(content *crdv1.VolumeSnap
607
607
608
608
// NOTE(xyang): Handle create snapshot timeout
609
609
// 2) shouldDelete returns false if AnnVolumeSnapshotBeingCreated
610
- // annotation is set and its value is Yes . This indicates a
611
- // CreateSnapshot CSI RPC has not responded with success or failure.
610
+ // annotation is set. This indicates a CreateSnapshot CSI RPC has
611
+ // not responded with success or failure.
612
612
// We need to keep waiting for a response from the CSI driver.
613
- if metav1 .HasAnnotation (content .ObjectMeta , utils .AnnVolumeSnapshotBeingCreated ) && content . ObjectMeta . Annotations [ utils . AnnVolumeSnapshotBeingCreated ] == utils . AnnVolumeSnapshotBeingCreated_Yes {
613
+ if metav1 .HasAnnotation (content .ObjectMeta , utils .AnnVolumeSnapshotBeingCreated ) {
614
614
return false
615
615
}
616
616
@@ -623,17 +623,15 @@ func (ctrl *csiSnapshotSideCarController) shouldDelete(content *crdv1.VolumeSnap
623
623
624
624
// setAnnVolumeSnapshotBeingCreated sets VolumeSnapshotBeingCreated annotation
625
625
// on VolumeSnapshotContent
626
- // If beingCreated is true, it indicates snapshot is being created
627
- // If beingCreated if false, it indicates CreateSnapshot CSI RPC returns
628
- // success or failure
629
- func (ctrl * csiSnapshotSideCarController ) setAnnVolumeSnapshotBeingCreated (content * crdv1.VolumeSnapshotContent , annBeingCreated string ) error {
630
- // Set AnnVolumeSnapshotBeingCreated if it is not set yet or if it is
631
- // set but has a different value
632
- if ! metav1 .HasAnnotation (content .ObjectMeta , utils .AnnVolumeSnapshotBeingCreated ) || content .ObjectMeta .Annotations [utils .AnnVolumeSnapshotBeingCreated ] != annBeingCreated {
633
- klog .V (5 ).Infof ("setAnnVolumeSnapshotBeingCreated: set annotation [%s:%s] on content [%s]." , utils .AnnVolumeSnapshotBeingCreated , annBeingCreated , content .Name )
634
- metav1 .SetMetaDataAnnotation (& content .ObjectMeta , utils .AnnVolumeSnapshotBeingCreated , annBeingCreated )
635
-
636
- updateContent , err := ctrl .clientset .SnapshotV1beta1 ().VolumeSnapshotContents ().Update (content )
626
+ // If set, it indicates snapshot is being created
627
+ func (ctrl * csiSnapshotSideCarController ) setAnnVolumeSnapshotBeingCreated (content * crdv1.VolumeSnapshotContent ) error {
628
+ // Set AnnVolumeSnapshotBeingCreated if it is not set yet
629
+ if ! metav1 .HasAnnotation (content .ObjectMeta , utils .AnnVolumeSnapshotBeingCreated ) {
630
+ klog .V (5 ).Infof ("setAnnVolumeSnapshotBeingCreated: set annotation [%s:yes] on content [%s]." , utils .AnnVolumeSnapshotBeingCreated , content .Name )
631
+ contentClone := content .DeepCopy ()
632
+ metav1 .SetMetaDataAnnotation (& contentClone .ObjectMeta , utils .AnnVolumeSnapshotBeingCreated , "yes" )
633
+
634
+ updateContent , err := ctrl .clientset .SnapshotV1beta1 ().VolumeSnapshotContents ().Update (contentClone )
637
635
if err != nil {
638
636
return newControllerUpdateError (content .Name , err .Error ())
639
637
}
@@ -649,3 +647,28 @@ func (ctrl *csiSnapshotSideCarController) setAnnVolumeSnapshotBeingCreated(conte
649
647
}
650
648
return nil
651
649
}
650
+
651
+ // removeAnnVolumeSnapshotBeingCreated removes the VolumeSnapshotBeingCreated
652
+ // annotation from a content if there exists one.
653
+ func (ctrl csiSnapshotSideCarController ) removeAnnVolumeSnapshotBeingCreated (content * crdv1.VolumeSnapshotContent ) error {
654
+ if ! metav1 .HasAnnotation (content .ObjectMeta , utils .AnnVolumeSnapshotBeingCreated ) {
655
+ // the annotation does not exit, return directly
656
+ return nil
657
+ }
658
+ contentClone := content .DeepCopy ()
659
+ delete (contentClone .ObjectMeta .Annotations , utils .AnnVolumeSnapshotBeingCreated )
660
+
661
+ updateContent , err := ctrl .clientset .SnapshotV1beta1 ().VolumeSnapshotContents ().Update (contentClone )
662
+ if err != nil {
663
+ return newControllerUpdateError (content .Name , err .Error ())
664
+ }
665
+ // update content if update is successful
666
+ content = updateContent
667
+
668
+ klog .V (5 ).Infof ("Removed VolumeSnapshotBeingCreated annotation from volume snapshot content %s" , content .Name )
669
+ _ , err = ctrl .storeContentUpdate (content )
670
+ if err != nil {
671
+ klog .Errorf ("failed to update content store %v" , err )
672
+ }
673
+ return nil
674
+ }
0 commit comments