Merge pull request #7515 from blackpiglet/7494_fix

Check whether the VolumeSnapshot's source PVC is nil before using it
This commit is contained in:
qiuming
2024-03-14 11:28:29 +08:00
committed by GitHub
5 changed files with 31 additions and 0 deletions

View File

@@ -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.

View File

@@ -28,6 +28,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/features"
"github.com/vmware-tanzu/velero/pkg/itemoperation"
"github.com/vmware-tanzu/velero/pkg/kuberesource"
"github.com/vmware-tanzu/velero/pkg/plugin/velero"
@@ -462,6 +463,11 @@ func (v *VolumesInformation) generateVolumeInfoFromPVB() {
// 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
}
tmpVolumeInfos := make([]*VolumeInfo, 0)
vsClassList := new(snapshotv1api.VolumeSnapshotClassList)
if err := v.crClient.List(context.TODO(), vsClassList); err != nil {

View File

@@ -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"
@@ -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

View File

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

View File

@@ -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{