Skip to content

Commit 3dd4e07

Browse files
authored
Merge pull request #263 from ggriffiths/common_controller_uts_2
Fix error propagation in checkandAddSnapshotFinalizers and add tests
2 parents 33d4ae2 + 9830b6e commit 3dd4e07

File tree

5 files changed

+218
-340
lines changed

5 files changed

+218
-340
lines changed

pkg/common-controller/framework_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,6 @@ func withSnapshotFinalizers(snapshots []*crdv1.VolumeSnapshot, finalizers ...str
173173
return snapshots
174174
}
175175

176-
func withContentFinalizer(content *crdv1.VolumeSnapshotContent) *crdv1.VolumeSnapshotContent {
177-
content.ObjectMeta.Finalizers = append(content.ObjectMeta.Finalizers, utils.VolumeSnapshotContentFinalizer)
178-
return content
179-
}
180-
181176
func withPVCFinalizer(pvc *v1.PersistentVolumeClaim) *v1.PersistentVolumeClaim {
182177
pvc.ObjectMeta.Finalizers = append(pvc.ObjectMeta.Finalizers, utils.PVCFinalizer)
183178
return pvc
@@ -837,6 +832,18 @@ func withContentAnnotations(contents []*crdv1.VolumeSnapshotContent, annotations
837832
return contents
838833
}
839834

835+
func withContentSpecSnapshotClassName(contents []*crdv1.VolumeSnapshotContent, volumeSnapshotClassName *string) []*crdv1.VolumeSnapshotContent {
836+
for i := range contents {
837+
contents[i].Spec.VolumeSnapshotClassName = volumeSnapshotClassName
838+
}
839+
return contents
840+
}
841+
842+
func withContentFinalizer(content *crdv1.VolumeSnapshotContent) *crdv1.VolumeSnapshotContent {
843+
content.ObjectMeta.Finalizers = append(content.ObjectMeta.Finalizers, utils.VolumeSnapshotContentFinalizer)
844+
return content
845+
}
846+
840847
func newContentArray(contentName, boundToSnapshotUID, boundToSnapshotName, snapshotHandle, snapshotClassName, desiredSnapshotHandle, volumeHandle string,
841848
deletionPolicy crdv1.DeletionPolicy, size, creationTime *int64,
842849
withFinalizer bool) []*crdv1.VolumeSnapshotContent {

pkg/common-controller/snapshot_controller.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ func (ctrl *csiSnapshotCommonController) syncSnapshot(snapshot *crdv1.VolumeSnap
173173
}
174174
} else {
175175
klog.V(5).Infof("syncSnapshot[%s]: check if we should add finalizers on snapshot", utils.SnapshotKey(snapshot))
176-
ctrl.checkandAddSnapshotFinalizers(snapshot)
176+
if err := ctrl.checkandAddSnapshotFinalizers(snapshot); err != nil {
177+
klog.Errorf("error check and add Snapshot finalizers for snapshot [%s]: %v", snapshot.Name, errFinalizer)
178+
ctrl.eventRecorder.Event(snapshot, v1.EventTypeWarning, "SnapshotCheckandUpdateFailed", fmt.Sprintf("Failed to check and update snapshot: %s", err.Error()))
179+
return err
180+
}
177181
// Need to build or update snapshot.Status in following cases:
178182
// 1) snapshot.Status is nil
179183
// 2) snapshot.Status.ReadyToUse is false
@@ -301,7 +305,7 @@ func (ctrl *csiSnapshotCommonController) checkandAddSnapshotFinalizers(snapshot
301305
if addSourceFinalizer || addBoundFinalizer {
302306
// Snapshot is not being deleted -> it should have the finalizer.
303307
klog.V(5).Infof("checkandAddSnapshotFinalizers: Add Finalizer for VolumeSnapshot[%s]", utils.SnapshotKey(snapshot))
304-
ctrl.addSnapshotFinalizer(snapshot, addSourceFinalizer, addBoundFinalizer)
308+
return ctrl.addSnapshotFinalizer(snapshot, addSourceFinalizer, addBoundFinalizer)
305309
}
306310
return nil
307311
}
@@ -665,26 +669,6 @@ func (ctrl *csiSnapshotCommonController) updateSnapshotErrorStatusWithEvent(snap
665669
return nil
666670
}
667671

668-
// isSnapshotConentBeingUsed checks if snapshot content is bound to snapshot.
669-
func (ctrl *csiSnapshotCommonController) isSnapshotContentBeingUsed(content *crdv1.VolumeSnapshotContent) bool {
670-
if content.Spec.VolumeSnapshotRef.Name != "" && content.Spec.VolumeSnapshotRef.Namespace != "" {
671-
snapshotObj, err := ctrl.clientset.SnapshotV1beta1().VolumeSnapshots(content.Spec.VolumeSnapshotRef.Namespace).Get(content.Spec.VolumeSnapshotRef.Name, metav1.GetOptions{})
672-
if err != nil {
673-
klog.Infof("isSnapshotContentBeingUsed: Cannot get snapshot %s from api server: [%v]. VolumeSnapshot object may be deleted already.", content.Spec.VolumeSnapshotRef.Name, err)
674-
return false
675-
}
676-
677-
// Check if the snapshot content is bound to the snapshot
678-
if utils.IsSnapshotBound(snapshotObj, content) {
679-
klog.Infof("isSnapshotContentBeingUsed: VolumeSnapshot %s is bound to volumeSnapshotContent [%s]", snapshotObj.Name, content.Name)
680-
return true
681-
}
682-
}
683-
684-
klog.V(5).Infof("isSnapshotContentBeingUsed: Snapshot content %s is not being used", content.Name)
685-
return false
686-
}
687-
688672
// addContentFinalizer adds a Finalizer for VolumeSnapshotContent.
689673
func (ctrl *csiSnapshotCommonController) addContentFinalizer(content *crdv1.VolumeSnapshotContent) error {
690674
contentClone := content.DeepCopy()
@@ -858,7 +842,7 @@ func (ctrl *csiSnapshotCommonController) checkandBindSnapshotContent(snapshot *c
858842
}
859843
newContent, err := ctrl.clientset.SnapshotV1beta1().VolumeSnapshotContents().Update(contentClone)
860844
if err != nil {
861-
klog.V(4).Infof("updating VolumeSnapshotContent[%s] error status failed %v", newContent.Name, err)
845+
klog.V(4).Infof("updating VolumeSnapshotContent[%s] error status failed %v", contentClone.Name, err)
862846
return nil, err
863847
}
864848

0 commit comments

Comments
 (0)