Skip to content

Commit ea11bc3

Browse files
Merge pull request kubernetes-csi#17 from huffmanca/BZ1808123
Bug 1808123: UPSTREAM 275: Allows VolumeSnapshot to be deleted if the class isn't found
2 parents 8c40671 + 2fcb633 commit ea11bc3

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

pkg/common-controller/snapshot_controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
crdv1 "github.com/kubernetes-csi/external-snapshotter/pkg/apis/volumesnapshot/v1beta1"
2525
"github.com/kubernetes-csi/external-snapshotter/pkg/utils"
26-
"k8s.io/api/core/v1"
26+
v1 "k8s.io/api/core/v1"
2727
storagev1 "k8s.io/api/storage/v1"
2828
apierrs "k8s.io/apimachinery/pkg/api/errors"
2929
"k8s.io/apimachinery/pkg/api/resource"
@@ -1092,7 +1092,7 @@ func (ctrl *csiSnapshotCommonController) getSnapshotClass(className string) (*cr
10921092
class, err := ctrl.classLister.Get(className)
10931093
if err != nil {
10941094
klog.Errorf("failed to retrieve snapshot class %s from the informer: %q", className, err)
1095-
return nil, fmt.Errorf("failed to retrieve snapshot class %s from the informer: %q", className, err)
1095+
return nil, err
10961096
}
10971097

10981098
return class, nil

pkg/common-controller/snapshot_controller_base.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
storagelisters "github.com/kubernetes-csi/external-snapshotter/pkg/client/listers/volumesnapshot/v1beta1"
2727
"github.com/kubernetes-csi/external-snapshotter/pkg/utils"
2828

29-
"k8s.io/api/core/v1"
29+
v1 "k8s.io/api/core/v1"
3030
"k8s.io/apimachinery/pkg/api/errors"
3131
"k8s.io/apimachinery/pkg/labels"
3232
"k8s.io/apimachinery/pkg/util/wait"
@@ -215,8 +215,11 @@ func (ctrl *csiSnapshotCommonController) snapshotWorker() {
215215
// The volume snapshot still exists in informer cache, the event must have
216216
// been add/update/sync
217217
newSnapshot, err := ctrl.checkAndUpdateSnapshotClass(snapshot)
218-
if err == nil {
219-
klog.V(5).Infof("passed checkAndUpdateSnapshotClass for snapshot %q", key)
218+
if err == nil || (newSnapshot.ObjectMeta.DeletionTimestamp != nil && errors.IsNotFound(err)) {
219+
// If the VolumeSnapshotClass is not found, we still need to process an update
220+
// so that syncSnapshot can delete the snapshot, should it still exist in the
221+
// cluster after it's been removed from the informer cache
222+
klog.V(5).Infof("updating snapshot %q; snapshotClass may have already been removed", key)
220223
ctrl.updateSnapshot(newSnapshot)
221224
}
222225
return false
@@ -243,7 +246,10 @@ func (ctrl *csiSnapshotCommonController) snapshotWorker() {
243246
return false
244247
}
245248
newSnapshot, err := ctrl.checkAndUpdateSnapshotClass(snapshot)
246-
if err == nil {
249+
if err == nil || errors.IsNotFound(err) {
250+
// We should still handle deletion events even if the VolumeSnapshotClass
251+
// is not found in the cluster
252+
klog.V(5).Infof("deleting snapshot %q; snapshotClass may have already been removed", key)
247253
ctrl.deleteSnapshot(newSnapshot)
248254
}
249255
return false
@@ -329,7 +335,8 @@ func (ctrl *csiSnapshotCommonController) checkAndUpdateSnapshotClass(snapshot *c
329335
if err != nil {
330336
klog.Errorf("checkAndUpdateSnapshotClass failed to getSnapshotClass %v", err)
331337
ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "GetSnapshotClassFailed", fmt.Sprintf("Failed to get snapshot class with error %v", err))
332-
return nil, err
338+
// we need to return the original snapshot even if the class isn't found, as it may need to be deleted
339+
return newSnapshot, err
333340
}
334341
} else {
335342
klog.V(5).Infof("checkAndUpdateSnapshotClass [%s]: SetDefaultSnapshotClass", snapshot.Name)

pkg/common-controller/snapshot_create_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestCreateSnapshotSync(t *testing.T) {
9494
initialContents: nocontents,
9595
expectedContents: nocontents,
9696
initialSnapshots: newSnapshotArray("snap7-1", "snapuid7-1", "claim7-1", "", classNonExisting, "", &False, nil, nil, nil, false, true, nil),
97-
expectedSnapshots: newSnapshotArray("snap7-1", "snapuid7-1", "claim7-1", "", classNonExisting, "", &False, nil, nil, newVolumeError("Failed to create snapshot content with error failed to get input parameters to create snapshot snap7-1: \"failed to retrieve snapshot class non-existing from the informer: \\\"volumesnapshotclass.snapshot.storage.k8s.io \\\\\\\"non-existing\\\\\\\" not found\\\"\""), false, true, nil),
97+
expectedSnapshots: newSnapshotArray("snap7-1", "snapuid7-1", "claim7-1", "", classNonExisting, "", &False, nil, nil, newVolumeError("Failed to create snapshot content with error failed to get input parameters to create snapshot snap7-1: \"volumesnapshotclass.snapshot.storage.k8s.io \\\"non-existing\\\" not found\""), false, true, nil),
9898
initialClaims: newClaimArray("claim7-1", "pvc-uid7-1", "1Gi", "volume7-1", v1.ClaimBound, &classEmpty),
9999
initialVolumes: newVolumeArray("volume7-1", "pv-uid7-1", "pv-handle7-1", "1Gi", "pvc-uid7-1", "claim7-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
100100
expectedEvents: []string{"Warning SnapshotContentCreationFailed"},

pkg/common-controller/snapshotclass_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestUpdateSnapshotClass(t *testing.T) {
6363
name: "1-3 - snapshot class name not found",
6464
initialContents: nocontents,
6565
initialSnapshots: newSnapshotArray("snap1-1", "snapuid1-1", "claim1-1", "content1-1", "missing-class", "content1-1", &True, nil, nil, nil, false, true, nil),
66-
expectedSnapshots: newSnapshotArray("snap1-1", "snapuid1-1", "claim1-1", "content1-1", "missing-class", "content1-1", &False, nil, nil, newVolumeError("Failed to get snapshot class with error failed to retrieve snapshot class missing-class from the informer: \"volumesnapshotclass.snapshot.storage.k8s.io \\\"missing-class\\\" not found\""), false, true, nil),
66+
expectedSnapshots: newSnapshotArray("snap1-1", "snapuid1-1", "claim1-1", "content1-1", "missing-class", "content1-1", &False, nil, nil, newVolumeError("Failed to get snapshot class with error volumesnapshotclass.snapshot.storage.k8s.io \"missing-class\" not found"), false, true, nil),
6767
initialClaims: newClaimArray("claim1-1", "pvc-uid1-1", "1Gi", "volume1-1", v1.ClaimBound, &sameDriver),
6868
initialVolumes: newVolumeArray("volume1-1", "pv-uid1-1", "pv-handle1-1", "1Gi", "pvc-uid1-1", "claim1-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
6969
initialStorageClasses: []*storage.StorageClass{sameDriverStorageClass},

0 commit comments

Comments
 (0)