diff --git a/internal/restore/pvc_action.go b/internal/restore/pvc_action.go index 74a347e3..68e508e2 100644 --- a/internal/restore/pvc_action.go +++ b/internal/restore/pvc_action.go @@ -131,7 +131,8 @@ func (p *PVCRestoreItemAction) Execute(input *velero.RestoreItemActionExecuteInp }) logger.Info("Starting PVCRestoreItemAction for PVC") - if _, err := p.Client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(context.Background(), pvc.Name, metav1.GetOptions{}); err == nil { + // If PVC already exists, returns early. + if p.isResourceExist(pvc, *input.Restore) { logger.Warnf("PVC already exists. Skip restore this PVC.") return &velero.RestoreItemActionExecuteOutput{ UpdatedItem: input.Item, @@ -463,3 +464,15 @@ func restoreFromDataUploadResult(ctx context.Context, restore *velerov1api.Resto return dataDownload, nil } + +func (p *PVCRestoreItemAction) isResourceExist(pvc corev1api.PersistentVolumeClaim, restore velerov1api.Restore) bool { + // get target namespace to restore into, if different from source namespace + targetNamespace := pvc.Namespace + if target, ok := restore.Spec.NamespaceMapping[pvc.Namespace]; ok { + targetNamespace = target + } + if _, err := p.Client.CoreV1().PersistentVolumeClaims(targetNamespace).Get(context.Background(), pvc.Name, metav1.GetOptions{}); err == nil { + return true + } + return false +} diff --git a/internal/restore/pvc_action_test.go b/internal/restore/pvc_action_test.go index 2e9b28be..f96b7b39 100644 --- a/internal/restore/pvc_action_test.go +++ b/internal/restore/pvc_action_test.go @@ -616,6 +616,13 @@ func TestExecute(t *testing.T) { pvc: builder.ForPersistentVolumeClaim("velero", "testPVC").ObjectMeta(builder.WithAnnotations(util.VolumeSnapshotRestoreSize, "10Gi", util.DataUploadNameAnnotation, "velero/")).Result(), preCreatePVC: true, }, + { + name: "Restore a PVC that already exists in the mapping namespace", + backup: builder.ForBackup("velero", "testBackup").SnapshotMoveData(true).Result(), + restore: builder.ForRestore("velero", "testRestore").Backup("testBackup").NamespaceMappings("velero", "restore").ObjectMeta(builder.WithUID("uid")).Result(), + pvc: builder.ForPersistentVolumeClaim("restore", "testPVC").ObjectMeta(builder.WithAnnotations(util.VolumeSnapshotRestoreSize, "10Gi", util.DataUploadNameAnnotation, "velero/")).Result(), + preCreatePVC: true, + }, } for _, tc := range tests {