-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcommon.go
706 lines (606 loc) · 22.4 KB
/
common.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*
* Copyright 2022- IBM Inc. All rights reserved
* SPDX-License-Identifier: Apache2.0
*/
package controllers
import (
"bytes"
"context"
"fmt"
"hash/fnv"
"io/ioutil"
"net/http"
"regexp"
"sort"
"strings"
"text/template"
cpev1 "github.com/IBM/cpe-operator/api/v1"
"github.com/go-logr/logr"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/dynamic/dynamicinformer"
"k8s.io/client-go/restmapper"
cache "k8s.io/client-go/tools/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var itrHandler *IterationHandler = &IterationHandler{}
const (
BENCHMARK_LABEL = "cpe-benchmark"
MAX_LABEL_LEN = 60
NODESELECT_ITR_NAME = "profile"
NODESELECT_ITR_DEFAULT = "default"
INIT_BUILD_NAME = "init"
BUILD_KEY = "build"
REPETITION_KEY = "repno"
JOBHASH_KEY = "cpe-jobhash"
HASH_DELIMIT = "-cpeh-"
INVALID_REGEX = "[^A-Za-z0-9]"
JOB_LOG_PATH = "/cpe-local-log"
)
func GetInformerFromGVK(dc *discovery.DiscoveryClient, dyn dynamic.Interface, gvk schema.GroupVersionKind) (cache.SharedIndexInformer, dynamicinformer.DynamicSharedInformerFactory) {
factory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(dyn, 0, v1.NamespaceAll, nil)
mapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(dc))
mapping, _ := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
informer := factory.ForResource(mapping.Resource)
s := informer.Informer()
return s, factory
}
func getResourceInterface(dc *discovery.DiscoveryClient, dyn dynamic.Interface, gvk *schema.GroupVersionKind, ns string) dynamic.ResourceInterface {
mapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(dc))
mapping, _ := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
var dr dynamic.ResourceInterface
if mapping.Scope.Name() == meta.RESTScopeNameNamespace {
dr = dyn.Resource(mapping.Resource).Namespace(ns)
} else {
dr = dyn.Resource(mapping.Resource)
}
return dr
}
func getInfoFromURL(dc *discovery.DiscoveryClient, dyn dynamic.Interface, yamlURL string) (*unstructured.Unstructured, dynamic.ResourceInterface, error) {
// 1. get yaml body from url
resp, err := http.Get(yamlURL)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}
// 2. get GVK
obj := &unstructured.Unstructured{}
dec := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
_, gvk, err := dec.Decode(bodyBytes, nil, obj)
if err != nil {
return nil, nil, err
}
dr := getResourceInterface(dc, dyn, gvk, obj.GetNamespace())
return obj, dr, nil
}
func CreateFromURL(dc *discovery.DiscoveryClient, dyn dynamic.Interface, yamlURL string) (*unstructured.Unstructured, error) {
obj, dr, err := getInfoFromURL(dc, dyn, yamlURL)
if err != nil {
return nil, err
}
instance, err := dr.Get(context.TODO(), obj.GetName(), metav1.GetOptions{})
if err != nil { // create if not exists
instance, err = dr.Create(context.TODO(), obj, metav1.CreateOptions{})
}
return instance, err
}
func DeleteFromURL(dc *discovery.DiscoveryClient, dyn dynamic.Interface, yamlURL string) error {
obj, dr, err := getInfoFromURL(dc, dyn, yamlURL)
if dr == nil {
return err
}
err = dr.Delete(context.TODO(), obj.GetName(), metav1.DeleteOptions{})
return err
}
func getValidValue(val string) string {
invalidReg, _ := regexp.Compile(INVALID_REGEX)
valSize := len(val)
firstChar := invalidReg.ReplaceAllString(val[0:1], "")
restChars := invalidReg.ReplaceAllString(val[1:valSize], "-")
val = firstChar + restChars
if len(val) > MAX_LABEL_LEN {
val = val[len(val)-MAX_LABEL_LEN:]
}
return val
}
func getIterationPairStr(key string, val string) string {
return fmt.Sprintf(";%s=%s", key, val)
}
func getSubfixFromIterationLabel(iterationLabel map[string]string) string {
subfix := ""
if len(iterationLabel) == 0 {
return ""
}
keys := make([]string, 0, len(iterationLabel))
for key := range iterationLabel {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
subfix += getIterationPairStr(key, iterationLabel[key])
}
return subfix
}
// func getJobName(benchmark *cpev1.Benchmark, iterationLabel map[string]string, build string, repetition int) string {
// return fmt.Sprintf("%s%s-bc-%s-rp-%d", benchmark.ObjectMeta.Name, getSubfixFromIterationLabel(iterationLabel), build, repetition)
// }
func getJobHash(iterationLabel map[string]string, build string, repetition int) string {
fullKey := fmt.Sprintf("%s-bc-%s-rp-%d", getSubfixFromIterationLabel(iterationLabel), build, repetition)
h := fnv.New32a()
h.Write([]byte(fullKey))
return fmt.Sprintf("%d", h.Sum32())
}
func getJobNameFromHash(benchmarkName string, jobHash string) string {
return benchmarkName + HASH_DELIMIT + jobHash
}
func getJobName(benchmark *cpev1.Benchmark, iterationLabel map[string]string, build string, repetition int) string {
jobHash := getJobHash(iterationLabel, build, repetition)
return getJobNameFromHash(benchmark.GetName(), jobHash)
}
func getOrderedKey(hashItem map[string]string) []string {
var orderedKeys []string
for k, _ := range hashItem {
orderedKeys = append(orderedKeys, k)
}
sort.Strings(orderedKeys)
return orderedKeys
}
func GetDetailFromJobName(jobName string, benchmark *cpev1.Benchmark) (benchmarkName string, iterationMap map[string]string, configurationMap map[string]string, repetition string, buildID string, iterationID string, configurationID string) {
iterations := benchmark.Spec.IterationSpec.Iteration
var configurations []cpev1.IterationItem
if benchmark.Spec.IterationSpec.NodeSelection != nil {
nodeSelectionItr := NodeSelectionSpecToIteration(benchmark.Spec.IterationSpec.NodeSelection)
configurations = append(benchmark.Spec.IterationSpec.Configuration, nodeSelectionItr)
} else {
configurations = benchmark.Spec.IterationSpec.Configuration
}
iterationMap = make(map[string]string)
configurationMap = make(map[string]string)
var iterationKeys map[string]interface{}
var configurationKeys map[string]interface{}
iterationKeys = make(map[string]interface{})
configurationKeys = make(map[string]interface{})
for _, item := range iterations {
iterationKeys[item.Name] = nil
}
for _, item := range configurations {
configurationKeys[item.Name] = nil
}
// init value
buildID = ""
iterationID = ""
configurationID = ""
repetition = "0"
if strings.Contains(jobName, HASH_DELIMIT) {
var orderedKeys []string
if len(benchmark.Status.Hash) > 0 {
orderedKeys = getOrderedKey(benchmark.Status.Hash[0].Iteration)
}
splited := strings.Split(jobName, HASH_DELIMIT)
targetHash := splited[len(splited)-1]
for _, hashItem := range benchmark.Status.Hash {
if hashItem.Hash == targetHash {
buildID = hashItem.Build
for _, key := range orderedKeys {
val := hashItem.Iteration[key]
if _, ok := iterationKeys[key]; ok {
iterationMap[key] = val
iterationID += getIterationPairStr(key, val)
} else if _, ok := configurationKeys[key]; ok {
configurationMap[key] = val
configurationID += getIterationPairStr(key, val)
}
}
repetition = hashItem.Repetition
break
}
}
}
if len(iterationID) > 0 {
iterationID = iterationID[1:]
}
if len(configurationID) > 0 {
configurationID = configurationID[1:]
}
return benchmark.GetName(), iterationMap, configurationMap, repetition, buildID, iterationID, configurationID
}
func GetSimpleJobGVK(benchmarkOperator *cpev1.BenchmarkOperator) schema.GroupVersionKind {
apiVersion := benchmarkOperator.Spec.APIVersion
kind := benchmarkOperator.Spec.Kind
benchmarkObj := make(map[string]interface{})
benchmarkObj["apiVersion"] = apiVersion
benchmarkObj["kind"] = kind
extBenchmark := &unstructured.Unstructured{
Object: benchmarkObj,
}
gvk := extBenchmark.GroupVersionKind()
return gvk
}
func GetIteratedValues(benchmark *cpev1.Benchmark) (firstLabel map[string]string, iterationLabels []map[string]string, builds []string, maxRepetition int) {
// iterations
iterations := GetCombinedIterations(benchmark)
if len(iterations) > 0 {
iterationLabels = itrHandler.GetAllCombination(iterations)
firstLabel, iterationLabels = iterationLabels[0], iterationLabels[1:]
} else {
firstLabel = make(map[string]string)
iterationLabels = []map[string]string{}
}
// builds
buildSize := len(benchmark.Status.TrackedBuilds)
if buildSize > 0 {
builds = benchmark.Status.TrackedBuilds
} else {
builds = []string{INIT_BUILD_NAME}
}
// repetition
maxRepetition = benchmark.Spec.Repetition
if maxRepetition <= 0 {
maxRepetition = 1
}
return firstLabel, iterationLabels, builds, maxRepetition
}
func patchBenchmarkStatus(client client.Client, benchmark *cpev1.Benchmark, jobHash string, iterationLabel map[string]string, build string, repInString string) error {
if checkHashExist(benchmark, jobHash) {
return nil
}
newHashItem := cpev1.IterationHash{
Hash: jobHash,
Build: build,
Iteration: iterationLabel,
Repetition: repInString,
}
benchmark.Status.Hash = append(benchmark.Status.Hash, newHashItem)
benchmark.Status.JobCompleted = GetJobCompletedStatus(benchmark)
err := client.Status().Update(context.Background(), benchmark)
return err
}
func GetJobCompletedStatus(benchmark *cpev1.Benchmark) string {
completedJob := 0
for _, result := range benchmark.Status.Results {
completedJob = completedJob + len(result.Items)
}
return fmt.Sprintf("%d/%d", completedJob, len(benchmark.Status.Hash))
}
func GetExpandLabel(iterationLabel map[string]string) map[string]interface{} {
expandLabel := make(map[string]interface{})
for key, val := range iterationLabel {
valSplits := strings.Split(val, ";")
if len(valSplits) > 1 {
expandLabel[key] = valSplits
} else {
expandLabel[key] = val
}
}
return expandLabel
}
func GetBenchmarkWithIteration(client client.Client, ns string, benchmark *cpev1.Benchmark, benchmarkObj map[string]interface{}, iterationLabel map[string]string, build string, repetition int) (*unstructured.Unstructured, error) {
labels := map[string]interface{}{BENCHMARK_LABEL: benchmark.ObjectMeta.Name}
for key, value := range iterationLabel {
labels[key] = getValidValue(value)
}
labels[BUILD_KEY] = getValidValue(build)
repInString := fmt.Sprintf("%d", repetition)
labels[REPETITION_KEY] = repInString
// get hash
jobHash := getJobHash(iterationLabel, build, repetition)
jobName := getJobNameFromHash(benchmark.GetName(), jobHash)
patchBenchmarkStatus(client, benchmark, jobHash, iterationLabel, build, repInString)
labels[JOBHASH_KEY] = jobHash
benchmarkObj["metadata"] = map[string]interface{}{"name": jobName, "namespace": ns, "labels": labels}
// generate job spec
tmpl, err := template.New("").Parse(benchmark.Spec.Spec)
if err != nil {
return nil, err
}
var buffer bytes.Buffer
expandLabel := GetExpandLabel(iterationLabel)
err = tmpl.Execute(&buffer, expandLabel)
if err != nil {
return nil, err
}
executedSpec := buffer.String()
var decUnstructured = yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
obj := &unstructured.Unstructured{}
decUnstructured.Decode([]byte(executedSpec), nil, obj)
specObject := obj.Object
if _, ok := iterationLabel[NODESELECT_ITR_NAME]; ok {
if iterationLabel[NODESELECT_ITR_NAME] != NODESELECT_ITR_DEFAULT {
nodeSelectionItr := NodeSelectionSpecToIteration(benchmark.Spec.IterationSpec.NodeSelection)
specObject = itrHandler.UpdateValue(specObject, nodeSelectionItr.Location, iterationLabel[NODESELECT_ITR_NAME])
}
}
// add selector label
nodeSelectionSpec := benchmark.Spec.IterationSpec.NodeSelection
if nodeSelectionSpec != nil {
labelMap, _ := metav1.LabelSelectorAsMap(nodeSelectionSpec.TargetSelector)
for selectorLabelName, selectorLabelValue := range labelMap {
selectorLabelLocation := nodeSelectionSpec.Location + "." + "(" + selectorLabelName + ")"
specObject = itrHandler.UpdateValue(specObject, selectorLabelLocation, selectorLabelValue)
}
}
benchmarkObj["spec"] = specObject
extBenchmark := &unstructured.Unstructured{
Object: benchmarkObj,
}
return extBenchmark, nil
}
func CheckIfJobDone(benchmark *cpev1.Benchmark, jobName string) bool {
for _, result := range benchmark.Status.Results {
for _, item := range result.Items {
if item.JobName == jobName {
return true
}
}
}
return false
}
func CreateIfNotExists(dr dynamic.ResourceInterface, benchmark *cpev1.Benchmark, unstructuredInstance *unstructured.Unstructured, adaptor OperatorAdaptor, tunedHandler *TunedHandler, nodeTunedOptimizer *BaysesOptimizer) (error, bool) {
if CheckIfJobDone(benchmark, unstructuredInstance.GetName()) {
return nil, false
}
existJob, err := dr.Get(context.TODO(), unstructuredInstance.GetName(), metav1.GetOptions{})
tunedValue := NODESELECT_ITR_DEFAULT
autoTuned := false
completed := true
nodeSelectionSpec := benchmark.Spec.IterationSpec.NodeSelection
if err == nil {
// found
completed = adaptor.CheckComplete(existJob.Object)
}
if completed {
if nodeSelectionSpec != nil {
tunedValue = getTunedValue(nodeSelectionSpec, unstructuredInstance)
if tunedValue == RESERVED_AUTOTUNED_PROFILE_NAME && tunedHandler != nil {
sampledProfileMaps, ok := <-nodeTunedOptimizer.SampleQueue
if !ok {
if nodeTunedOptimizer.FinalizedApplied {
return fmt.Errorf("no more sample"), true
} else {
sampledProfileMaps = nodeTunedOptimizer.FinalizedTunedProfile
nodeTunedOptimizer.SetFinalizedApplied()
}
}
tunedHandler.DeleteAutoTunedProfile()
err = tunedHandler.CreateAutoTunedProfile(sampledProfileMaps)
if err != nil {
return err, true
}
// deleted previous tuned job
dr.Delete(context.TODO(), unstructuredInstance.GetName(), metav1.DeleteOptions{})
autoTuned = true
}
}
}
if err != nil || autoTuned { // create if not exists or autotuned deleted
// handler tuned profile
if tunedValue != NODESELECT_ITR_DEFAULT && tunedHandler != nil {
tunedHandler.ApplyProfile(nodeSelectionSpec.TargetSelector, tunedValue)
}
// create
_, err = dr.Create(context.TODO(), unstructuredInstance, metav1.CreateOptions{})
return err, true
} else if completed {
return err, false
}
return err, true
}
func checkHashExist(benchmark *cpev1.Benchmark, jobHash string) bool {
for _, hashItem := range benchmark.Status.Hash {
if hashItem.Hash == jobHash {
return true
}
}
return false
}
func JobListChanged(benchmark *cpev1.Benchmark) bool {
firstLabel, iterationLabels, builds, maxRepetition := GetIteratedValues(benchmark)
noHash := true
repetition := 0
for {
if repetition >= maxRepetition {
break
}
for _, build := range builds {
noHash = true
jobHash := getJobHash(firstLabel, build, repetition)
if !checkHashExist(benchmark, jobHash) {
return true
}
for _, iterationLabel := range iterationLabels {
jobHash = getJobHash(iterationLabel, build, repetition)
if !checkHashExist(benchmark, jobHash) {
return true
}
}
}
repetition = repetition + 1
}
if len(benchmark.Status.Hash) == 0 && noHash {
return false
}
return true
}
func NewBenchmarkObject(benchmarkOperator *cpev1.BenchmarkOperator) map[string]interface{} {
apiVersion := benchmarkOperator.Spec.APIVersion
kind := benchmarkOperator.Spec.Kind
benchmarkObj := make(map[string]interface{})
benchmarkObj["apiVersion"] = apiVersion
benchmarkObj["kind"] = kind
return benchmarkObj
}
func CreateFromOperator(jtm *JobTrackManager, client client.Client, dc *discovery.DiscoveryClient, dyn dynamic.Interface, benchmark *cpev1.Benchmark, benchmarkOperator *cpev1.BenchmarkOperator, reqLogger logr.Logger, adaptor OperatorAdaptor, tunedHandler *TunedHandler) error {
gvk := GetSimpleJobGVK(benchmarkOperator)
if jtm.IsExist(gvk, benchmark.GetName()) {
reqLogger.Info(fmt.Sprintf("Benchmark %s has already registered", benchmark.GetName()))
return nil
}
dr := getResourceInterface(dc, dyn, &gvk, benchmark.Namespace)
if dr == nil {
reqLogger.Info(fmt.Sprintf("Benchmark %s cannot getResourceInterface", benchmark.GetName()))
return nil
}
firstLabel, iterationLabels, builds, maxRepetition := GetIteratedValues(benchmark)
var waitingJob []*unstructured.Unstructured
jobOptMap := make(map[string]*BaysesOptimizer)
isNew := false
var err error
repetition := 0
reqLogger.Info(fmt.Sprintf("Max Repetition: %d", maxRepetition))
for {
if repetition >= maxRepetition {
break
}
for _, build := range builds {
// for firstLabel (iteration0 or nolabel)
firstBenchmarkObj := NewBenchmarkObject(benchmarkOperator)
extBenchmark, err := GetBenchmarkWithIteration(client, benchmark.Namespace, benchmark, firstBenchmarkObj, firstLabel, build, repetition)
if err != nil {
reqLogger.Info(fmt.Sprintf("Failed to GetBenchmarkWithIteration: %v)", err))
}
nodeTunedOptimizer := NewBayesOptimizer(benchmark.Spec.IterationSpec.Minimize)
jobName := extBenchmark.GetName()
jobOptMap[jobName] = nodeTunedOptimizer
nodeSelectionSpec := benchmark.Spec.IterationSpec.NodeSelection
if nodeSelectionSpec != nil {
tunedValue := getTunedValue(nodeSelectionSpec, extBenchmark)
if tunedValue == RESERVED_AUTOTUNED_PROFILE_NAME && tunedHandler != nil {
// activate auto-tuning
go nodeTunedOptimizer.AutoTune()
} else {
nodeTunedOptimizer.SetFinalizedApplied()
}
reqLogger.Info(fmt.Sprintf("Set JobOptimizerMap %s - %s, %v)", jobName, tunedValue, extBenchmark))
} else {
nodeTunedOptimizer.SetFinalizedApplied()
}
if err == nil && !isNew {
err, isNew = CreateIfNotExists(dr, benchmark, extBenchmark, adaptor, tunedHandler, nodeTunedOptimizer)
reqLogger.Info(fmt.Sprintf("Try creating %s (first label)", extBenchmark.GetName()))
if err != nil {
reqLogger.Info(fmt.Sprintf("Failed to create benchmark %s: %v)", benchmark.Name, err))
}
} else {
_, existErr := dr.Get(context.TODO(), extBenchmark.GetName(), metav1.GetOptions{})
if existErr != nil {
waitingJob = append(waitingJob, extBenchmark.DeepCopy())
}
}
// for the rest iteration
for _, iterationLabel := range iterationLabels {
benchmarkObj := NewBenchmarkObject(benchmarkOperator)
waitExtBenchmark, err := GetBenchmarkWithIteration(client, benchmark.Namespace, benchmark, benchmarkObj, iterationLabel, build, repetition)
if err != nil {
reqLogger.Info(fmt.Sprintf("Failed to GetBenchmarkWithIteration: %v)", err))
continue
}
nodeTunedOptimizer := NewBayesOptimizer(benchmark.Spec.IterationSpec.Minimize)
jobName = waitExtBenchmark.GetName()
jobOptMap[jobName] = nodeTunedOptimizer
if nodeSelectionSpec != nil {
tunedValue := getTunedValue(nodeSelectionSpec, waitExtBenchmark)
if tunedValue == RESERVED_AUTOTUNED_PROFILE_NAME {
// activate auto-tuning
go nodeTunedOptimizer.AutoTune()
} else {
nodeTunedOptimizer.SetFinalizedApplied()
}
reqLogger.Info(fmt.Sprintf("Set JobOptimizerMap %s - %s, %v)", jobName, tunedValue, waitExtBenchmark))
} else {
nodeTunedOptimizer.SetFinalizedApplied()
}
if benchmark.Spec.IterationSpec.Sequential || benchmark.Spec.IterationSpec.NodeSelection != nil { // must be sequential if node selection is set
// to create at least one new job
if err == nil && !isNew {
err, isNew = CreateIfNotExists(dr, benchmark, waitExtBenchmark, adaptor, tunedHandler, nodeTunedOptimizer)
reqLogger.Info(fmt.Sprintf("Try creating %s", waitExtBenchmark.GetName()))
if err != nil {
reqLogger.Info(fmt.Sprintf("Failed to create benchmark %s: %v)", benchmark.Name, err))
}
} else {
_, existErr := dr.Get(context.TODO(), waitExtBenchmark.GetName(), metav1.GetOptions{})
if existErr != nil {
waitingJob = append(waitingJob, waitExtBenchmark.DeepCopy())
}
}
} else {
err, _ = CreateIfNotExists(dr, benchmark, waitExtBenchmark, adaptor, tunedHandler, nodeTunedOptimizer)
if err != nil {
reqLogger.Info(fmt.Sprintf("Failed to create benchmark %s: %v)", benchmark.Name, err))
}
}
}
}
repetition = repetition + 1
}
if err != nil {
reqLogger.Info(fmt.Sprintf("Cannot create #%v: %s", err, benchmark.GetName()))
return err
}
jtm.NewTracker(gvk, benchmark.GetName(), waitingJob, dr, adaptor, jobOptMap)
return nil
}
func DeleteFromOperator(dc *discovery.DiscoveryClient, dyn dynamic.Interface, benchmark *cpev1.Benchmark, benchmarkOperator *cpev1.BenchmarkOperator) error {
apiVersion := benchmarkOperator.Spec.APIVersion
kind := benchmarkOperator.Spec.Kind
ns := benchmark.ObjectMeta.Namespace
gvk := schema.FromAPIVersionAndKind(apiVersion, kind)
dr := getResourceInterface(dc, dyn, &gvk, ns)
firstLabel, iterationLabels, builds, maxRepetition := GetIteratedValues(benchmark)
repetition := 0
var err error
for {
if repetition >= maxRepetition {
break
}
for _, build := range builds {
// for firstLabel (iteration0 or nolabel)
jobName := getJobName(benchmark, firstLabel, build, repetition)
err = dr.Delete(context.TODO(), jobName, metav1.DeleteOptions{})
// for the rest iteration
for _, iterationLabel := range iterationLabels {
jobName = getJobName(benchmark, iterationLabel, build, repetition)
err = dr.Delete(context.TODO(), jobName, metav1.DeleteOptions{})
}
}
repetition = repetition + 1
}
return err
}
func GetCombinedIterations(benchmark *cpev1.Benchmark) []cpev1.IterationItem {
iterations := append(benchmark.Spec.IterationSpec.Iteration, benchmark.Spec.IterationSpec.Configuration...)
if benchmark.Spec.IterationSpec.NodeSelection != nil {
nodeSelectionItr := NodeSelectionSpecToIteration(benchmark.Spec.IterationSpec.NodeSelection)
iterations = append(iterations, nodeSelectionItr)
}
return iterations
}
// NodeSelectionSpec
func NodeSelectionSpecToIteration(spec *cpev1.NodeSelectionSpec) cpev1.IterationItem {
location := spec.Location + "." + NODESELECT_ITR_NAME
itr := cpev1.IterationItem{
Name: NODESELECT_ITR_NAME,
Location: location,
Values: spec.TunedValues,
}
return itr
}
func getTunedValue(spec *cpev1.NodeSelectionSpec, job *unstructured.Unstructured) string {
location := spec.Location + "." + NODESELECT_ITR_NAME
tunedValue := itrHandler.GetValue(job.Object["spec"].(map[string]interface{}), location)
if tunedValue == nil {
return NODESELECT_ITR_DEFAULT
}
return tunedValue[0]
}