Skip to content

Commit

Permalink
Check whether the PVC exists in RIA.
Browse files Browse the repository at this point in the history
Signed-off-by: Xun Jiang <[email protected]>
  • Loading branch information
Xun Jiang committed Aug 5, 2023
1 parent 22c2e87 commit 9379190
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/restore/pvc_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ 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 {
logger.Warnf("PVC already exists. Skip restore this PVC.")
return &velero.RestoreItemActionExecuteOutput{
UpdatedItem: input.Item,
}, nil
}

removePVCAnnotations(&pvc,
[]string{AnnBindCompleted, AnnBoundByController, AnnStorageProvisioner, AnnBetaStorageProvisioner, AnnSelectedNode})

Expand Down
13 changes: 13 additions & 0 deletions internal/restore/pvc_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ func TestExecute(t *testing.T) {
expectedErr string
expectedDataDownload *velerov2alpha1.DataDownload
expectedPVC *corev1api.PersistentVolumeClaim
preCreatePVC bool
}{
{
name: "Don't restore PV",
Expand Down Expand Up @@ -608,6 +609,13 @@ func TestExecute(t *testing.T) {
restore: builder.ForRestore("migre209d0da-49c7-45ba-8d5a-3e59fd591ec1", "testRestore").Backup("testBackup").ObjectMeta(builder.WithUID("uid")).Result(),
pvc: builder.ForPersistentVolumeClaim("migre209d0da-49c7-45ba-8d5a-3e59fd591ec1", "kibishii-data-kibishii-deployment-0").ObjectMeta(builder.WithAnnotations(util.VolumeSnapshotRestoreSize, "10Gi")).Result(),
},
{
name: "",
backup: builder.ForBackup("velero", "testBackup").SnapshotMoveData(true).Result(),
restore: builder.ForRestore("velero", "testRestore").Backup("testBackup").ObjectMeta(builder.WithUID("uid")).Result(),
pvc: builder.ForPersistentVolumeClaim("velero", "testPVC").ObjectMeta(builder.WithAnnotations(util.VolumeSnapshotRestoreSize, "10Gi", util.DataUploadNameAnnotation, "velero/")).Result(),
preCreatePVC: true,
},
}

for _, tc := range tests {
Expand All @@ -629,6 +637,11 @@ func TestExecute(t *testing.T) {
input.Restore = tc.restore
}

if tc.preCreatePVC {
_, err := pvcRIA.Client.CoreV1().PersistentVolumeClaims(tc.pvc.Namespace).Create(context.Background(), tc.pvc, metav1.CreateOptions{})
require.NoError(t, err)
}

if tc.backup != nil {
_, err := pvcRIA.VeleroClient.VeleroV1().Backups(tc.backup.Namespace).Create(context.Background(), tc.backup, metav1.CreateOptions{})
require.NoError(t, err)
Expand Down

0 comments on commit 9379190

Please sign in to comment.