@@ -26,6 +26,7 @@ import (
26
26
"k8s.io/apimachinery/pkg/labels"
27
27
"k8s.io/apimachinery/pkg/util/sets"
28
28
"k8s.io/component-helpers/scheduling/corev1"
29
+ "k8s.io/component-helpers/scheduling/corev1/nodeaffinity"
29
30
"k8s.io/klog/v2"
30
31
)
31
32
@@ -55,56 +56,13 @@ func PodMatchesTermsNamespaceAndSelector(pod *v1.Pod, namespaces sets.Set[string
55
56
return true
56
57
}
57
58
58
- // The following code has been copied from predicates package to avoid the
59
- // huge vendoring issues, mostly copied from
60
- // k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/predicates/
61
- // Some minor changes have been made to ease the imports, but most of the code
62
- // remains untouched
63
-
64
59
// PodMatchNodeSelector checks if a pod node selector matches the node label.
65
60
func PodMatchNodeSelector (pod * v1.Pod , node * v1.Node ) (bool , error ) {
66
61
if node == nil {
67
62
return false , fmt .Errorf ("node not found" )
68
63
}
69
- return podMatchesNodeLabels (pod , node ), nil
70
- }
71
-
72
- // The pod can only schedule onto nodes that satisfy requirements in both NodeAffinity and nodeSelector.
73
- func podMatchesNodeLabels (pod * v1.Pod , node * v1.Node ) bool {
74
- // Check if node.Labels match pod.Spec.NodeSelector.
75
- if len (pod .Spec .NodeSelector ) > 0 {
76
- selector := labels .SelectorFromSet (pod .Spec .NodeSelector )
77
- if ! selector .Matches (labels .Set (node .Labels )) {
78
- return false
79
- }
80
- }
81
-
82
- // 1. nil NodeSelector matches all nodes (i.e. does not filter out any nodes)
83
- // 2. nil []NodeSelectorTerm (equivalent to non-nil empty NodeSelector) matches no nodes
84
- // 3. zero-length non-nil []NodeSelectorTerm matches no nodes also, just for simplicity
85
- // 4. nil []NodeSelectorRequirement (equivalent to non-nil empty NodeSelectorTerm) matches no nodes
86
- // 5. zero-length non-nil []NodeSelectorRequirement matches no nodes also, just for simplicity
87
- // 6. non-nil empty NodeSelectorRequirement is not allowed
88
-
89
- affinity := pod .Spec .Affinity
90
- if affinity != nil && affinity .NodeAffinity != nil {
91
- nodeAffinity := affinity .NodeAffinity
92
- // if no required NodeAffinity requirements, will do no-op, means select all nodes.
93
- if nodeAffinity .RequiredDuringSchedulingIgnoredDuringExecution == nil {
94
- return true
95
- }
96
-
97
- // Match node selector for requiredDuringSchedulingIgnoredDuringExecution.
98
- if nodeAffinity .RequiredDuringSchedulingIgnoredDuringExecution != nil {
99
- klog .V (10 ).InfoS ("Match for RequiredDuringSchedulingIgnoredDuringExecution node selector" , "selector" , nodeAffinity .RequiredDuringSchedulingIgnoredDuringExecution )
100
- matches , err := corev1 .MatchNodeSelectorTerms (node , nodeAffinity .RequiredDuringSchedulingIgnoredDuringExecution )
101
- if err != nil {
102
- klog .ErrorS (err , "error parsing node selector" , "selector" , nodeAffinity .RequiredDuringSchedulingIgnoredDuringExecution )
103
- }
104
- return matches
105
- }
106
- }
107
- return true
64
+ nodeRequiredAffinity := nodeaffinity .GetRequiredNodeAffinity (pod )
65
+ return nodeRequiredAffinity .Match (node )
108
66
}
109
67
110
68
func uniqueSortNodeSelectorTerms (srcTerms []v1.NodeSelectorTerm ) []v1.NodeSelectorTerm {
0 commit comments