Fix interpodaffinity issue

Signed-off-by: Bharath Thiruveedula <bharath_ves@hotmail.com>
This commit is contained in:
Bharath Thiruveedula 2020-01-09 00:47:04 +05:30
parent b34c96b62c
commit 8e3c1b54a7
2 changed files with 54 additions and 7 deletions

View File

@ -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)

View File

@ -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)
}