Skip to content

Commit bbc5dc0

Browse files
committed
Remove VolumeSnapshotBeingCreated annotation after driver response
1 parent 8a46721 commit bbc5dc0

File tree

2 files changed

+54
-35
lines changed

2 files changed

+54
-35
lines changed

pkg/sidecar-controller/snapshot_controller.go

+51-28
Original file line numberDiff line numberDiff line change
@@ -332,25 +332,25 @@ func (ctrl *csiSnapshotSideCarController) createSnapshotOperation(content *crdv1
332332
}
333333

334334
// 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
336336
// 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
338338
// 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
340340
// resources on the storage system
341-
err = ctrl.setAnnVolumeSnapshotBeingCreated(content, utils.AnnVolumeSnapshotBeingCreated_Yes)
341+
err = ctrl.setAnnVolumeSnapshotBeingCreated(content)
342342
if err != nil {
343343
return nil, fmt.Errorf("failed to set VolumeSnapshotBeingCreated annotation to Yes on the content %s: %q", content.Name, err)
344344
}
345345

346346
driverName, snapshotID, creationTime, size, readyToUse, err := ctrl.handler.CreateSnapshot(content, class.Parameters, snapshotterCredentials)
347347
if err != nil {
348348
// 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
350350
// storage system has responded with an error
351351
errStr := fmt.Sprintf("%q", err)
352352
if !strings.Contains(errStr, "DeadlineExceeded") {
353-
err = ctrl.setAnnVolumeSnapshotBeingCreated(content, utils.AnnVolumeSnapshotBeingCreated_No)
353+
err = ctrl.removeAnnVolumeSnapshotBeingCreated(content)
354354
if err != nil {
355355
return nil, fmt.Errorf("failed to set VolumeSnapshotBeingCreated annotation to No on the content %s: %q", content.Name, err)
356356
}
@@ -362,14 +362,6 @@ func (ctrl *csiSnapshotSideCarController) createSnapshotOperation(content *crdv1
362362
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)
363363
}
364364

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-
373365
klog.V(5).Infof("Created snapshot: driver %s, snapshotId %s, creationTime %v, size %d, readyToUse %t", driverName, snapshotID, creationTime, size, readyToUse)
374366

375367
timestamp := creationTime.UnixNano()
@@ -381,6 +373,14 @@ func (ctrl *csiSnapshotSideCarController) createSnapshotOperation(content *crdv1
381373
content = newContent
382374
}
383375

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+
384384
// Update content in the cache store
385385
_, err = ctrl.storeContentUpdate(content)
386386
if err != nil {
@@ -607,10 +607,10 @@ func (ctrl *csiSnapshotSideCarController) shouldDelete(content *crdv1.VolumeSnap
607607

608608
// NOTE(xyang): Handle create snapshot timeout
609609
// 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.
612612
// 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) {
614614
return false
615615
}
616616

@@ -623,17 +623,15 @@ func (ctrl *csiSnapshotSideCarController) shouldDelete(content *crdv1.VolumeSnap
623623

624624
// setAnnVolumeSnapshotBeingCreated sets VolumeSnapshotBeingCreated annotation
625625
// 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)
637635
if err != nil {
638636
return newControllerUpdateError(content.Name, err.Error())
639637
}
@@ -649,3 +647,28 @@ func (ctrl *csiSnapshotSideCarController) setAnnVolumeSnapshotBeingCreated(conte
649647
}
650648
return nil
651649
}
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+
}

pkg/utils/util.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,16 @@ const (
7979
AnnVolumeSnapshotBeingDeleted = "snapshot.storage.kubernetes.io/volumesnapshot-being-deleted"
8080

8181
// AnnVolumeSnapshotBeingCreated annotation applies to VolumeSnapshotContents.
82-
// If it is set and value is "yes", it indicates that the csi-snapshotter
82+
// If it is set, it indicates that the csi-snapshotter
8383
// sidecar has sent the create snapshot request to the storage system and
8484
// is waiting for a response of success or failure.
85-
// This annotation will be set to "no" if the driver's CreateSnapshot
85+
// This annotation will be removed if the driver's CreateSnapshot
8686
// CSI function returns success or failure. If the create snapshot
87-
// request times out, retry will happen and the annotation value will
88-
// stay as "yes".
87+
// request times out, retry will happen and the annotation will remain.
8988
// This only applies to dynamic provisioning of snapshots because
9089
// the create snapshot CSI method will not be called for pre-provisioned
9190
// snapshots.
9291
AnnVolumeSnapshotBeingCreated = "snapshot.storage.kubernetes.io/volumesnapshot-being-created"
93-
// VolumeSnapshotBeingCreated annotation values can be "yes" or "no"
94-
AnnVolumeSnapshotBeingCreated_Yes = "yes"
95-
AnnVolumeSnapshotBeingCreated_No = "no"
9692

9793
// Annotation for secret name and namespace will be added to the content
9894
// and used at snapshot content deletion time.

0 commit comments

Comments
 (0)