Skip to content

Cherry-pick the fix for panic when source PVC does not exist #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG/CHANGELOG-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

- 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))

- 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))

## Dependencies

### Added
Expand Down
24 changes: 23 additions & 1 deletion pkg/common-controller/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,21 @@ type reactorError struct {
error error
}

// testError is an error returned from a test that marks a test as failed even
// though the test case itself expected a common error (such as API error)
type testError string

func (t testError) Error() string {
return string(t)
}

var _ error = testError("foo")

func isTestError(err error) bool {
_, ok := err.(testError)
return ok
}

func withSnapshotFinalizers(snapshots []*crdv1.VolumeSnapshot, finalizers ...string) []*crdv1.VolumeSnapshot {
for i := range snapshots {
for _, f := range finalizers {
Expand Down Expand Up @@ -1159,7 +1174,11 @@ func testRemoveSnapshotFinalizer(ctrl *csiSnapshotCommonController, reactor *sna
}

func testUpdateSnapshotClass(ctrl *csiSnapshotCommonController, reactor *snapshotReactor, test controllerTest) error {
_, err := ctrl.checkAndUpdateSnapshotClass(test.initialSnapshots[0])
snap, err := ctrl.checkAndUpdateSnapshotClass(test.initialSnapshots[0])
// syncSnapshotByKey expects that checkAndUpdateSnapshotClass always returns a snapshot
if snap == nil {
return testError(fmt.Sprintf("checkAndUpdateSnapshotClass returned nil snapshot on error: %v", err))
}
return err
}

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

// Run the tested functions
err = test.test(ctrl, reactor, test)
if err != nil && isTestError(err) {
t.Errorf("Test %q failed: %v", test.name, err)
}
if test.expectSuccess && err != nil {
t.Errorf("Test %q failed: %v", test.name, err)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/common-controller/snapshot_controller_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/common-controller/snapshotclass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ func TestUpdateSnapshotClass(t *testing.T) {
},
test: testUpdateSnapshotClass,
},
{
// PVC does not exist
name: "1-5 - snapshot update with default class name failed because PVC was not found",
initialContents: nocontents,
initialSnapshots: newSnapshotArray("snap1-5", "snapuid1-5", "claim1-5", "", "", "", &True, nil, nil, nil, false, true, nil),
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),
initialClaims: nil,
initialVolumes: nil,
initialStorageClasses: []*storage.StorageClass{},
expectedEvents: []string{"Warning SetDefaultSnapshotClassFailed"},
errors: noerrors,
test: testUpdateSnapshotClass,
},
}

runUpdateSnapshotClassTests(t, tests, snapshotClasses)
Expand Down