diff --git a/pkg/scheduler/framework/plugins/interpodaffinity/filtering.go b/pkg/scheduler/framework/plugins/interpodaffinity/filtering.go index 67c17efc8d7..4d7af918ccf 100644 --- a/pkg/scheduler/framework/plugins/interpodaffinity/filtering.go +++ b/pkg/scheduler/framework/plugins/interpodaffinity/filtering.go @@ -92,7 +92,7 @@ func (s *preFilterState) updateWithPod(updatedPod, pod *v1.Pod, node *v1.Node, m affinity := pod.Spec.Affinity podNodeName := updatedPod.Spec.NodeName if affinity != nil && len(podNodeName) > 0 { - if affinity.PodAffinity == nil { + if affinity.PodAffinity != nil { affinityTerms, err := getAffinityTerms(pod, schedutil.GetPodAffinityTerms(affinity.PodAffinity)) if err != nil { return fmt.Errorf("error in getting affinity terms of Pod %v: %v", pod.Name, err) diff --git a/pkg/scheduler/framework/plugins/interpodaffinity/filtering_test.go b/pkg/scheduler/framework/plugins/interpodaffinity/filtering_test.go index 83799483165..556df83b71f 100644 --- a/pkg/scheduler/framework/plugins/interpodaffinity/filtering_test.go +++ b/pkg/scheduler/framework/plugins/interpodaffinity/filtering_test.go @@ -1719,12 +1719,14 @@ func TestPreFilterStateAddRemovePod(t *testing.T) { } tests := []struct { - name string - pendingPod *v1.Pod - addedPod *v1.Pod - existingPods []*v1.Pod - nodes []*v1.Node - services []*v1.Service + name string + pendingPod *v1.Pod + addedPod *v1.Pod + existingPods []*v1.Pod + nodes []*v1.Node + services []*v1.Service + expectedAntiAffinity topologyToMatchedTermCount + expectedAffinity topologyToMatchedTermCount }{ { name: "no affinity exist", @@ -1748,11 +1750,18 @@ func TestPreFilterStateAddRemovePod(t *testing.T) { {ObjectMeta: metav1.ObjectMeta{Name: "nodeB", Labels: label2}}, {ObjectMeta: metav1.ObjectMeta{Name: "nodeC", Labels: label3}}, }, + expectedAntiAffinity: topologyToMatchedTermCount{}, + expectedAffinity: topologyToMatchedTermCount{}, }, { name: "preFilterState anti-affinity terms are updated correctly after adding and removing a pod", pendingPod: &v1.Pod{ ObjectMeta: metav1.ObjectMeta{Name: "pending", Labels: selector1}, + Spec: v1.PodSpec{ + Affinity: &v1.Affinity{ + PodAntiAffinity: antiAffinityFooBar, + }, + }, }, existingPods: []*v1.Pod{ {ObjectMeta: metav1.ObjectMeta{Name: "p1", Labels: selector1}, @@ -1781,11 +1790,20 @@ func TestPreFilterStateAddRemovePod(t *testing.T) { {ObjectMeta: metav1.ObjectMeta{Name: "nodeB", Labels: label2}}, {ObjectMeta: metav1.ObjectMeta{Name: "nodeC", Labels: label3}}, }, + expectedAntiAffinity: topologyToMatchedTermCount{ + {key: "region", value: "r1"}: 2, + }, + expectedAffinity: topologyToMatchedTermCount{}, }, { name: "preFilterState anti-affinity terms are updated correctly after adding and removing a pod", pendingPod: &v1.Pod{ ObjectMeta: metav1.ObjectMeta{Name: "pending", Labels: selector1}, + Spec: v1.PodSpec{ + Affinity: &v1.Affinity{ + PodAntiAffinity: antiAffinityComplex, + }, + }, }, existingPods: []*v1.Pod{ {ObjectMeta: metav1.ObjectMeta{Name: "p1", Labels: selector1}, @@ -1815,11 +1833,22 @@ func TestPreFilterStateAddRemovePod(t *testing.T) { {ObjectMeta: metav1.ObjectMeta{Name: "nodeB", Labels: label2}}, {ObjectMeta: metav1.ObjectMeta{Name: "nodeC", Labels: label3}}, }, + expectedAntiAffinity: topologyToMatchedTermCount{ + {key: "region", value: "r1"}: 2, + {key: "zone", value: "z11"}: 2, + {key: "zone", value: "z21"}: 1, + }, + expectedAffinity: topologyToMatchedTermCount{}, }, { name: "preFilterState matching pod affinity and anti-affinity are updated correctly after adding and removing a pod", pendingPod: &v1.Pod{ ObjectMeta: metav1.ObjectMeta{Name: "pending", Labels: selector1}, + Spec: v1.PodSpec{ + Affinity: &v1.Affinity{ + PodAffinity: affinityComplex, + }, + }, }, existingPods: []*v1.Pod{ {ObjectMeta: metav1.ObjectMeta{Name: "p1", Labels: selector1}, @@ -1850,6 +1879,11 @@ func TestPreFilterStateAddRemovePod(t *testing.T) { {ObjectMeta: metav1.ObjectMeta{Name: "nodeB", Labels: label2}}, {ObjectMeta: metav1.ObjectMeta{Name: "nodeC", Labels: label3}}, }, + expectedAntiAffinity: topologyToMatchedTermCount{}, + expectedAffinity: topologyToMatchedTermCount{ + {key: "region", value: "r1"}: 2, + {key: "zone", value: "z11"}: 2, + }, }, } @@ -1889,6 +1923,19 @@ func TestPreFilterStateAddRemovePod(t *testing.T) { t.Errorf("error adding pod to meta: %v", err) } + newState, err := getPreFilterState(cycleState) + if err != nil { + t.Errorf("failed to get preFilterState from cycleState: %v", err) + } + + if !reflect.DeepEqual(newState.topologyToMatchedAntiAffinityTerms, test.expectedAntiAffinity) { + t.Errorf("State is not equal, got: %v, want: %v", newState.topologyToMatchedAntiAffinityTerms, test.expectedAntiAffinity) + } + + if !reflect.DeepEqual(newState.topologyToMatchedAffinityTerms, test.expectedAffinity) { + t.Errorf("State is not equal, got: %v, want: %v", newState.topologyToMatchedAffinityTerms, test.expectedAffinity) + } + if !reflect.DeepEqual(allPodsState, state) { t.Errorf("State is not equal, got: %v, want: %v", state, allPodsState) }