From e250912a7b5d2d1cfe5b900f1e223f6068127214 Mon Sep 17 00:00:00 2001 From: Xun Jiang Date: Sat, 5 Aug 2023 09:51:52 +0800 Subject: [PATCH] Check whether the PVC exists in RIA. Separate label and annotation setting in PVC BIA. Signed-off-by: Xun Jiang --- internal/backup/pvc_action.go | 2 +- internal/backup/pvc_action_test.go | 6 +++--- internal/restore/pvc_action.go | 7 +++++++ internal/restore/pvc_action_test.go | 13 +++++++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/internal/backup/pvc_action.go b/internal/backup/pvc_action.go index 5fd9b2cc..d21fc865 100644 --- a/internal/backup/pvc_action.go +++ b/internal/backup/pvc_action.go @@ -166,7 +166,7 @@ func (p *PVCBackupItemAction) Execute(item runtime.Unstructured, backup *velerov velerov1api.BackupNameLabel: backup.Name, } - annotations := labels + annotations := make(map[string]string) annotations[util.MustIncludeAdditionalItemAnnotation] = "true" var additionalItems []velero.ResourceIdentifier diff --git a/internal/backup/pvc_action_test.go b/internal/backup/pvc_action_test.go index 0c0b019b..4d59bf93 100644 --- a/internal/backup/pvc_action_test.go +++ b/internal/backup/pvc_action_test.go @@ -115,7 +115,7 @@ func TestExecute(t *testing.T) { }, }, { - name: "", + name: "Verify PVC is modified as expected", backup: builder.ForBackup("velero", "test").SnapshotMoveData(true).Result(), pvc: builder.ForPersistentVolumeClaim("velero", "testPVC").VolumeName("testPV").StorageClass("testSC").Phase(corev1.ClaimBound).Result(), pv: builder.ForPersistentVolume("testPV").CSI("hostpath", "testVolume").Result(), @@ -125,8 +125,8 @@ func TestExecute(t *testing.T) { operationID: ".", expectedErr: nil, expectedPVC: builder.ForPersistentVolumeClaim("velero", "testPVC"). - ObjectMeta(builder.WithAnnotations(util.MustIncludeAdditionalItemAnnotation, "true", velerov1api.BackupNameLabel, "test", util.DataUploadNameAnnotation, "velero/", util.VolumeSnapshotLabel, ""), - builder.WithLabels(util.MustIncludeAdditionalItemAnnotation, "true", velerov1api.BackupNameLabel, "test", util.DataUploadNameAnnotation, "velero/", util.VolumeSnapshotLabel, "")). + ObjectMeta(builder.WithAnnotations(util.MustIncludeAdditionalItemAnnotation, "true", util.DataUploadNameAnnotation, "velero/"), + builder.WithLabels(velerov1api.BackupNameLabel, "test", util.VolumeSnapshotLabel, "")). VolumeName("testPV").StorageClass("testSC").Phase(corev1.ClaimBound).Result(), }, } diff --git a/internal/restore/pvc_action.go b/internal/restore/pvc_action.go index b01c7398..74a347e3 100644 --- a/internal/restore/pvc_action.go +++ b/internal/restore/pvc_action.go @@ -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}) diff --git a/internal/restore/pvc_action_test.go b/internal/restore/pvc_action_test.go index 33440884..387bc147 100644 --- a/internal/restore/pvc_action_test.go +++ b/internal/restore/pvc_action_test.go @@ -534,6 +534,7 @@ func TestExecute(t *testing.T) { expectedErr string expectedDataDownload *velerov2alpha1.DataDownload expectedPVC *corev1api.PersistentVolumeClaim + preCreatePVC bool }{ { name: "Don't restore PV", @@ -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 { @@ -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)