-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathutil.go
461 lines (418 loc) · 13.3 KB
/
util.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
Copyright 2024 The HAMi 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 util
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"strconv"
"strings"
"time"
"github.com/Project-HAMi/HAMi/pkg/util/client"
"github.com/Project-HAMi/HAMi/pkg/util/nodelock"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
)
const (
// OneContainerMultiDeviceSplitSymbol this is when one container use multi device, use : symbol to join device info.
OneContainerMultiDeviceSplitSymbol = ":"
// OnePodMultiContainerSplitSymbol this is when one pod having multi container and more than one container use device, use ; symbol to join device info.
OnePodMultiContainerSplitSymbol = ";"
)
var (
InRequestDevices map[string]string
SupportDevices map[string]string
HandshakeAnnos map[string]string
)
func init() {
InRequestDevices = make(map[string]string)
SupportDevices = make(map[string]string)
HandshakeAnnos = make(map[string]string)
}
func GetNode(nodename string) (*corev1.Node, error) {
if nodename == "" {
klog.ErrorS(nil, "Node name is empty")
return nil, fmt.Errorf("nodename is empty")
}
klog.InfoS("Fetching node", "nodeName", nodename)
n, err := client.GetClient().CoreV1().Nodes().Get(context.Background(), nodename, metav1.GetOptions{})
if err != nil {
switch {
case apierrors.IsNotFound(err):
klog.ErrorS(err, "Node not found", "nodeName", nodename)
return nil, fmt.Errorf("node %s not found", nodename)
case apierrors.IsUnauthorized(err):
klog.ErrorS(err, "Unauthorized to access node", "nodeName", nodename)
return nil, fmt.Errorf("unauthorized to access node %s", nodename)
default:
klog.ErrorS(err, "Failed to get node", "nodeName", nodename)
return nil, fmt.Errorf("failed to get node %s: %v", nodename, err)
}
}
klog.InfoS("Successfully fetched node", "nodeName", nodename)
return n, nil
}
func GetPendingPod(ctx context.Context, node string) (*corev1.Pod, error) {
pod, err := GetAllocatePodByNode(ctx, node)
if err != nil {
return nil, err
}
if pod != nil {
return pod, nil
}
// filter pods for this node.
selector := fmt.Sprintf("spec.nodeName=%s", node)
podListOptions := metav1.ListOptions{
FieldSelector: selector,
}
podlist, err := client.GetClient().CoreV1().Pods("").List(ctx, podListOptions)
if err != nil {
return nil, err
}
for _, p := range podlist.Items {
if p.Status.Phase != corev1.PodPending {
continue
}
if _, ok := p.Annotations[BindTimeAnnotations]; !ok {
continue
}
if phase, ok := p.Annotations[DeviceBindPhase]; !ok {
continue
} else {
if strings.Compare(phase, DeviceBindAllocating) != 0 {
continue
}
}
if n, ok := p.Annotations[AssignedNodeAnnotations]; !ok {
continue
} else {
if strings.Compare(n, node) == 0 {
return &p, nil
}
}
}
return nil, fmt.Errorf("no binding pod found on node %s", node)
}
func GetAllocatePodByNode(ctx context.Context, nodeName string) (*corev1.Pod, error) {
node, err := client.GetClient().CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{})
if err != nil {
return nil, err
}
if value, ok := node.Annotations[nodelock.NodeLockKey]; ok {
klog.V(2).Infof("node annotation key is %s, value is %s ", nodelock.NodeLockKey, value)
_, ns, name, err := nodelock.ParseNodeLock(value)
if err != nil {
return nil, err
}
if ns == "" || name == "" {
return nil, nil
}
return client.GetClient().CoreV1().Pods(ns).Get(ctx, name, metav1.GetOptions{})
}
return nil, nil
}
func DecodeNodeDevices(str string) ([]*DeviceInfo, error) {
if !strings.Contains(str, OneContainerMultiDeviceSplitSymbol) {
return []*DeviceInfo{}, errors.New("node annotations not decode successfully")
}
tmp := strings.Split(str, OneContainerMultiDeviceSplitSymbol)
var retval []*DeviceInfo
for _, val := range tmp {
if strings.Contains(val, ",") {
items := strings.Split(val, ",")
if len(items) == 7 || len(items) == 9 {
count, _ := strconv.ParseInt(items[1], 10, 32)
devmem, _ := strconv.ParseInt(items[2], 10, 32)
devcore, _ := strconv.ParseInt(items[3], 10, 32)
health, _ := strconv.ParseBool(items[6])
numa, _ := strconv.Atoi(items[5])
mode := "hami-core"
index := 0
if len(items) == 9 {
index, _ = strconv.Atoi(items[7])
mode = items[8]
}
i := DeviceInfo{
ID: items[0],
Count: int32(count),
Devmem: int32(devmem),
Devcore: int32(devcore),
Type: items[4],
Numa: numa,
Health: health,
Mode: mode,
Index: uint(index),
}
retval = append(retval, &i)
} else {
return []*DeviceInfo{}, errors.New("node annotations not decode successfully")
}
}
}
return retval, nil
}
func EncodeNodeDevices(dlist []*DeviceInfo) string {
builder := strings.Builder{}
for _, val := range dlist {
builder.WriteString(val.ID)
builder.WriteString(",")
builder.WriteString(strconv.FormatInt(int64(val.Count), 10))
builder.WriteString(",")
builder.WriteString(strconv.Itoa(int(val.Devmem)))
builder.WriteString(",")
builder.WriteString(strconv.Itoa(int(val.Devcore)))
builder.WriteString(",")
builder.WriteString(val.Type)
builder.WriteString(",")
builder.WriteString(strconv.Itoa(val.Numa))
builder.WriteString(",")
builder.WriteString(strconv.FormatBool(val.Health))
builder.WriteString(",")
builder.WriteString(strconv.Itoa(int(val.Index)))
builder.WriteString(",")
builder.WriteString(val.Mode)
builder.WriteString(OneContainerMultiDeviceSplitSymbol)
//tmp += val.ID + "," + strconv.FormatInt(int64(val.Count), 10) + "," + strconv.Itoa(int(val.Devmem)) + "," + strconv.Itoa(int(val.Devcore)) + "," + val.Type + "," + strconv.Itoa(val.Numa) + "," + strconv.FormatBool(val.Health) + "," + strconv.Itoa(val.Index) + OneContainerMultiDeviceSplitSymbol
}
tmp := builder.String()
klog.V(5).Infof("Encoded node Devices: %s", tmp)
return tmp
}
func MarshalNodeDevices(dlist []*DeviceInfo) string {
data, err := json.Marshal(dlist)
if err != nil {
return ""
}
return string(data)
}
func UnMarshalNodeDevices(str string) ([]*DeviceInfo, error) {
var dlist []*DeviceInfo
err := json.Unmarshal([]byte(str), &dlist)
return dlist, err
}
func EncodeContainerDevices(cd ContainerDevices) string {
tmp := ""
for _, val := range cd {
tmp += val.UUID + "," + val.Type + "," + strconv.Itoa(int(val.Usedmem)) + "," + strconv.Itoa(int(val.Usedcores)) + OneContainerMultiDeviceSplitSymbol
}
klog.Infof("Encoded container Devices: %s", tmp)
return tmp
//return strings.Join(cd, ",")
}
func EncodeContainerDeviceType(cd ContainerDevices, t string) string {
tmp := ""
for _, val := range cd {
if strings.Compare(val.Type, t) == 0 {
tmp += val.UUID + "," + val.Type + "," + strconv.Itoa(int(val.Usedmem)) + "," + strconv.Itoa(int(val.Usedcores))
}
tmp += OneContainerMultiDeviceSplitSymbol
}
klog.Infof("Encoded container Certain Device type: %s->%s", t, tmp)
return tmp
}
func EncodePodSingleDevice(pd PodSingleDevice) string {
res := ""
for _, ctrdevs := range pd {
res = res + EncodeContainerDevices(ctrdevs)
res = res + OnePodMultiContainerSplitSymbol
}
klog.Infof("Encoded pod single devices %s", res)
return res
}
func EncodePodDevices(checklist map[string]string, pd PodDevices) map[string]string {
res := map[string]string{}
for devType, cd := range pd {
klog.Infoln("devtype=", devType)
res[checklist[devType]] = EncodePodSingleDevice(cd)
}
klog.Infof("Encoded pod Devices %s\n", res)
return res
}
func DecodeContainerDevices(str string) (ContainerDevices, error) {
if len(str) == 0 {
return ContainerDevices{}, nil
}
cd := strings.Split(str, OneContainerMultiDeviceSplitSymbol)
contdev := ContainerDevices{}
tmpdev := ContainerDevice{}
klog.V(5).Infof("Start to decode container device %s", str)
if len(str) == 0 {
return ContainerDevices{}, nil
}
for _, val := range cd {
if strings.Contains(val, ",") {
//fmt.Println("cd is ", val)
tmpstr := strings.Split(val, ",")
if len(tmpstr) < 4 {
return ContainerDevices{}, fmt.Errorf("pod annotation format error; information missing, please do not use nodeName field in task")
}
tmpdev.UUID = tmpstr[0]
tmpdev.Type = tmpstr[1]
devmem, _ := strconv.ParseInt(tmpstr[2], 10, 32)
tmpdev.Usedmem = int32(devmem)
devcores, _ := strconv.ParseInt(tmpstr[3], 10, 32)
tmpdev.Usedcores = int32(devcores)
contdev = append(contdev, tmpdev)
}
}
klog.V(5).Infof("Finished decoding container devices. Total devices: %d", len(contdev))
return contdev, nil
}
func DecodePodDevices(checklist map[string]string, annos map[string]string) (PodDevices, error) {
klog.V(5).Infof("checklist is [%+v], annos is [%+v]", checklist, annos)
if len(annos) == 0 {
return PodDevices{}, nil
}
pd := make(PodDevices)
for devID, devs := range checklist {
str, ok := annos[devs]
if !ok {
continue
}
pd[devID] = make(PodSingleDevice, 0)
for _, s := range strings.Split(str, OnePodMultiContainerSplitSymbol) {
cd, err := DecodeContainerDevices(s)
if err != nil {
return PodDevices{}, nil
}
if len(cd) == 0 {
continue
}
pd[devID] = append(pd[devID], cd)
}
}
klog.InfoS("Decoded pod annos", "poddevices", pd)
return pd, nil
}
func PatchNodeAnnotations(node *corev1.Node, annotations map[string]string) error {
type patchMetadata struct {
Annotations map[string]string `json:"annotations,omitempty"`
}
type patchPod struct {
Metadata patchMetadata `json:"metadata"`
//Spec patchSpec `json:"spec,omitempty"`
}
p := patchPod{}
p.Metadata.Annotations = annotations
bytes, err := json.Marshal(p)
if err != nil {
return err
}
_, err = client.GetClient().CoreV1().Nodes().
Patch(context.Background(), node.Name, k8stypes.StrategicMergePatchType, bytes, metav1.PatchOptions{})
if err != nil {
klog.Infoln("annotations=", annotations)
klog.Infof("patch pod %v failed, %v", node.Name, err)
}
return err
}
func PatchPodAnnotations(pod *corev1.Pod, annotations map[string]string) error {
type patchMetadata struct {
Annotations map[string]string `json:"annotations,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}
type patchPod struct {
Metadata patchMetadata `json:"metadata"`
//Spec patchSpec `json:"spec,omitempty"`
}
p := patchPod{}
p.Metadata.Annotations = annotations
label := make(map[string]string)
if v, ok := annotations[AssignedNodeAnnotations]; ok && v != "" {
label[AssignedNodeAnnotations] = v
p.Metadata.Labels = label
}
bytes, err := json.Marshal(p)
if err != nil {
return err
}
klog.V(5).Infof("patch pod %s/%s annotation content is %s", pod.Namespace, pod.Name, string(bytes))
_, err = client.GetClient().CoreV1().Pods(pod.Namespace).
Patch(context.Background(), pod.Name, k8stypes.StrategicMergePatchType, bytes, metav1.PatchOptions{})
if err != nil {
klog.Infof("patch pod %v failed, %v", pod.Name, err)
}
return err
}
func InitKlogFlags() *flag.FlagSet {
// Init log flags
flagset := flag.NewFlagSet("klog", flag.ExitOnError)
klog.InitFlags(flagset)
return flagset
}
func CheckHealth(devType string, n *corev1.Node) (bool, bool) {
handshake := n.Annotations[HandshakeAnnos[devType]]
if strings.Contains(handshake, "Requesting") {
formertime, _ := time.Parse(time.DateTime, strings.Split(handshake, "_")[1])
return time.Now().Before(formertime.Add(time.Second * 60)), false
} else if strings.Contains(handshake, "Deleted") {
return true, false
} else {
return true, true
}
}
func MarkAnnotationsToDelete(devType string, nn string) error {
tmppat := make(map[string]string)
tmppat[devType] = "Deleted_" + time.Now().Format(time.DateTime)
n, err := GetNode(nn)
if err != nil {
klog.Errorln("get node failed", err.Error())
return err
}
return PatchNodeAnnotations(n, tmppat)
}
// Enhanced ExtractMigTemplatesFromUUID with error handling.
func ExtractMigTemplatesFromUUID(uuid string) (int, int, error) {
parts := strings.Split(uuid, "[")
if len(parts) < 2 {
return -1, -1, fmt.Errorf("invalid UUID format: missing '[' delimiter")
}
tmp := parts[1]
parts = strings.Split(tmp, "]")
if len(parts) < 2 {
return -1, -1, fmt.Errorf("invalid UUID format: missing ']' delimiter")
}
tmp = parts[0]
parts = strings.Split(tmp, "-")
if len(parts) < 2 {
return -1, -1, fmt.Errorf("invalid UUID format: missing '-' delimiter")
}
templateIdx, err := strconv.Atoi(parts[0])
if err != nil {
return -1, -1, fmt.Errorf("invalid template index: %v", err)
}
pos, err := strconv.Atoi(parts[1])
if err != nil {
return -1, -1, fmt.Errorf("invalid position: %v", err)
}
return templateIdx, pos, nil
}
func PlatternMIG(n *MigInUse, templates []Geometry, templateIdx int) {
for _, val := range templates[templateIdx] {
count := 0
for count < int(val.Count) {
n.Index = int32(templateIdx)
n.UsageList = append(n.UsageList, MigTemplateUsage{
Name: val.Name,
Memory: val.Memory,
InUse: false,
})
count++
}
}
}