Skip to content

Commit 68f3802

Browse files
committed
move NodeLabel predicate logic to its Filter plugin
1 parent 22a0f55 commit 68f3802

File tree

4 files changed

+28
-128
lines changed

4 files changed

+28
-128
lines changed

pkg/scheduler/algorithm/predicates/predicates.go

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -939,59 +939,6 @@ func PodFitsHost(pod *v1.Pod, meta Metadata, nodeInfo *schedulernodeinfo.NodeInf
939939
return false, []PredicateFailureReason{ErrPodNotMatchHostName}, nil
940940
}
941941

942-
// NodeLabelChecker contains information to check node labels for a predicate.
943-
type NodeLabelChecker struct {
944-
// presentLabels should be present for the node to be considered a fit for hosting the pod
945-
presentLabels []string
946-
// absentLabels should be absent for the node to be considered a fit for hosting the pod
947-
absentLabels []string
948-
}
949-
950-
// NewNodeLabelPredicate creates a predicate which evaluates whether a pod can fit based on the
951-
// node labels which match a filter that it requests.
952-
func NewNodeLabelPredicate(presentLabels []string, absentLabels []string) FitPredicate {
953-
labelChecker := &NodeLabelChecker{
954-
presentLabels: presentLabels,
955-
absentLabels: absentLabels,
956-
}
957-
return labelChecker.CheckNodeLabelPresence
958-
}
959-
960-
// CheckNodeLabelPresence checks whether all of the specified labels exists on a node or not, regardless of their value
961-
// If "presence" is false, then returns false if any of the requested labels matches any of the node's labels,
962-
// otherwise returns true.
963-
// If "presence" is true, then returns false if any of the requested labels does not match any of the node's labels,
964-
// otherwise returns true.
965-
//
966-
// Consider the cases where the nodes are placed in regions/zones/racks and these are identified by labels
967-
// In some cases, it is required that only nodes that are part of ANY of the defined regions/zones/racks be selected
968-
//
969-
// Alternately, eliminating nodes that have a certain label, regardless of value, is also useful
970-
// A node may have a label with "retiring" as key and the date as the value
971-
// and it may be desirable to avoid scheduling new pods on this node.
972-
func (n *NodeLabelChecker) CheckNodeLabelPresence(pod *v1.Pod, meta Metadata, nodeInfo *schedulernodeinfo.NodeInfo) (bool, []PredicateFailureReason, error) {
973-
node := nodeInfo.Node()
974-
if node == nil {
975-
return false, nil, fmt.Errorf("node not found")
976-
}
977-
978-
nodeLabels := labels.Set(node.Labels)
979-
check := func(labels []string, presence bool) bool {
980-
for _, label := range labels {
981-
exists := nodeLabels.Has(label)
982-
if (exists && !presence) || (!exists && presence) {
983-
return false
984-
}
985-
}
986-
return true
987-
}
988-
if check(n.presentLabels, true) && check(n.absentLabels, false) {
989-
return true, nil, nil
990-
}
991-
992-
return false, []PredicateFailureReason{ErrNodeLabelPresenceViolated}, nil
993-
}
994-
995942
// PodFitsHostPorts is a wrapper around PodFitsHostPortsPredicate. This is needed until
996943
// we are able to get rid of the FitPredicate function signature.
997944
// TODO(#85822): remove this function once predicate registration logic is deleted.

pkg/scheduler/algorithm/predicates/predicates_test.go

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,69 +1651,6 @@ func TestPodFitsSelector(t *testing.T) {
16511651
}
16521652
}
16531653

1654-
func TestNodeLabelPresence(t *testing.T) {
1655-
label := map[string]string{"foo": "bar", "bar": "foo"}
1656-
tests := []struct {
1657-
pod *v1.Pod
1658-
presentLabels []string
1659-
absentLabels []string
1660-
fits bool
1661-
name string
1662-
}{
1663-
{
1664-
presentLabels: []string{"baz"},
1665-
fits: false,
1666-
name: "label does not match, presence true",
1667-
},
1668-
{
1669-
absentLabels: []string{"baz"},
1670-
fits: true,
1671-
name: "label does not match, presence false",
1672-
},
1673-
{
1674-
presentLabels: []string{"foo", "baz"},
1675-
fits: false,
1676-
name: "one label matches, presence true",
1677-
},
1678-
{
1679-
absentLabels: []string{"foo", "baz"},
1680-
fits: false,
1681-
name: "one label matches, presence false",
1682-
},
1683-
{
1684-
presentLabels: []string{"foo", "bar"},
1685-
fits: true,
1686-
name: "all labels match, presence true",
1687-
},
1688-
{
1689-
absentLabels: []string{"foo", "bar"},
1690-
fits: false,
1691-
name: "all labels match, presence false",
1692-
},
1693-
}
1694-
expectedFailureReasons := []PredicateFailureReason{ErrNodeLabelPresenceViolated}
1695-
1696-
for _, test := range tests {
1697-
t.Run(test.name, func(t *testing.T) {
1698-
node := v1.Node{ObjectMeta: metav1.ObjectMeta{Labels: label}}
1699-
nodeInfo := schedulernodeinfo.NewNodeInfo()
1700-
nodeInfo.SetNode(&node)
1701-
1702-
labelChecker := NodeLabelChecker{test.presentLabels, test.absentLabels}
1703-
fits, reasons, err := labelChecker.CheckNodeLabelPresence(test.pod, nil, nodeInfo)
1704-
if err != nil {
1705-
t.Errorf("unexpected error: %v", err)
1706-
}
1707-
if !fits && !reflect.DeepEqual(reasons, expectedFailureReasons) {
1708-
t.Errorf("unexpected failure reasons: %v, want: %v", reasons, expectedFailureReasons)
1709-
}
1710-
if fits != test.fits {
1711-
t.Errorf("expected: %v got %v", test.fits, fits)
1712-
}
1713-
})
1714-
}
1715-
}
1716-
17171654
func newPodWithPort(hostPorts ...int) *v1.Pod {
17181655
networkPorts := []v1.ContainerPort{}
17191656
for _, port := range hostPorts {

pkg/scheduler/algorithm_factory.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,7 @@ func RegisterCustomFitPredicate(policy schedulerapi.PredicatePolicy, pluginArgs
278278
pluginArgs.NodeLabelArgs.AbsentLabels = append(pluginArgs.NodeLabelArgs.AbsentLabels, policy.Argument.LabelsPresence.Labels...)
279279
}
280280
predicateFactory = func(_ AlgorithmFactoryArgs) predicates.FitPredicate {
281-
return predicates.NewNodeLabelPredicate(
282-
pluginArgs.NodeLabelArgs.PresentLabels,
283-
pluginArgs.NodeLabelArgs.AbsentLabels,
284-
)
281+
return nil
285282
}
286283
}
287284
} else if predicateFactory, ok = fitPredicateMap[policyName]; ok {

pkg/scheduler/framework/plugins/nodelabel/node_label.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,14 @@ func New(plArgs *runtime.Unknown, handle framework.FrameworkHandle) (framework.P
7171
return nil, err
7272
}
7373
return &NodeLabel{
74-
handle: handle,
75-
predicate: predicates.NewNodeLabelPredicate(args.PresentLabels, args.AbsentLabels),
76-
Args: args,
74+
handle: handle,
75+
Args: args,
7776
}, nil
7877
}
7978

8079
// NodeLabel checks whether a pod can fit based on the node labels which match a filter that it requests.
8180
type NodeLabel struct {
82-
handle framework.FrameworkHandle
83-
predicate predicates.FitPredicate
81+
handle framework.FrameworkHandle
8482
Args
8583
}
8684

@@ -93,10 +91,31 @@ func (pl *NodeLabel) Name() string {
9391
}
9492

9593
// Filter invoked at the filter extension point.
94+
// It checks whether all of the specified labels exists on a node or not, regardless of their value
95+
//
96+
// Consider the cases where the nodes are placed in regions/zones/racks and these are identified by labels
97+
// In some cases, it is required that only nodes that are part of ANY of the defined regions/zones/racks be selected
98+
//
99+
// Alternately, eliminating nodes that have a certain label, regardless of value, is also useful
100+
// A node may have a label with "retiring" as key and the date as the value
101+
// and it may be desirable to avoid scheduling new pods on this node.
96102
func (pl *NodeLabel) Filter(ctx context.Context, _ *framework.CycleState, pod *v1.Pod, nodeInfo *nodeinfo.NodeInfo) *framework.Status {
97-
// Note that NodeLabelPredicate doesn't use predicate metadata, hence passing nil here.
98-
_, reasons, err := pl.predicate(pod, nil, nodeInfo)
99-
return migration.PredicateResultToFrameworkStatus(reasons, err)
103+
node := nodeInfo.Node()
104+
nodeLabels := labels.Set(node.Labels)
105+
check := func(labels []string, presence bool) bool {
106+
for _, label := range labels {
107+
exists := nodeLabels.Has(label)
108+
if (exists && !presence) || (!exists && presence) {
109+
return false
110+
}
111+
}
112+
return true
113+
}
114+
if check(pl.PresentLabels, true) && check(pl.AbsentLabels, false) {
115+
return nil
116+
}
117+
118+
return migration.PredicateResultToFrameworkStatus([]predicates.PredicateFailureReason{predicates.ErrNodeLabelPresenceViolated}, nil)
100119
}
101120

102121
// Score invoked at the score extension point.

0 commit comments

Comments
 (0)