-
Notifications
You must be signed in to change notification settings - Fork 137
/
scheduler.go
381 lines (348 loc) · 14.3 KB
/
scheduler.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scheduling
import (
"bytes"
"context"
"fmt"
"sort"
"time"
"github.com/samber/lo"
"go.uber.org/multierr"
v1 "k8s.io/api/core/v1"
"knative.dev/pkg/logging"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/karpenter/pkg/apis/v1beta1"
"sigs.k8s.io/karpenter/pkg/cloudprovider"
"sigs.k8s.io/karpenter/pkg/controllers/state"
"sigs.k8s.io/karpenter/pkg/events"
"sigs.k8s.io/karpenter/pkg/metrics"
"sigs.k8s.io/karpenter/pkg/scheduling"
"sigs.k8s.io/karpenter/pkg/utils/pod"
"sigs.k8s.io/karpenter/pkg/utils/pretty"
"sigs.k8s.io/karpenter/pkg/utils/resources"
)
// SchedulerOptions can be used to control the scheduling, these options are currently only used during consolidation.
type SchedulerOptions struct {
// SimulationMode if true will prevent recording of the pod nomination decisions as events
SimulationMode bool
}
func NewScheduler(ctx context.Context, kubeClient client.Client, nodeClaimTemplates []*NodeClaimTemplate,
nodePools []v1beta1.NodePool, cluster *state.Cluster, stateNodes []*state.StateNode, topology *Topology,
instanceTypes map[string][]*cloudprovider.InstanceType, daemonSetPods []*v1.Pod,
recorder events.Recorder, opts SchedulerOptions) *Scheduler {
// if any of the nodePools add a taint with a prefer no schedule effect, we add a toleration for the taint
// during preference relaxation
toleratePreferNoSchedule := false
for _, prov := range nodePools {
for _, taint := range prov.Spec.Template.Spec.Taints {
if taint.Effect == v1.TaintEffectPreferNoSchedule {
toleratePreferNoSchedule = true
}
}
}
s := &Scheduler{
ctx: ctx,
kubeClient: kubeClient,
nodeClaimTemplates: nodeClaimTemplates,
topology: topology,
cluster: cluster,
instanceTypes: instanceTypes,
daemonOverhead: getDaemonOverhead(nodeClaimTemplates, daemonSetPods),
recorder: recorder,
opts: opts,
preferences: &Preferences{ToleratePreferNoSchedule: toleratePreferNoSchedule},
remainingResources: map[string]v1.ResourceList{},
}
for _, nodePool := range nodePools {
s.remainingResources[nodePool.Name] = v1.ResourceList(nodePool.Spec.Limits)
}
s.calculateExistingNodeClaims(stateNodes, daemonSetPods)
return s
}
type Scheduler struct {
ctx context.Context
newNodeClaims []*NodeClaim
existingNodes []*ExistingNode
nodeClaimTemplates []*NodeClaimTemplate
remainingResources map[string]v1.ResourceList // (NodePool name) -> remaining resources for that NodePool
instanceTypes map[string][]*cloudprovider.InstanceType // (NodePool name) -> instance types for NodePool
daemonOverhead map[*NodeClaimTemplate]v1.ResourceList
preferences *Preferences
topology *Topology
cluster *state.Cluster
recorder events.Recorder
opts SchedulerOptions
kubeClient client.Client
}
// Results contains the results of the scheduling operation
type Results struct {
NewNodeClaims []*NodeClaim
ExistingNodes []*ExistingNode
PodErrors map[*v1.Pod]error
}
// AllNonPendingPodsScheduled returns true if all pods scheduled.
// We don't care if a pod was pending before consolidation and will still be pending after. It may be a pod that we can't
// schedule at all and don't want it to block consolidation.
func (r Results) AllNonPendingPodsScheduled() bool {
return len(lo.OmitBy(r.PodErrors, func(p *v1.Pod, err error) bool {
return pod.IsProvisionable(p)
})) == 0
}
// NonPendingPodSchedulingErrors creates a string that describes why pods wouldn't schedule that is suitable for presentation
func (r Results) NonPendingPodSchedulingErrors() string {
errs := lo.OmitBy(r.PodErrors, func(p *v1.Pod, err error) bool {
return pod.IsProvisionable(p)
})
if len(errs) == 0 {
return "No Pod Scheduling Errors"
}
var msg bytes.Buffer
fmt.Fprintf(&msg, "not all pods would schedule, ")
const MaxErrors = 5
numErrors := 0
for k, err := range errs {
fmt.Fprintf(&msg, "%s/%s => %s ", k.Namespace, k.Name, err)
numErrors++
if numErrors >= MaxErrors {
fmt.Fprintf(&msg, " and %d other(s)", len(errs)-MaxErrors)
break
}
}
return msg.String()
}
func (s *Scheduler) Solve(ctx context.Context, pods []*v1.Pod) *Results {
defer metrics.Measure(schedulingSimulationDuration)()
schedulingStart := time.Now()
// We loop trying to schedule unschedulable pods as long as we are making progress. This solves a few
// issues including pods with affinity to another pod in the batch. We could topo-sort to solve this, but it wouldn't
// solve the problem of scheduling pods where a particular order is needed to prevent a max-skew violation. E.g. if we
// had 5xA pods and 5xB pods were they have a zonal topology spread, but A can only go in one zone and B in another.
// We need to schedule them alternating, A, B, A, B, .... and this solution also solves that as well.
errors := map[*v1.Pod]error{}
q := NewQueue(pods...)
for {
// Try the next pod
pod, ok := q.Pop()
if !ok {
break
}
// Schedule to existing nodes or create a new node
if errors[pod] = s.add(ctx, pod); errors[pod] == nil {
continue
}
// If unsuccessful, relax the pod and recompute topology
relaxed := s.preferences.Relax(ctx, pod)
q.Push(pod, relaxed)
if relaxed {
if err := s.topology.Update(ctx, pod); err != nil {
logging.FromContext(ctx).Errorf("updating topology, %s", err)
}
}
}
for _, m := range s.newNodeClaims {
m.FinalizeScheduling()
}
if !s.opts.SimulationMode {
s.recordSchedulingResults(ctx, pods, q.List(), errors, time.Since(schedulingStart))
}
// clear any nil errors so we can know that len(PodErrors) == 0 => all pods scheduled
for k, v := range errors {
if v == nil {
delete(errors, k)
}
}
return &Results{
NewNodeClaims: s.newNodeClaims,
ExistingNodes: s.existingNodes,
PodErrors: errors,
}
}
func (s *Scheduler) recordSchedulingResults(ctx context.Context, pods []*v1.Pod, failedToSchedule []*v1.Pod, errors map[*v1.Pod]error, schedulingDuration time.Duration) {
// Report failures and nominations
for _, pod := range failedToSchedule {
logging.FromContext(ctx).With("pod", client.ObjectKeyFromObject(pod)).Errorf("Could not schedule pod, %s", errors[pod])
s.recorder.Publish(PodFailedToScheduleEvent(pod, errors[pod]))
}
for _, existing := range s.existingNodes {
if len(existing.Pods) > 0 {
s.cluster.NominateNodeForPod(ctx, existing.ProviderID())
}
for _, pod := range existing.Pods {
s.recorder.Publish(NominatePodEvent(pod, existing.Node, existing.NodeClaim))
}
}
// Report new nodes, or exit to avoid log spam
newCount := 0
for _, nodeClaim := range s.newNodeClaims {
newCount += len(nodeClaim.Pods)
}
if newCount == 0 {
return
}
var podNames []string
for _, p := range pods {
podNames = append(podNames, fmt.Sprintf("%s/%s", p.Namespace, p.Name))
}
logging.FromContext(ctx).With("pods", pretty.Slice(podNames, 5)).
With("duration", schedulingDuration).
Infof("found provisionable pod(s)")
logging.FromContext(ctx).With("nodeclaims", len(s.newNodeClaims), "pods", newCount).Infof("computed new nodeclaim(s) to fit pod(s)")
// Report in flight newNodes, or exit to avoid log spam
inflightCount := 0
existingCount := 0
for _, node := range lo.Filter(s.existingNodes, func(node *ExistingNode, _ int) bool { return len(node.Pods) > 0 }) {
inflightCount++
existingCount += len(node.Pods)
}
if existingCount == 0 {
return
}
logging.FromContext(ctx).Infof("computed %d unready node(s) will fit %d pod(s)", inflightCount, existingCount)
}
func (s *Scheduler) add(ctx context.Context, pod *v1.Pod) error {
// first try to schedule against an in-flight real node
for _, node := range s.existingNodes {
if err := node.Add(ctx, s.kubeClient, pod); err == nil {
return nil
}
}
// Consider using https://pkg.go.dev/container/heap
sort.Slice(s.newNodeClaims, func(a, b int) bool { return len(s.newNodeClaims[a].Pods) < len(s.newNodeClaims[b].Pods) })
// Pick existing node that we are about to create
for _, nodeClaim := range s.newNodeClaims {
if err := nodeClaim.Add(pod); err == nil {
return nil
}
}
// Create new node
var errs error
for _, nodeClaimTemplate := range s.nodeClaimTemplates {
instanceTypes := s.instanceTypes[nodeClaimTemplate.NodePoolName]
// if limits have been applied to the nodepool, ensure we filter instance types to avoid violating those limits
if remaining, ok := s.remainingResources[nodeClaimTemplate.NodePoolName]; ok {
instanceTypes = filterByRemainingResources(s.instanceTypes[nodeClaimTemplate.NodePoolName], remaining)
if len(instanceTypes) == 0 {
errs = multierr.Append(errs, fmt.Errorf("all available instance types exceed limits for nodepool: %q", nodeClaimTemplate.NodePoolName))
continue
} else if len(s.instanceTypes[nodeClaimTemplate.NodePoolName]) != len(instanceTypes) && !s.opts.SimulationMode {
logging.FromContext(ctx).With("nodepool", nodeClaimTemplate.NodePoolName).Debugf("%d out of %d instance types were excluded because they would breach limits",
len(s.instanceTypes[nodeClaimTemplate.NodePoolName])-len(instanceTypes), len(s.instanceTypes[nodeClaimTemplate.NodePoolName]))
}
}
nodeClaim := NewNodeClaim(nodeClaimTemplate, s.topology, s.daemonOverhead[nodeClaimTemplate], instanceTypes)
if err := nodeClaim.Add(pod); err != nil {
errs = multierr.Append(errs, fmt.Errorf("incompatible with nodepool %q, daemonset overhead=%s, %w",
nodeClaimTemplate.NodePoolName,
resources.String(s.daemonOverhead[nodeClaimTemplate]),
err))
continue
}
// we will launch this nodeClaim and need to track its maximum possible resource usage against our remaining resources
s.newNodeClaims = append(s.newNodeClaims, nodeClaim)
s.remainingResources[nodeClaimTemplate.NodePoolName] = subtractMax(s.remainingResources[nodeClaimTemplate.NodePoolName], nodeClaim.InstanceTypeOptions)
return nil
}
return errs
}
func (s *Scheduler) calculateExistingNodeClaims(stateNodes []*state.StateNode, daemonSetPods []*v1.Pod) {
// create our existing nodes
for _, node := range stateNodes {
// Calculate any daemonsets that should schedule to the inflight node
var daemons []*v1.Pod
for _, p := range daemonSetPods {
if err := scheduling.Taints(node.Taints()).Tolerates(p); err != nil {
continue
}
if err := scheduling.NewLabelRequirements(node.Labels()).Compatible(scheduling.NewPodRequirements(p)); err != nil {
continue
}
daemons = append(daemons, p)
}
s.existingNodes = append(s.existingNodes, NewExistingNode(node, s.topology, resources.RequestsForPods(daemons...)))
// We don't use the status field and instead recompute the remaining resources to ensure we have a consistent view
// of the cluster during scheduling. Depending on how node creation falls out, this will also work for cases where
// we don't create NodeClaim resources.
if _, ok := s.remainingResources[node.Labels()[v1beta1.NodePoolLabelKey]]; ok {
s.remainingResources[node.Labels()[v1beta1.NodePoolLabelKey]] = resources.Subtract(s.remainingResources[node.Labels()[v1beta1.NodePoolLabelKey]], node.Capacity())
}
}
// Order the existing nodes for scheduling with initialized nodes first
// This is done specifically for consolidation where we want to make sure we schedule to initialized nodes
// before we attempt to schedule un-initialized ones
sort.SliceStable(s.existingNodes, func(i, j int) bool {
if s.existingNodes[i].Initialized() && !s.existingNodes[j].Initialized() {
return true
}
if !s.existingNodes[i].Initialized() && s.existingNodes[j].Initialized() {
return false
}
return s.existingNodes[i].Name() < s.existingNodes[j].Name()
})
}
func getDaemonOverhead(nodeClaimTemplates []*NodeClaimTemplate, daemonSetPods []*v1.Pod) map[*NodeClaimTemplate]v1.ResourceList {
overhead := map[*NodeClaimTemplate]v1.ResourceList{}
for _, nodeClaimTemplate := range nodeClaimTemplates {
var daemons []*v1.Pod
for _, p := range daemonSetPods {
if err := scheduling.Taints(nodeClaimTemplate.Spec.Taints).Tolerates(p); err != nil {
continue
}
if err := nodeClaimTemplate.Requirements.Compatible(scheduling.NewPodRequirements(p), scheduling.AllowUndefinedWellKnownLabels); err != nil {
continue
}
daemons = append(daemons, p)
}
overhead[nodeClaimTemplate] = resources.RequestsForPods(daemons...)
}
return overhead
}
// subtractMax returns the remaining resources after subtracting the max resource quantity per instance type. To avoid
// overshooting out, we need to pessimistically assume that if e.g. we request a 2, 4 or 8 CPU instance type
// that the 8 CPU instance type is all that will be available. This could cause a batch of pods to take multiple rounds
// to schedule.
func subtractMax(remaining v1.ResourceList, instanceTypes []*cloudprovider.InstanceType) v1.ResourceList {
// shouldn't occur, but to be safe
if len(instanceTypes) == 0 {
return remaining
}
var allInstanceResources []v1.ResourceList
for _, it := range instanceTypes {
allInstanceResources = append(allInstanceResources, it.Capacity)
}
result := v1.ResourceList{}
itResources := resources.MaxResources(allInstanceResources...)
for k, v := range remaining {
cp := v.DeepCopy()
cp.Sub(itResources[k])
result[k] = cp
}
return result
}
// filterByRemainingResources is used to filter out instance types that if launched would exceed the nodepool limits
func filterByRemainingResources(instanceTypes []*cloudprovider.InstanceType, remaining v1.ResourceList) []*cloudprovider.InstanceType {
var filtered []*cloudprovider.InstanceType
for _, it := range instanceTypes {
itResources := it.Capacity
viableInstance := true
for resourceName, remainingQuantity := range remaining {
// if the instance capacity is greater than the remaining quantity for this resource
if resources.Cmp(itResources[resourceName], remainingQuantity) > 0 {
viableInstance = false
}
}
if viableInstance {
filtered = append(filtered, it)
}
}
return filtered
}