Skip to content

Commit

Permalink
Merge pull request #196 from blackpiglet/namespace_mapping
Browse files Browse the repository at this point in the history
Support namespace-mapping restore case for PVC BIA.
  • Loading branch information
Lyndon-Li authored Aug 8, 2023
2 parents 0857bc9 + 4e50838 commit 8fff955
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
15 changes: 14 additions & 1 deletion internal/restore/pvc_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
7 changes: 7 additions & 0 deletions internal/restore/pvc_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8fff955

Please sign in to comment.