Skip to content
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

Check whether the VolumeSnapshot's source PVC is nil before using it #7515

Merged
merged 2 commits into from
Mar 14, 2024
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 changelogs/unreleased/7515-blackpiglet
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Check whether the VolumeSnapshot's source PVC is nil before using it.
Skip populate VolumeInfo for data-moved PV when CSI is not enabled.
6 changes: 6 additions & 0 deletions internal/volume/volumes_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
velerov2alpha1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v2alpha1"
"github.com/vmware-tanzu/velero/pkg/features"
"github.com/vmware-tanzu/velero/pkg/itemoperation"
"github.com/vmware-tanzu/velero/pkg/kuberesource"
"github.com/vmware-tanzu/velero/pkg/plugin/velero"
Expand Down Expand Up @@ -462,6 +463,11 @@

// generateVolumeInfoFromDataUpload generate VolumeInfo for DataUpload.
func (v *VolumesInformation) generateVolumeInfoFromDataUpload() {
if !features.IsEnabled(velerov1api.CSIFeatureFlag) {
v.logger.Debug("Skip generating VolumeInfo when the CSI feature is disabled.")
return
}

Check warning on line 469 in internal/volume/volumes_information.go

View check run for this annotation

Codecov / codecov/patch

internal/volume/volumes_information.go#L467-L469

Added lines #L467 - L469 were not covered by tests

tmpVolumeInfos := make([]*VolumeInfo, 0)
vsClassList := new(snapshotv1api.VolumeSnapshotClassList)
if err := v.crClient.List(context.TODO(), vsClassList); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/volume/volumes_information_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
velerov2alpha1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v2alpha1"
"github.com/vmware-tanzu/velero/pkg/builder"
"github.com/vmware-tanzu/velero/pkg/features"
"github.com/vmware-tanzu/velero/pkg/itemoperation"
"github.com/vmware-tanzu/velero/pkg/plugin/velero"
velerotest "github.com/vmware-tanzu/velero/pkg/test"
Expand Down Expand Up @@ -605,6 +606,8 @@ func TestGenerateVolumeInfoFromPVB(t *testing.T) {
}

func TestGenerateVolumeInfoFromDataUpload(t *testing.T) {
features.Enable(velerov1api.CSIFeatureFlag)
defer features.Disable(velerov1api.CSIFeatureFlag)
now := metav1.Now()
tests := []struct {
name string
Expand Down
5 changes: 5 additions & 0 deletions pkg/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -1955,6 +1955,11 @@ func hasCSIVolumeSnapshot(ctx *restoreContext, unstructuredPV *unstructured.Unst
}

for _, vs := range ctx.csiVolumeSnapshots {
// In some error cases, the VSs' source PVC could be nil. Skip them.
if vs.Spec.Source.PersistentVolumeClaimName == nil {
continue
}

if pv.Spec.ClaimRef.Name == *vs.Spec.Source.PersistentVolumeClaimName &&
pv.Spec.ClaimRef.Namespace == vs.Namespace {
return true
Expand Down
15 changes: 15 additions & 0 deletions pkg/restore/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4044,6 +4044,21 @@ func TestHasCSIVolumeSnapshot(t *testing.T) {
},
expectedResult: false,
},
{
name: "VS's source PVC is nil, expect false",
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": "PersistentVolume",
"apiVersion": "v1",
"metadata": map[string]interface{}{
"namespace": "default",
"name": "test",
},
},
},
vs: builder.ForVolumeSnapshot("velero", "test").Result(),
expectedResult: false,
},
{
name: "Find VS, expect true.",
obj: &unstructured.Unstructured{
Expand Down
Loading