Skip to content

Commit

Permalink
Check whether the PVC exists in RIA.
Browse files Browse the repository at this point in the history
Separate label and annotation setting in PVC BIA.

Signed-off-by: Xun Jiang <[email protected]>
  • Loading branch information
Xun Jiang committed Aug 5, 2023
1 parent 22c2e87 commit e250912
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/backup/pvc_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions internal/backup/pvc_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
},
}
Expand Down
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 e250912

Please sign in to comment.