From 42ddbb4276666535670606f59a6d61e8ee1202d0 Mon Sep 17 00:00:00 2001 From: mantuliu <240951888@qq.com> Date: Fri, 10 Mar 2023 20:08:31 +0800 Subject: [PATCH] Fix PreFilter logic problem in node affinity --- .../framework/plugins/nodeaffinity/node_affinity.go | 11 +++++------ .../plugins/nodeaffinity/node_affinity_test.go | 5 +++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity.go b/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity.go index 841da141b58..d966e53d1d1 100644 --- a/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity.go +++ b/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity.go @@ -127,14 +127,13 @@ func (pl *NodeAffinity) PreFilter(ctx context.Context, cycleState *framework.Cyc // then all nodes are eligible because the terms are ORed. return nil, nil } - // If the set is empty, it means the terms had affinity to different - // sets of nodes, and since they are ANDed, then the pod will not match any node. - if len(termNodeNames) == 0 { - return nil, framework.NewStatus(framework.UnschedulableAndUnresolvable, errReasonConflict) - } nodeNames = nodeNames.Union(termNodeNames) } - if nodeNames != nil { + // If nodeNames is not nil, but length is 0, it means each term have conflicting affinity to node.Name; + // therefore, pod will not match any node. + if nodeNames != nil && len(nodeNames) == 0 { + return nil, framework.NewStatus(framework.UnschedulableAndUnresolvable, errReasonConflict) + } else if len(nodeNames) > 0 { return &framework.PreFilterResult{NodeNames: nodeNames}, nil } return nil, nil diff --git a/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity_test.go b/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity_test.go index dc815ee6674..6b7cda4fd97 100644 --- a/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity_test.go +++ b/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity_test.go @@ -517,6 +517,11 @@ func TestNodeAffinity(t *testing.T) { Operator: v1.NodeSelectorOpIn, Values: []string{"node1"}, }, + { + Key: metav1.ObjectNameField, + Operator: v1.NodeSelectorOpIn, + Values: []string{"node2"}, + }, }, }, {