From 8e3c1b54a7c38e38d57000ef9c0a7a71d773f3e9 Mon Sep 17 00:00:00 2001 From: Bharath Thiruveedula Date: Thu, 9 Jan 2020 00:47:04 +0530 Subject: [PATCH] Fix interpodaffinity issue Signed-off-by: Bharath Thiruveedula --- .../plugins/interpodaffinity/filtering.go | 2 +- .../interpodaffinity/filtering_test.go | 59 +++++++++++++++++-- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/pkg/scheduler/framework/plugins/interpodaffinity/filtering.go b/pkg/scheduler/framework/plugins/interpodaffinity/filtering.go index 1dfd7fd0d41..125e81ae381 100644 --- a/pkg/scheduler/framework/plugins/interpodaffinity/filtering.go +++ b/pkg/scheduler/framework/plugins/interpodaffinity/filtering.go @@ -83,7 +83,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 9b94b5682a1..2236c150f94 100644 --- a/pkg/scheduler/framework/plugins/interpodaffinity/filtering_test.go +++ b/pkg/scheduler/framework/plugins/interpodaffinity/filtering_test.go @@ -1720,12 +1720,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", @@ -1749,11 +1751,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}, @@ -1782,11 +1791,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}, @@ -1816,11 +1834,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}, @@ -1851,6 +1880,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, + }, }, } @@ -1890,6 +1924,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) }