-
Notifications
You must be signed in to change notification settings - Fork 90
/
plugin.go
306 lines (263 loc) · 8.91 KB
/
plugin.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
/*
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 noderesourcetopology
import (
"fmt"
"strconv"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
quotav1 "k8s.io/apiserver/pkg/quota/v1"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/apis/core/v1/helper/qos"
"k8s.io/kubernetes/pkg/scheduler/apis/config"
"k8s.io/kubernetes/pkg/scheduler/framework"
"github.com/kubewharf/katalyst-api/pkg/apis/node/v1alpha1"
apisconfig "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/eventhandlers"
"github.com/kubewharf/katalyst-core/pkg/scheduler/util"
"github.com/kubewharf/katalyst-core/pkg/util/native"
)
const (
TopologyMatchName = "NodeResourceTopology"
)
var nativeAlignedResources = sets.NewString()
type (
filterFn func(*v1.Pod, []*v1alpha1.TopologyZone, *framework.NodeInfo) *framework.Status
scoringFn func(*v1.Pod, []*v1alpha1.TopologyZone) (int64, *framework.Status)
)
type NUMANode struct {
SocketID string
NUMAID int
Capacity v1.ResourceList
Allocatable v1.ResourceList
Available v1.ResourceList
Costs map[int]int
}
type NUMANodeList []NUMANode
type TopologyMatch struct {
scoreStrategyFunc scoreStrategyFn
scoreStrategyType config.ScoringStrategyType
resourceToWeightMap resourceToWeightMap
alignedResources sets.String
resourcePolicy consts.ResourcePluginPolicyName
sharedLister framework.SharedLister
}
var (
_ framework.FilterPlugin = &TopologyMatch{}
_ framework.ScorePlugin = &TopologyMatch{}
_ framework.ReservePlugin = &TopologyMatch{}
_ framework.EnqueueExtensions = &TopologyMatch{}
)
// Name returns name of the plugin.
func (tm *TopologyMatch) Name() string {
return TopologyMatchName
}
func New(args runtime.Object, h framework.Handle) (framework.Plugin, error) {
klog.Info("Creating new TopologyMatch plugin")
klog.Infof("args: %+v", args)
tcfg, ok := args.(*apisconfig.NodeResourceTopologyArgs)
if !ok {
return nil, fmt.Errorf("want args to be of type NodeResourceTopologyArgs, got %T", args)
}
if err := validation.ValidateNodeResourceTopologyMatchArgs(nil, tcfg); err != nil {
return nil, err
}
resourceToWeightMap := make(resourceToWeightMap)
for _, resource := range tcfg.ScoringStrategy.Resources {
resourceToWeightMap[v1.ResourceName(resource.Name)] = resource.Weight
}
alignedResources := sets.NewString(tcfg.AlignedResources...)
strategy, err := getScoringStrategyFunction(tcfg.ScoringStrategy.Type)
if err != nil {
return nil, err
}
eventhandlers.RegisterCommonPodHandler()
eventhandlers.RegisterCommonCNRHandler()
return &TopologyMatch{
scoreStrategyType: tcfg.ScoringStrategy.Type,
alignedResources: alignedResources,
resourceToWeightMap: resourceToWeightMap,
scoreStrategyFunc: strategy,
resourcePolicy: tcfg.ResourcePluginPolicy,
sharedLister: h.SnapshotSharedLister(),
}, 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 (tm *TopologyMatch) EventsToRegister() []framework.ClusterEvent {
// To register a custom event, follow the naming convention at:
// https://git.k8s.io/kubernetes/pkg/scheduler/eventhandlers.go#L403-L410
cnrGVK := fmt.Sprintf("customnoderesources.v1alpha1.%v", v1alpha1.GroupName)
return []framework.ClusterEvent{
{Resource: framework.Pod, ActionType: framework.Delete},
{Resource: framework.Node, ActionType: framework.Add | framework.UpdateNodeAllocatable},
{Resource: framework.GVK(cnrGVK), ActionType: framework.Add | framework.Update},
}
}
func (tm *TopologyMatch) topologyMatchSupport(pod *v1.Pod) bool {
if tm.resourcePolicy == consts.ResourcePluginPolicyNameNative {
// native policy, only Guaranteed pod with full CPU supported
if qos.GetPodQOS(pod) == v1.PodQOSGuaranteed && util.IsRequestFullCPU(pod) {
return true
}
return false
}
if tm.resourcePolicy == consts.ResourcePluginPolicyNameDynamic {
// dynamic policy, only dedicated_cores with numaBinding supported
if util.IsDedicatedPod(pod) && util.IsNumaBinding(pod) {
return true
}
}
return false
}
func (tm *TopologyMatch) dedicatedPodsFilter(nodeInfo *framework.NodeInfo) func(consumer string) bool {
dedicatedPods := make(map[string]struct{})
for _, podInfo := range nodeInfo.Pods {
if util.IsDedicatedPod(podInfo.Pod) {
key := native.GenerateNamespaceNameKey(podInfo.Pod.Namespace, podInfo.Pod.Name)
dedicatedPods[key] = struct{}{}
}
}
return func(consumer string) bool {
namespace, name, _, err := native.ParseNamespaceNameUIDKey(consumer)
if err != nil {
klog.Errorf("ParseNamespaceNameUIDKey consumer %v fail: %v", consumer, err)
return false
}
// read only after map inited
key := native.GenerateNamespaceNameKey(namespace, name)
if _, ok := dedicatedPods[key]; ok {
return true
}
return false
}
}
func getScoringStrategyFunction(strategy config.ScoringStrategyType) (scoreStrategyFn, error) {
switch strategy {
case config.MostAllocated:
return mostAllocatedScoreStrategy, nil
case config.LeastAllocated:
return leastAllocatedScoreStrategy, nil
case consts.BalancedAllocation:
return balancedAllocationScoreStrategy, nil
case consts.LeastNUMANodes:
return nil, fmt.Errorf("LeastNUMANodes not support yet")
default:
return nil, fmt.Errorf("illegal scoring strategy found")
}
}
func TopologyZonesToNUMANodeList(zones []*v1alpha1.TopologyZone) NUMANodeList {
nodes := NUMANodeList{}
for _, topologyZone := range zones {
if topologyZone.Type != v1alpha1.TopologyTypeSocket {
continue
}
for _, child := range topologyZone.Children {
if child.Type != v1alpha1.TopologyTypeNuma {
continue
}
numaID, err := getID(child.Name)
if err != nil {
klog.Error(err)
continue
}
capacity, allocatable, available := extractAvailableResources(child)
nodes = append(nodes, NUMANode{
SocketID: topologyZone.Name,
NUMAID: numaID,
Capacity: capacity,
Allocatable: allocatable,
Available: available,
})
}
}
return nodes
}
func TopologyZonesToNUMANodeMap(zones []*v1alpha1.TopologyZone) map[int]NUMANode {
numaNodeMap := make(map[int]NUMANode)
for _, topologyZone := range zones {
if topologyZone.Type != v1alpha1.TopologyTypeSocket {
continue
}
for _, child := range topologyZone.Children {
if child.Type != v1alpha1.TopologyTypeNuma {
continue
}
numaID, err := getID(child.Name)
if err != nil {
klog.Error(err)
continue
}
capacity, allocatable, available := extractAvailableResources(child)
numaNodeMap[numaID] = NUMANode{
SocketID: topologyZone.Name,
NUMAID: numaID,
Capacity: capacity,
Allocatable: allocatable,
Available: available,
}
}
}
return numaNodeMap
}
func getID(name string) (int, error) {
numaID, err := strconv.Atoi(name)
if err != nil {
return -1, fmt.Errorf("invalid zone format zone: %s : %v", name, err)
}
if numaID > maxNUMAId-1 || numaID < 0 {
return -1, fmt.Errorf("invalid NUMA id range numaID: %d", numaID)
}
return numaID, nil
}
func extractAvailableResources(zone *v1alpha1.TopologyZone) (capacity, allocatable, available v1.ResourceList) {
used := make(v1.ResourceList)
for _, alloc := range zone.Allocations {
for resName, quantity := range *alloc.Requests {
if _, ok := used[resName]; !ok {
used[resName] = quantity.DeepCopy()
} else {
value := used[resName]
value.Add(quantity)
used[resName] = value
}
}
}
return zone.Resources.Capacity.DeepCopy(), zone.Resources.Allocatable.DeepCopy(), quotav1.SubtractWithNonNegativeResult(*zone.Resources.Allocatable, used)
}
func minNumaNodeCount(resourceName v1.ResourceName, quantity resource.Quantity, numaNodeMap map[int]NUMANode) int {
var (
i = 0
sumResource resource.Quantity
)
// allocatable in each numa may not equal because of resource reserve
for _, numaNode := range numaNodeMap {
i++
if i == 1 {
sumResource = numaNode.Capacity[resourceName]
} else {
sumResource.Add(numaNode.Capacity[resourceName])
}
if sumResource.Cmp(quantity) >= 0 {
return i
}
}
return i
}