Skip to content

Commit 3682428

Browse files
authored
Merge pull request #416 from xing-yang/cherrypick_3.0.2
Cherry-pick the fix for panic when source PVC does not exist
2 parents 62edbf2 + 586045c commit 3682428

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

CHANGELOG/CHANGELOG-3.0.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
- Cherry-pick from https://github.com/kubernetes-csi/external-snapshotter/pull/413. Bug fix to allow creation of snapshot content if pvc finalizer exists, even if pvc is marked for deletion. ([#414](https://github.com/kubernetes-csi/external-snapshotter/pull/414), [@RaunakShah](https://github.com/RaunakShah))
1212

13+
- Cherry-pick from https://github.com/kubernetes-csi/external-snapshotter/pull/381. Fixed crash of snapshot-controller when source PVC of a snapshot to take does not exist. ([#415](https://github.com/kubernetes-csi/external-snapshotter/pull/415), [@jsafrane](https://github.com/jsafrane))
14+
1315
## Dependencies
1416

1517
### Added

pkg/common-controller/framework_test.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,21 @@ type reactorError struct {
163163
error error
164164
}
165165

166+
// testError is an error returned from a test that marks a test as failed even
167+
// though the test case itself expected a common error (such as API error)
168+
type testError string
169+
170+
func (t testError) Error() string {
171+
return string(t)
172+
}
173+
174+
var _ error = testError("foo")
175+
176+
func isTestError(err error) bool {
177+
_, ok := err.(testError)
178+
return ok
179+
}
180+
166181
func withSnapshotFinalizers(snapshots []*crdv1.VolumeSnapshot, finalizers ...string) []*crdv1.VolumeSnapshot {
167182
for i := range snapshots {
168183
for _, f := range finalizers {
@@ -1159,7 +1174,11 @@ func testRemoveSnapshotFinalizer(ctrl *csiSnapshotCommonController, reactor *sna
11591174
}
11601175

11611176
func testUpdateSnapshotClass(ctrl *csiSnapshotCommonController, reactor *snapshotReactor, test controllerTest) error {
1162-
_, err := ctrl.checkAndUpdateSnapshotClass(test.initialSnapshots[0])
1177+
snap, err := ctrl.checkAndUpdateSnapshotClass(test.initialSnapshots[0])
1178+
// syncSnapshotByKey expects that checkAndUpdateSnapshotClass always returns a snapshot
1179+
if snap == nil {
1180+
return testError(fmt.Sprintf("checkAndUpdateSnapshotClass returned nil snapshot on error: %v", err))
1181+
}
11631182
return err
11641183
}
11651184

@@ -1497,6 +1516,9 @@ func runUpdateSnapshotClassTests(t *testing.T, tests []controllerTest, snapshotC
14971516

14981517
// Run the tested functions
14991518
err = test.test(ctrl, reactor, test)
1519+
if err != nil && isTestError(err) {
1520+
t.Errorf("Test %q failed: %v", test.name, err)
1521+
}
15001522
if test.expectSuccess && err != nil {
15011523
t.Errorf("Test %q failed: %v", test.name, err)
15021524
}

pkg/common-controller/snapshot_controller_base.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ func (ctrl *csiSnapshotCommonController) syncContentByKey(key string) error {
317317

318318
// checkAndUpdateSnapshotClass gets the VolumeSnapshotClass from VolumeSnapshot. If it is not set,
319319
// gets it from default VolumeSnapshotClass and sets it.
320+
// On error, it must return the original snapshot, not nil, because the caller syncContentByKey
321+
// needs to check snapshot's timestamp.
320322
func (ctrl *csiSnapshotCommonController) checkAndUpdateSnapshotClass(snapshot *crdv1.VolumeSnapshot) (*crdv1.VolumeSnapshot, error) {
321323
className := snapshot.Spec.VolumeSnapshotClassName
322324
var class *crdv1.VolumeSnapshotClass
@@ -337,7 +339,7 @@ func (ctrl *csiSnapshotCommonController) checkAndUpdateSnapshotClass(snapshot *c
337339
if err != nil {
338340
klog.Errorf("checkAndUpdateSnapshotClass failed to setDefaultClass %v", err)
339341
ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "SetDefaultSnapshotClassFailed", fmt.Sprintf("Failed to set default snapshot class with error %v", err))
340-
return nil, err
342+
return snapshot, err
341343
}
342344
}
343345

pkg/common-controller/snapshotclass_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ func TestUpdateSnapshotClass(t *testing.T) {
8686
},
8787
test: testUpdateSnapshotClass,
8888
},
89+
{
90+
// PVC does not exist
91+
name: "1-5 - snapshot update with default class name failed because PVC was not found",
92+
initialContents: nocontents,
93+
initialSnapshots: newSnapshotArray("snap1-5", "snapuid1-5", "claim1-5", "", "", "", &True, nil, nil, nil, false, true, nil),
94+
expectedSnapshots: newSnapshotArray("snap1-5", "snapuid1-5", "claim1-5", "", "", "", &False, nil, nil, newVolumeError("Failed to set default snapshot class with error failed to retrieve PVC claim1-5 from the lister: \"persistentvolumeclaim \\\"claim1-5\\\" not found\""), false, true, nil),
95+
initialClaims: nil,
96+
initialVolumes: nil,
97+
initialStorageClasses: []*storage.StorageClass{},
98+
expectedEvents: []string{"Warning SetDefaultSnapshotClassFailed"},
99+
errors: noerrors,
100+
test: testUpdateSnapshotClass,
101+
},
89102
}
90103

91104
runUpdateSnapshotClassTests(t, tests, snapshotClasses)

0 commit comments

Comments
 (0)