Skip to content

Commit ac3ef99

Browse files
authored
Merge pull request #275 from huffmanca/update_snapshotclass_deletion
Allows VolumeSnapshot to be deleted if the class isn't found
2 parents bf65357 + 91a55c0 commit ac3ef99

5 files changed

+16
-9
lines changed

pkg/common-controller/snapshot_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ func (ctrl *csiSnapshotCommonController) getSnapshotClass(className string) (*cr
10761076
class, err := ctrl.classLister.Get(className)
10771077
if err != nil {
10781078
klog.Errorf("failed to retrieve snapshot class %s from the informer: %q", className, err)
1079-
return nil, fmt.Errorf("failed to retrieve snapshot class %s from the informer: %q", className, err)
1079+
return nil, err
10801080
}
10811081

10821082
return class, nil

pkg/common-controller/snapshot_controller_base.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
storagelisters "github.com/kubernetes-csi/external-snapshotter/v2/pkg/client/listers/volumesnapshot/v1beta1"
2727
"github.com/kubernetes-csi/external-snapshotter/v2/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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func TestCreateSnapshotSync(t *testing.T) {
9999
initialContents: nocontents,
100100
expectedContents: nocontents,
101101
initialSnapshots: newSnapshotArray("snap7-1", "snapuid7-1", "claim7-1", "", classNonExisting, "", &False, nil, nil, nil, false, true, nil),
102-
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),
102+
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),
103103
initialClaims: newClaimArray("claim7-1", "pvc-uid7-1", "1Gi", "volume7-1", v1.ClaimBound, &classEmpty),
104104
initialVolumes: newVolumeArray("volume7-1", "pv-uid7-1", "pv-handle7-1", "1Gi", "pvc-uid7-1", "claim7-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
105105
expectedEvents: []string{"Warning SnapshotContentCreationFailed"},

pkg/common-controller/snapshot_update_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func TestSync(t *testing.T) {
146146
initialContents: nocontents,
147147
expectedContents: nocontents,
148148
initialSnapshots: newSnapshotArray("snap7-1", "snapuid7-1", "claim7-1", "", classNonExisting, "", &False, nil, nil, nil, false, true, nil),
149-
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),
149+
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),
150150
initialClaims: newClaimArray("claim7-1", "pvc-uid7-1", "1Gi", "volume7-1", v1.ClaimBound, &classEmpty),
151151
initialVolumes: newVolumeArray("volume7-1", "pv-uid7-1", "pv-handle7-1", "1Gi", "pvc-uid7-1", "claim7-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
152152
expectedEvents: []string{"Warning SnapshotContentCreationFailed"},

pkg/common-controller/snapshotclass_test.go

Lines changed: 1 addition & 1 deletion
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)