-
Notifications
You must be signed in to change notification settings - Fork 129
/
nodematching.go
250 lines (219 loc) · 7.94 KB
/
nodematching.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package schedulerobjects
import (
"fmt"
"github.com/segmentio/fasthash/fnv1a"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/component-helpers/scheduling/corev1"
)
const (
// When checking if a pod fits on a node, this score indicates how well the pods fits.
// However, all nodes are currently given the same score.
SchedulableScore = 0
SchedulableBestScore = SchedulableScore
PodRequirementsNotMetReasonUnmatchedNodeSelector = "node does not match pod NodeAffinity"
PodRequirementsNotMetReasonUnknown = "unknown"
PodRequirementsNotMetReasonInsufficientResources = "insufficient resources available"
)
type PodRequirementsNotMetReason interface {
fmt.Stringer
// Returns a 64-bit hash of this reason.
Sum64() uint64
}
type UntoleratedTaint struct {
Taint v1.Taint
}
func (r *UntoleratedTaint) Sum64() uint64 {
h := fnv1a.Init64
h = fnv1a.AddString64(h, r.Taint.Key)
h = fnv1a.AddString64(h, r.Taint.Value)
h = fnv1a.AddString64(h, string(r.Taint.Effect))
return h
}
func (r *UntoleratedTaint) String() string {
return fmt.Sprintf("taint %s=%s:%s not tolerated", r.Taint.Key, r.Taint.Value, r.Taint.Effect)
}
type MissingLabel struct {
Label string
}
func (r *MissingLabel) Sum64() uint64 {
h := fnv1a.Init64
h = fnv1a.AddString64(h, r.Label)
return h
}
func (r *MissingLabel) String() string {
return fmt.Sprintf("node does not match pod NodeSelector: label %s not set", r.Label)
}
type UnmatchedLabel struct {
Label string
PodValue string
NodeValue string
}
func (r *UnmatchedLabel) Sum64() uint64 {
h := fnv1a.Init64
h = fnv1a.AddString64(h, r.Label)
h = fnv1a.AddString64(h, r.PodValue)
h = fnv1a.AddString64(h, r.NodeValue)
return h
}
func (r *UnmatchedLabel) String() string {
return fmt.Sprintf("node does not match pod NodeSelector: required label %s = %s, but node has %s", r.Label, r.PodValue, r.NodeValue)
}
type UnmatchedNodeSelector struct {
NodeSelector *v1.NodeSelector
}
func (r *UnmatchedNodeSelector) Sum64() uint64 {
h := fnv1a.Init64
h = fnv1a.AddString64(h, PodRequirementsNotMetReasonUnmatchedNodeSelector)
return h
}
func (err *UnmatchedNodeSelector) String() string {
return PodRequirementsNotMetReasonUnmatchedNodeSelector
}
type InsufficientResources struct {
Resource string
Required resource.Quantity
Available resource.Quantity
}
func (r *InsufficientResources) Sum64() uint64 {
h := fnv1a.Init64
h = fnv1a.AddString64(h, r.Resource)
h = fnv1a.AddUint64(h, uint64(r.Required.MilliValue()))
h = fnv1a.AddUint64(h, uint64(r.Available.MilliValue()))
return h
}
func (err *InsufficientResources) String() string {
return "pod requires " + err.Required.String() + " " + err.Resource + ", but only " +
err.Available.String() + " is available"
}
// PodRequirementsMet determines whether a pod can be scheduled on nodes of this NodeType.
// If the requirements are not met, it returns the reason for why.
// If the requirements can't be parsed, an error is returned.
func (nodeType *NodeType) PodRequirementsMet(req *PodRequirements) (bool, PodRequirementsNotMetReason, error) {
matches, reason, err := podTolerationRequirementsMet(nodeType.GetTaints(), req)
if !matches || err != nil {
return matches, reason, err
}
return podNodeSelectorRequirementsMet(nodeType.GetLabels(), nodeType.GetUnsetIndexedLabels(), req)
}
// PodRequirementsMet determines whether a pod can be scheduled onto this node.
// If the pod can be scheduled, the returned score indicates how well the node fits:
// - 0: Pod can be scheduled by preempting running pods.
// - 1: Pod can be scheduled without preempting any running pods.
// If the requirements are not met, it returns the reason why.
// If the requirements can't be parsed, an error is returned.
func (node *Node) PodRequirementsMet(priority int32, req *PodRequirements) (bool, int, PodRequirementsNotMetReason, error) {
matches, reason, err := node.StaticPodRequirementsMet(req)
if !matches || err != nil {
return matches, 0, reason, err
}
return node.DynamicPodRequirementsMet(priority, req)
}
// StaticPodRequirementsMet checks if a pod can be scheduled onto this node,
// accounting for taints, node selectors, node affinity, and total resources available on the node.
func (node *Node) StaticPodRequirementsMet(req *PodRequirements) (bool, PodRequirementsNotMetReason, error) {
matches, reason, err := podTolerationRequirementsMet(node.GetTaints(), req)
if !matches || err != nil {
return matches, reason, err
}
matches, reason, err = podNodeSelectorRequirementsMet(node.GetLabels(), nil, req)
if !matches || err != nil {
return matches, reason, err
}
matches, reason, err = podNodeAffinityRequirementsMet(node.GetLabels(), req)
if !matches || err != nil {
return matches, reason, err
}
for resource, required := range req.ResourceRequirements.Requests {
available := node.TotalResources.Get(string(resource))
if required.Cmp(available) == 1 {
return false, &InsufficientResources{
Resource: string(resource),
Required: required,
Available: available,
}, nil
}
}
return true, nil, nil
}
// DynamicPodRequirementsMet checks if a pod can be scheduled onto this node,
// accounting for resources allocated to pods already assigned to this node.
func (node *Node) DynamicPodRequirementsMet(priority int32, req *PodRequirements) (bool, int, PodRequirementsNotMetReason, error) {
matches, reason, err := podResourceRequirementsMet(priority, node.AllocatableByPriorityAndResource, req)
return matches, SchedulableScore, reason, err
}
func podTolerationRequirementsMet(nodeTaints []v1.Taint, req *PodRequirements) (bool, PodRequirementsNotMetReason, error) {
untoleratedTaint, hasUntoleratedTaint := corev1.FindMatchingUntoleratedTaint(
nodeTaints,
req.Tolerations,
nil,
)
if hasUntoleratedTaint {
return false, &UntoleratedTaint{Taint: untoleratedTaint}, nil
}
return true, nil, nil
}
func podNodeSelectorRequirementsMet(nodeLabels, unsetIndexedLabels map[string]string, req *PodRequirements) (bool, PodRequirementsNotMetReason, error) {
for label, podValue := range req.NodeSelector {
// If the label value differs between nodeLabels and the pod,
// always return false.
if nodeValue, ok := nodeLabels[label]; ok {
if nodeValue != podValue {
return false, &UnmatchedLabel{
Label: label,
PodValue: podValue,
NodeValue: nodeValue,
}, nil
}
} else {
// If unsetIndexedLabels is provided, return false only if
// this label is explicitly marked as not set.
//
// If unsetIndexedLabels is not provided,
// we assume that nodeLabels contains all labels and return false.
if unsetIndexedLabels != nil {
if _, ok := unsetIndexedLabels[label]; ok {
return false, &MissingLabel{Label: label}, nil
}
} else {
return false, &MissingLabel{Label: label}, nil
}
}
}
return true, nil, nil
}
func podNodeAffinityRequirementsMet(nodeLabels map[string]string, req *PodRequirements) (bool, PodRequirementsNotMetReason, error) {
if affinityNodeSelector := req.GetAffinityNodeSelector(); affinityNodeSelector != nil {
matchesNodeSelector, err := corev1.MatchNodeSelectorTerms(
&v1.Node{
ObjectMeta: metav1.ObjectMeta{
Labels: nodeLabels,
},
},
affinityNodeSelector,
)
if err != nil {
return false, nil, err
}
if !matchesNodeSelector {
return false, &UnmatchedNodeSelector{
NodeSelector: affinityNodeSelector,
}, nil
}
}
return true, nil, nil
}
func podResourceRequirementsMet(priority int32, allocatableResources AllocatableByPriorityAndResourceType, req *PodRequirements) (bool, PodRequirementsNotMetReason, error) {
for resource, required := range req.ResourceRequirements.Requests {
available := allocatableResources.Get(priority, string(resource))
if required.Cmp(available) == 1 {
return false, &InsufficientResources{
Resource: string(resource),
Required: required,
Available: available,
}, nil
}
}
return true, nil, nil
}