Merge pull request #6492 from Lyndon-Li/data-mover-restore-abort-for-existing-pvc

Data mover restore abort for existing PVC
This commit is contained in:
lyndon
2023-07-14 17:19:53 +08:00
committed by GitHub
4 changed files with 83 additions and 10 deletions
+7 -2
View File
@@ -284,11 +284,11 @@ func WaitPVBound(ctx context.Context, pvGetter corev1client.CoreV1Interface, pvN
}
if tmpPV.Spec.ClaimRef.Name != pvcName {
return false, nil
return false, errors.Errorf("pv has been bound by unexpected pvc %s/%s", tmpPV.Spec.ClaimRef.Namespace, tmpPV.Spec.ClaimRef.Name)
}
if tmpPV.Spec.ClaimRef.Namespace != pvcNamespace {
return false, nil
return false, errors.Errorf("pv has been bound by unexpected pvc %s/%s", tmpPV.Spec.ClaimRef.Namespace, tmpPV.Spec.ClaimRef.Name)
}
updated = tmpPV
@@ -302,3 +302,8 @@ func WaitPVBound(ctx context.Context, pvGetter corev1client.CoreV1Interface, pvN
return updated, nil
}
}
// IsPVCBound returns true if the specified PVC has been bound
func IsPVCBound(pvc *corev1api.PersistentVolumeClaim) bool {
return pvc.Spec.VolumeName != ""
}
+52 -8
View File
@@ -741,9 +741,10 @@ func TestWaitPVBound(t *testing.T) {
err: "error to wait for bound of PV: timed out waiting for the condition",
},
{
name: "pvc claimRef pvc name mismatch",
pvName: "fake-pv",
pvcName: "fake-pvc",
name: "pvc claimRef pvc name mismatch",
pvName: "fake-pv",
pvcName: "fake-pvc",
pvcNamespace: "fake-ns",
kubeClientObj: []runtime.Object{
&corev1api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
@@ -751,12 +752,14 @@ func TestWaitPVBound(t *testing.T) {
},
Spec: corev1api.PersistentVolumeSpec{
ClaimRef: &corev1api.ObjectReference{
Kind: "fake-kind",
Kind: "fake-kind",
Namespace: "fake-ns",
Name: "fake-pvc-1",
},
},
},
},
err: "error to wait for bound of PV: timed out waiting for the condition",
err: "error to wait for bound of PV: pv has been bound by unexpected pvc fake-ns/fake-pvc-1",
},
{
name: "pvc claimRef pvc namespace mismatch",
@@ -770,13 +773,14 @@ func TestWaitPVBound(t *testing.T) {
},
Spec: corev1api.PersistentVolumeSpec{
ClaimRef: &corev1api.ObjectReference{
Kind: "fake-kind",
Name: "fake-pvc",
Kind: "fake-kind",
Namespace: "fake-ns-1",
Name: "fake-pvc",
},
},
},
},
err: "error to wait for bound of PV: timed out waiting for the condition",
err: "error to wait for bound of PV: pv has been bound by unexpected pvc fake-ns-1/fake-pvc",
},
{
name: "success",
@@ -834,3 +838,43 @@ func TestWaitPVBound(t *testing.T) {
})
}
}
func TestIsPVCBound(t *testing.T) {
tests := []struct {
name string
pvc *corev1api.PersistentVolumeClaim
expect bool
}{
{
name: "expect bound",
pvc: &corev1api.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-ns",
Name: "fake-pvc",
},
Spec: corev1api.PersistentVolumeClaimSpec{
VolumeName: "fake-volume",
},
},
expect: true,
},
{
name: "expect not bound",
pvc: &corev1api.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-ns",
Name: "fake-pvc",
},
},
expect: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := IsPVCBound(test.pvc)
assert.Equal(t, test.expect, result)
})
}
}