-
Notifications
You must be signed in to change notification settings - Fork 104
/
fit.go
380 lines (324 loc) · 12.8 KB
/
fit.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
/*
Copyright 2022 The Katalyst Authors.
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 qosawarenoderesources
import (
"context"
"fmt"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
"k8s.io/kubernetes/pkg/scheduler/framework"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/feature"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/noderesources"
"github.com/kubewharf/katalyst-api/pkg/apis/scheduling/config"
"github.com/kubewharf/katalyst-api/pkg/apis/scheduling/config/validation"
"github.com/kubewharf/katalyst-api/pkg/consts"
"github.com/kubewharf/katalyst-core/pkg/scheduler/cache"
"github.com/kubewharf/katalyst-core/pkg/scheduler/eventhandlers"
"github.com/kubewharf/katalyst-core/pkg/scheduler/util"
"github.com/kubewharf/katalyst-core/pkg/util/native"
)
var (
_ framework.PreFilterPlugin = &Fit{}
_ framework.FilterPlugin = &Fit{}
_ framework.EnqueueExtensions = &Fit{}
_ framework.ScorePlugin = &Fit{}
_ framework.ReservePlugin = &Fit{}
)
const (
// FitName is the name of the plugin used in the plugin registry and configurations.
FitName = "QoSAwareNodeResourcesFit"
// preFilterStateKey is the key in CycleState to NodeResourcesFit pre-computed data.
// Using the name of the plugin will likely help us avoid collisions with other plugins.
preFilterStateKey = "PreFilter" + FitName
)
// nodeResourceStrategyTypeMap maps strategy to scorer implementation
var nodeResourceStrategyTypeMap = map[kubeschedulerconfig.ScoringStrategyType]scorer{
kubeschedulerconfig.LeastAllocated: func(args *config.QoSAwareNodeResourcesFitArgs) *resourceAllocationScorer {
resToWeightMap := resourcesToWeightMap(args.ScoringStrategy.ReclaimedResources)
return &resourceAllocationScorer{
Name: string(kubeschedulerconfig.LeastAllocated),
scorer: leastResourceScorer(resToWeightMap),
resourceToWeightMap: resToWeightMap,
}
},
kubeschedulerconfig.MostAllocated: func(args *config.QoSAwareNodeResourcesFitArgs) *resourceAllocationScorer {
resToWeightMap := resourcesToWeightMap(args.ScoringStrategy.ReclaimedResources)
return &resourceAllocationScorer{
Name: string(kubeschedulerconfig.MostAllocated),
scorer: mostResourceScorer(resToWeightMap),
resourceToWeightMap: resToWeightMap,
}
},
kubeschedulerconfig.RequestedToCapacityRatio: func(args *config.QoSAwareNodeResourcesFitArgs) *resourceAllocationScorer {
resToWeightMap := resourcesToWeightMap(args.ScoringStrategy.ReclaimedResources)
return &resourceAllocationScorer{
Name: string(kubeschedulerconfig.RequestedToCapacityRatio),
scorer: requestedToCapacityRatioScorer(resToWeightMap, args.ScoringStrategy.ReclaimedRequestedToCapacityRatio.Shape),
resourceToWeightMap: resToWeightMap,
}
},
}
// Fit is a plugin that checks if a node has sufficient resources.
type Fit struct {
handle framework.Handle
resourceAllocationScorer
nativeFit *noderesources.Fit
}
// ScoreExtensions of the Score plugin.
func (f *Fit) ScoreExtensions() framework.ScoreExtensions {
return nil
}
// preFilterState computed at PreFilter and used at Filter.
type preFilterState struct {
native.QoSResource
}
// Clone the prefilter state.
func (s *preFilterState) Clone() framework.StateData {
return s
}
// Name returns name of the plugin. It is used in logs, etc.
func (f *Fit) Name() string {
return FitName
}
// NewFit initializes a new plugin and returns it.
func NewFit(plArgs runtime.Object, h framework.Handle) (framework.Plugin, error) {
args, ok := plArgs.(*config.QoSAwareNodeResourcesFitArgs)
if !ok {
return nil, fmt.Errorf("want args to be of type NodeQoSResourcesFitArgs, got %T", plArgs)
}
if err := validation.ValidateQoSAwareNodeResourcesFitArgs(nil, args); err != nil {
return nil, err
}
if args.ScoringStrategy == nil {
return nil, fmt.Errorf("scoring strategy not specified")
}
strategy := args.ScoringStrategy.Type
scorePlugin, exists := nodeResourceStrategyTypeMap[strategy]
if !exists {
return nil, fmt.Errorf("scoring strategy %s is not supported", strategy)
}
nativeFit, err := newNativeFit(args, h)
if err != nil {
return nil, err
}
eventhandlers.RegisterCommonPodHandler()
eventhandlers.RegisterCommonCNRHandler()
return &Fit{
handle: h,
resourceAllocationScorer: *scorePlugin(args),
nativeFit: nativeFit,
}, nil
}
func newNativeFit(args *config.QoSAwareNodeResourcesFitArgs, h framework.Handle) (*noderesources.Fit, error) {
scoringStrategy := &kubeschedulerconfig.ScoringStrategy{
Type: args.ScoringStrategy.Type,
Resources: args.ScoringStrategy.Resources,
RequestedToCapacityRatio: args.ScoringStrategy.RequestedToCapacityRatio,
}
nativeFitPlugin, err := noderesources.NewFit(
&kubeschedulerconfig.NodeResourcesFitArgs{
ScoringStrategy: scoringStrategy,
}, h, feature.Features{},
)
if err != nil {
return nil, err
}
nativeFit, ok := nativeFitPlugin.(*noderesources.Fit)
if !ok {
return nil, fmt.Errorf("assert nativeFit type error, got %T", nativeFitPlugin)
}
return nativeFit, nil
}
// PreFilter invoked at the prefilter extension point.
func (f *Fit) PreFilter(ctx context.Context, cycleState *framework.CycleState, pod *v1.Pod) (*framework.PreFilterResult, *framework.Status) {
if !util.IsReclaimedPod(pod) {
return nil, nil
}
cycleState.Write(preFilterStateKey, computePodQoSResourceRequest(pod))
return nil, nil
}
// PreFilterExtensions returns prefilter extensions, pod add and remove.
func (f *Fit) PreFilterExtensions() framework.PreFilterExtensions {
return nil
}
// computePodQoSResourceRequest returns a framework.Resource that covers the largest
// width in each resource dimension. Because init-containers run sequentially, we collect
// the max in each dimension iteratively. In contrast, we sum the resource vectors for
// regular containers since they run simultaneously.
//
// the resources defined for Overhead should be added to the calculated QoSResource request sum
//
// example:
/*
// Pod:
// InitContainers
// IC1:
// CPU: 2
// Memory: 1G
// IC2:
// CPU: 2
// Memory: 3G
// Containers
// C1:
// CPU: 2
// Memory: 1G
// C2:
// CPU: 1
// Memory: 1G
//
// Result: CPU: 3, Memory: 3G
*/
func computePodQoSResourceRequest(pod *v1.Pod) *preFilterState {
result := &preFilterState{}
for _, container := range pod.Spec.Containers {
result.Add(container.Resources.Requests)
}
// take max_resource(sum_pod, any_init_container)
for _, container := range pod.Spec.InitContainers {
result.SetMaxResource(container.Resources.Requests)
}
// If Overhead is being utilized, add to the total requests for the pod
if pod.Spec.Overhead != nil {
result.Add(pod.Spec.Overhead)
}
return result
}
func getPreFilterState(cycleState *framework.CycleState) (*preFilterState, error) {
c, err := cycleState.Read(preFilterStateKey)
if err != nil {
// preFilterState doesn't exist, likely PreFilter wasn't invoked.
return nil, fmt.Errorf("error reading %q from cycleState: %w", preFilterStateKey, err)
}
s, ok := c.(*preFilterState)
if !ok {
return nil, fmt.Errorf("%+v convert to NodeQoSResourcesFit.preFilterState error", c)
}
return s, nil
}
// EventsToRegister returns the possible events that may make a Pod
// failed by this plugin schedulable.
// NOTE: if in-place-update (KEP 1287) gets implemented, then PodUpdate event
// should be registered for this plugin since a Pod update may free up resources
// that make other Pods schedulable.
func (f *Fit) EventsToRegister() []framework.ClusterEvent {
return []framework.ClusterEvent{
{Resource: framework.Pod, ActionType: framework.Delete},
{Resource: framework.Node, ActionType: framework.Add},
}
}
// Filter invoked at the filter extension point.
// Checks if a node has sufficient resources, such as cpu, memory, gpu, opaque int resources etc to run a pod.
// It returns a list of insufficient resources, if empty, then the node has all the resources requested by the pod.
func (f *Fit) Filter(ctx context.Context, cycleState *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) *framework.Status {
if !util.IsReclaimedPod(pod) {
return nil
}
s, err := getPreFilterState(cycleState)
if err != nil {
return framework.AsStatus(err)
}
insufficientResources := fitsRequest(s, nodeInfo)
if len(insufficientResources) != 0 {
// We will keep all failure reasons.
failureReasons := make([]string, 0, len(insufficientResources))
for i := range insufficientResources {
failureReasons = append(failureReasons, insufficientResources[i].Reason)
}
return framework.NewStatus(framework.Unschedulable, failureReasons...)
}
return nil
}
// InsufficientResource describes what kind of resource limit is hit and caused the pod to not fit the node.
type InsufficientResource struct {
ResourceName v1.ResourceName
// We explicitly have a parameter for reason to avoid formatting a message on the fly
// for common resources, which is expensive for cluster autoscaler simulations.
Reason string
Requested int64
Used int64
Capacity int64
}
func fitsRequest(podRequest *preFilterState, nodeInfo *framework.NodeInfo) []InsufficientResource {
insufficientResources := make([]InsufficientResource, 0, 2)
if podRequest.ReclaimedMilliCPU == 0 &&
podRequest.ReclaimedMemory == 0 {
return insufficientResources
}
extendedNodeInfo, err := cache.GetCache().GetNodeInfo(nodeInfo.Node().GetName())
if err != nil {
insufficientResources = append(insufficientResources,
InsufficientResource{
Reason: err.Error(),
},
)
return insufficientResources
}
extendedNodeInfo.Mutex.RLock()
defer extendedNodeInfo.Mutex.RUnlock()
if podRequest.ReclaimedMilliCPU > (extendedNodeInfo.QoSResourcesAllocatable.ReclaimedMilliCPU - extendedNodeInfo.QoSResourcesRequested.ReclaimedMilliCPU) {
insufficientResources = append(insufficientResources, InsufficientResource{
ResourceName: consts.ReclaimedResourceMilliCPU,
Reason: fmt.Sprintf("Insufficient %s", consts.ReclaimedResourceMilliCPU),
Requested: podRequest.ReclaimedMilliCPU,
Used: extendedNodeInfo.QoSResourcesRequested.ReclaimedMilliCPU,
Capacity: extendedNodeInfo.QoSResourcesAllocatable.ReclaimedMilliCPU,
})
}
if podRequest.ReclaimedMemory > (extendedNodeInfo.QoSResourcesAllocatable.ReclaimedMemory - extendedNodeInfo.QoSResourcesRequested.ReclaimedMemory) {
insufficientResources = append(insufficientResources, InsufficientResource{
ResourceName: consts.ReclaimedResourceMemory,
Reason: fmt.Sprintf("Insufficient %s", consts.ReclaimedResourceMemory),
Requested: podRequest.ReclaimedMemory,
Used: extendedNodeInfo.QoSResourcesRequested.ReclaimedMemory,
Capacity: extendedNodeInfo.QoSResourcesAllocatable.ReclaimedMemory,
})
}
return insufficientResources
}
// Score invoked at the Score extension point.
func (f *Fit) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) {
if util.IsReclaimedPod(pod) {
extendedNodeInfo, err := cache.GetCache().GetNodeInfo(nodeName)
if err != nil {
return 0, framework.AsStatus(fmt.Errorf("getting node %q error: %w", nodeName, err))
}
return f.score(pod, extendedNodeInfo, nodeName)
}
return f.nativeFit.Score(ctx, state, pod, nodeName)
}
// Reserve is the functions invoked by the framework at "Reserve" extension point.
func (f *Fit) Reserve(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) *framework.Status {
if !util.IsReclaimedPod(pod) || nodeName == "" || native.PodIsTerminated(pod) {
return nil
}
newPod := pod.DeepCopy()
newPod.Spec.NodeName = nodeName
if err := cache.GetCache().AddPod(newPod); err != nil {
return framework.NewStatus(framework.Unschedulable, fmt.Sprintf("extended cache reserve failed, err: %s", err.Error()))
}
return nil
}
// Unreserve is the functions invoked by the framework at "Unreserve" extension point.
func (f *Fit) Unreserve(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) {
if !util.IsReclaimedPod(pod) || nodeName == "" {
return
}
newPod := pod.DeepCopy()
newPod.Spec.NodeName = nodeName
if err := cache.GetCache().RemovePod(newPod); err != nil {
klog.ErrorS(err, "Unreserve failed to RemovePod",
"pod", klog.KObj(pod), "node", nodeName)
}
}