diff --git a/pkg/scheduler/predicates/ha.go b/pkg/scheduler/predicates/ha.go index 571f3bcc35..e1f20cb3e4 100644 --- a/pkg/scheduler/predicates/ha.go +++ b/pkg/scheduler/predicates/ha.go @@ -200,13 +200,18 @@ func (h *ha) realAcquireLock(pod *apiv1.Pod) (*apiv1.PersistentVolumeClaim, *api if schedulingPVC == currentPVC { return schedulingPVC, currentPVC, nil } - schedulingPodName := getPodNameFromPVC(schedulingPVC) - schedulingPod, err := h.podGetFn(ns, schedulingPodName) - if err != nil { - return schedulingPVC, currentPVC, err - } - if schedulingPVC.Status.Phase != apiv1.ClaimBound || schedulingPod.Spec.NodeName == "" { - return schedulingPVC, currentPVC, fmt.Errorf("waiting for Pod %s/%s scheduling", ns, strings.TrimPrefix(schedulingPVC.GetName(), component)) + + // if pvc is not defer deleting(has AnnPVCDeferDeleting annotation means defer deleting), we must wait for its scheduling + // else clear its AnnPVCPodScheduling annotation and acquire the lock + if schedulingPVC.Annotations[label.AnnPVCDeferDeleting] == "" { + schedulingPodName := getPodNameFromPVC(schedulingPVC) + schedulingPod, err := h.podGetFn(ns, schedulingPodName) + if err != nil { + return schedulingPVC, currentPVC, err + } + if schedulingPVC.Status.Phase != apiv1.ClaimBound || schedulingPod.Spec.NodeName == "" { + return schedulingPVC, currentPVC, fmt.Errorf("waiting for Pod %s/%s scheduling", ns, strings.TrimPrefix(schedulingPVC.GetName(), component)) + } } delete(schedulingPVC.Annotations, label.AnnPVCPodScheduling) diff --git a/pkg/scheduler/predicates/ha_test.go b/pkg/scheduler/predicates/ha_test.go index 26a5b89da4..b0674a6021 100644 --- a/pkg/scheduler/predicates/ha_test.go +++ b/pkg/scheduler/predicates/ha_test.go @@ -379,6 +379,45 @@ func TestHARealAcquireLockFn(t *testing.T) { g.Expect(currentPVC.Annotations[label.AnnPVCPodScheduling]).NotTo(BeEmpty()) }, }, + { + name: "scheduling pvc is defer deleting, current pvc acquire lock", + podFn: newHAPDPod, + pvcListFn: func(ns, instanceName, component string) (*corev1.PersistentVolumeClaimList, error) { + return &corev1.PersistentVolumeClaimList{ + TypeMeta: metav1.TypeMeta{Kind: "PersistentVolumeClaimList", APIVersion: "v1"}, + Items: []corev1.PersistentVolumeClaim{ + { + TypeMeta: metav1.TypeMeta{Kind: "PersistentVolumeClaim", APIVersion: "v1"}, + ObjectMeta: metav1.ObjectMeta{ + Namespace: metav1.NamespaceDefault, + Name: "pd-cluster-1-pd-0", + }, + }, + { + TypeMeta: metav1.TypeMeta{Kind: "PersistentVolumeClaim", APIVersion: "v1"}, + ObjectMeta: metav1.ObjectMeta{ + Namespace: metav1.NamespaceDefault, + Name: "pd-cluster-1-pd-1", + Annotations: map[string]string{ + label.AnnPVCPodScheduling: "true", + label.AnnPVCDeferDeleting: "true", + }, + }, + Status: corev1.PersistentVolumeClaimStatus{Phase: corev1.ClaimBound}, + }, + }, + }, nil + }, + podGetFn: podGetErr(), + updatePVCFn: func(claim *corev1.PersistentVolumeClaim) error { + return nil + }, + expectFn: func(schedulingPVC, currentPVC *apiv1.PersistentVolumeClaim, err error) { + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(schedulingPVC.Annotations[label.AnnPVCPodScheduling]).To(BeEmpty()) + g.Expect(currentPVC.Annotations[label.AnnPVCPodScheduling]).NotTo(BeEmpty()) + }, + }, } for i := range tests {