Fix panic when source PVC does not exist

checkAndUpdateSnapshotClass must always return a snapshot, even though it
fails somewhere in ctrl.SetDefaultSnapshotClass.

Add unit tests for that.
This commit is contained in:
Jan Safranek
2020-09-29 14:53:48 +02:00
parent e02d45a6a6
commit 73f72bd7b6
3 changed files with 39 additions and 2 deletions

View File

@@ -317,6 +317,8 @@ func (ctrl *csiSnapshotCommonController) syncContentByKey(key string) error {
// checkAndUpdateSnapshotClass gets the VolumeSnapshotClass from VolumeSnapshot. If it is not set,
// gets it from default VolumeSnapshotClass and sets it.
// On error, it must return the original snapshot, not nil, because the caller syncContentByKey
// needs to check snapshot's timestamp.
func (ctrl *csiSnapshotCommonController) checkAndUpdateSnapshotClass(snapshot *crdv1.VolumeSnapshot) (*crdv1.VolumeSnapshot, error) {
className := snapshot.Spec.VolumeSnapshotClassName
var class *crdv1.VolumeSnapshotClass
@@ -337,7 +339,7 @@ func (ctrl *csiSnapshotCommonController) checkAndUpdateSnapshotClass(snapshot *c
if err != nil {
klog.Errorf("checkAndUpdateSnapshotClass failed to setDefaultClass %v", err)
ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "SetDefaultSnapshotClassFailed", fmt.Sprintf("Failed to set default snapshot class with error %v", err))
return nil, err
return snapshot, err
}
}