-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_service.go
264 lines (227 loc) · 7.8 KB
/
node_service.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
/*
Copyright @ 2021 fushaosong <fushaosong@beyondlet.com>.
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 k8s
import (
"github.com/bocloud/carina/pkg/configuration"
"github.com/bocloud/carina/pkg/csidriver/csi"
"github.com/bocloud/carina/utils"
"context"
"errors"
"fmt"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/rand"
"sort"
"strings"
"time"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
// This annotation is present on K8s 1.11 release.
const annAlphaSelectedNode = "volume.alpha.kubernetes.io/selected-node"
type nodeService interface {
getNodes(ctx context.Context) (*corev1.NodeList, error)
// 支持 volume size 及 topology match
SelectVolumeNode(ctx context.Context, request int64, deviceGroup string, requirement *csi.TopologyRequirement) (string, string, map[string]string, error)
GetCapacityByNodeName(ctx context.Context, nodeName, deviceGroup string) (int64, error)
GetTotalCapacity(ctx context.Context, deviceGroup string, topology *csi.Topology) (int64, error)
SelectDeviceGroup(ctx context.Context, request int64, nodeName string) (string, error)
// sc WaitForConsumer
HaveSelectedNode(ctx context.Context, namespace, name string) (string, error)
}
// ErrNodeNotFound represents the error that node is not found.
var ErrNodeNotFound = errors.New("node not found")
// NodeService represents node service.
type NodeService struct {
client.Client
}
// NewNodeService returns NodeService.
func NewNodeService(mgr manager.Manager) *NodeService {
return &NodeService{Client: mgr.GetClient()}
}
func (s NodeService) getNodes(ctx context.Context) (*corev1.NodeList, error) {
nl := new(corev1.NodeList)
err := s.List(ctx, nl)
if err != nil {
return nil, err
}
return nl, nil
}
func (s NodeService) SelectVolumeNode(ctx context.Context, requestGb int64, deviceGroup string, requirement *csi.TopologyRequirement) (string, string, map[string]string, error) {
// 在并发场景下,兼顾调度效率与调度公平,将pv分配到不同时间段
time.Sleep(time.Duration(rand.Int63nRange(1, 30)) * time.Second)
var nodeName, selectDeviceGroup string
segments := map[string]string{}
nl, err := s.getNodes(ctx)
if err != nil {
return "", "", segments, err
}
type paris struct {
Key string
Value int64
}
preselectNode := []paris{}
for _, node := range nl.Items {
// topology selector
// 若是sc配置了allowedTopologies,在此过滤出符合条件的node
if requirement != nil {
topologySelector := false
for _, topo := range requirement.GetRequisite() {
selector := labels.SelectorFromSet(topo.GetSegments())
if selector.Matches(labels.Set(node.Labels)) {
topologySelector = true
break
}
}
// 如果没有通过topology selector则节点不可用
if !topologySelector {
continue
}
}
// capacity selector
// 注册设备时有特殊前缀的,若是sc指定了设备组则过滤出所有节点上符合条件的设备组
for key, value := range node.Status.Allocatable {
if strings.HasPrefix(string(key), utils.DeviceCapacityKeyPrefix) {
if deviceGroup != "" && string(key) != deviceGroup && string(key) != utils.DeviceCapacityKeyPrefix+deviceGroup {
continue
}
if value.Value() < requestGb {
continue
}
preselectNode = append(preselectNode, paris{
Key: node.Name + "-*-" + string(key),
Value: value.Value(),
})
}
}
}
if len(preselectNode) < 1 {
return "", "", segments, ErrNodeNotFound
}
sort.Slice(preselectNode, func(i, j int) bool {
return preselectNode[i].Value < preselectNode[j].Value
})
// 根据配置文件中设置算法进行节点选择
if configuration.SchedulerStrategy() == configuration.SchedulerBinpack {
nodeName = strings.Split(preselectNode[0].Key, "-*-")[0]
selectDeviceGroup = strings.Split(preselectNode[0].Key, "/")[1]
} else if configuration.SchedulerStrategy() == configuration.SchedulerSpradout {
nodeName = strings.Split(preselectNode[len(preselectNode)-1].Key, "-*-")[0]
selectDeviceGroup = strings.Split(preselectNode[len(preselectNode)-1].Key, "/")[1]
} else {
return "", "", segments, errors.New(fmt.Sprintf("no support scheduler strategy %s", configuration.SchedulerStrategy()))
}
// 获取选择节点的label
for _, node := range nl.Items {
if node.Name == nodeName {
for _, topo := range requirement.GetRequisite() {
for k, _ := range topo.GetSegments() {
segments[k] = node.Labels[k]
}
}
}
}
return nodeName, selectDeviceGroup, segments, nil
}
// GetCapacityByNodeName returns VG capacity of specified node by name.
func (s NodeService) GetCapacityByNodeName(ctx context.Context, name, deviceGroup string) (int64, error) {
node := new(corev1.Node)
err := s.Get(ctx, client.ObjectKey{Name: name}, node)
if err != nil {
return 0, err
}
for key, v := range node.Status.Allocatable {
if string(key) == deviceGroup || string(key) == utils.DeviceCapacityKeyPrefix+deviceGroup {
return v.Value(), nil
}
}
return 0, errors.New("device group not found")
}
// GetTotalCapacity returns total VG capacity of all nodes.
func (s NodeService) GetTotalCapacity(ctx context.Context, deviceGroup string, topology *csi.Topology) (int64, error) {
nl, err := s.getNodes(ctx)
if err != nil {
return 0, err
}
capacity := int64(0)
for _, node := range nl.Items {
// topology selector
if topology != nil {
selector := labels.SelectorFromSet(topology.GetSegments())
if !selector.Matches(labels.Set(node.Labels)) {
continue
}
}
for key, v := range node.Status.Capacity {
if deviceGroup == "" && strings.HasPrefix(string(key), utils.DeviceCapacityKeyPrefix) {
capacity += v.Value()
} else if string(key) == deviceGroup || string(key) == utils.DeviceCapacityKeyPrefix+deviceGroup {
capacity += v.Value()
}
}
}
return capacity, nil
}
func (s NodeService) SelectDeviceGroup(ctx context.Context, request int64, nodeName string) (string, error) {
var selectDeviceGroup string
nl, err := s.getNodes(ctx)
if err != nil {
return "", err
}
type paris struct {
Key string
Value int64
}
preselectNode := []paris{}
for _, node := range nl.Items {
if nodeName != node.Name {
continue
}
// capacity selector
// 经过上层过滤,这里只会有一个节点
for key, value := range node.Status.Allocatable {
if strings.HasPrefix(string(key), utils.DeviceCapacityKeyPrefix) {
preselectNode = append(preselectNode, paris{
Key: string(key),
Value: value.Value(),
})
}
}
}
if len(preselectNode) < 1 {
return "", ErrNodeNotFound
}
sort.Slice(preselectNode, func(i, j int) bool {
return preselectNode[i].Value < preselectNode[j].Value
})
// 这里只能选最小满足的,因为可能存在一个pod多个pv都需要落在这个节点
for _, p := range preselectNode {
if p.Value >= request {
selectDeviceGroup = strings.Split(p.Key, "/")[1]
}
}
return selectDeviceGroup, nil
}
func (s NodeService) HaveSelectedNode(ctx context.Context, namespace, name string) (string, error) {
node := ""
pvc := new(corev1.PersistentVolumeClaim)
err := s.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, pvc)
if err != nil {
return node, err
}
node = pvc.Annotations[utils.AnnSelectedNode]
if node == "" {
node = pvc.Annotations[annAlphaSelectedNode]
}
return node, nil
}