From 5fc1de88588b41c627d46e700929b6151d0547c2 Mon Sep 17 00:00:00 2001 From: Xun Jiang Date: Wed, 13 Mar 2024 11:21:48 +0800 Subject: [PATCH] Check whether the VolumeSnapshot's source PVC is nil before using it. Signed-off-by: Xun Jiang --- pkg/restore/restore.go | 5 +++++ pkg/restore/restore_test.go | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/pkg/restore/restore.go b/pkg/restore/restore.go index ca2e83579..36547b80e 100644 --- a/pkg/restore/restore.go +++ b/pkg/restore/restore.go @@ -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 diff --git a/pkg/restore/restore_test.go b/pkg/restore/restore_test.go index 33b6d4a2e..efca33870 100644 --- a/pkg/restore/restore_test.go +++ b/pkg/restore/restore_test.go @@ -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{