Skip to content

Commit

Permalink
Check whether the VolumeSnapshot's source PVC is nil before using it.
Browse files Browse the repository at this point in the history
Signed-off-by: Xun Jiang <blackpigletbruce@gmail.com>
  • Loading branch information
blackpiglet committed Mar 13, 2024
1 parent 79e9e31 commit 4d01c7f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
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

0 comments on commit 4d01c7f

Please sign in to comment.