/
simulator.go
executable file
·1139 lines (1023 loc) · 37.5 KB
/
simulator.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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package simulator
import (
"context"
"encoding/json"
"fmt"
"math"
"math/rand"
"sort"
"sync"
"time"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/informers"
externalclientset "k8s.io/client-go/kubernetes"
fakeclientset "k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/kubernetes/cmd/kube-scheduler/app/config"
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
"k8s.io/kubernetes/pkg/scheduler"
"k8s.io/kubernetes/pkg/scheduler/framework"
frameworkruntime "k8s.io/kubernetes/pkg/scheduler/framework/runtime"
"github.com/hkust-adsl/kubernetes-scheduler-simulator/pkg/algo"
"github.com/hkust-adsl/kubernetes-scheduler-simulator/pkg/api/v1alpha1"
simonplugin "github.com/hkust-adsl/kubernetes-scheduler-simulator/pkg/simulator/plugin"
simontype "github.com/hkust-adsl/kubernetes-scheduler-simulator/pkg/type"
gpushareutils "github.com/hkust-adsl/kubernetes-scheduler-simulator/pkg/type/open-gpu-share/utils"
"github.com/hkust-adsl/kubernetes-scheduler-simulator/pkg/utils"
)
// Simulator is used to simulate a cluster and pods scheduling
type Simulator struct {
// kube client
// externalclient externalclientset.Interface
client externalclientset.Interface
informerFactory informers.SharedInformerFactory
// scheduler
scheduler *scheduler.Scheduler
// context
ctx context.Context
cancelFunc context.CancelFunc
status status
//
workloadPods []*corev1.Pod
typicalPods simontype.TargetPodList
skylinePods simontype.SkylinePodList
nodeResourceMap map[string]simontype.NodeResource
customConfig v1alpha1.CustomConfig
fragMemo sync.Map
arrPodGpuMilli int64
podTotalMilliCpuReq int64
podTotalMilliGpuReq int64
nodeTotalMilliCpu int64
nodeTotalMilliGpu int64
}
// status captures reason why one pod fails to be scheduled
type status struct {
stopReason string
}
type simulatorOptions struct {
kubeconfig string
schedulerConfig string
customConfig v1alpha1.CustomConfig
}
// Option configures a Simulator
type Option func(*simulatorOptions)
var defaultSimulatorOptions = simulatorOptions{
kubeconfig: "",
schedulerConfig: "",
customConfig: v1alpha1.CustomConfig{},
}
// New generates all components that will be needed to simulate scheduling and returns a complete simulator
func New(opts ...Option) (Interface, error) {
var err error
// configure a simulator by opts
options := defaultSimulatorOptions
for _, opt := range opts {
opt(&options)
}
// get scheduler config and set the list of scheduler bind plugins to the simulator
kubeSchedulerConfig, err := GetAndSetSchedulerConfig(options.schedulerConfig)
if err != nil {
return nil, err
}
displaySchedulerConfig(kubeSchedulerConfig)
// create a real/fake client
var client externalclientset.Interface
if options.kubeconfig != "" {
varConfig, err := clientcmd.BuildConfigFromFlags("", options.kubeconfig)
if err != nil {
log.Errorf("%s\n", err.Error())
}
client, err = externalclientset.NewForConfig(varConfig)
} else {
client = fakeclientset.NewSimpleClientset()
}
kubeSchedulerConfig.Client = client
sharedInformerFactory := informers.NewSharedInformerFactory(client, 0)
// create a simulator
ctx, cancel := context.WithCancel(context.Background())
storagev1Informers := sharedInformerFactory.Storage().V1()
scInformer := storagev1Informers.StorageClasses().Informer()
sharedInformerFactory.Start(ctx.Done())
cache.WaitForCacheSync(ctx.Done(), scInformer.HasSynced)
sim := &Simulator{
client: client,
informerFactory: sharedInformerFactory,
ctx: ctx,
cancelFunc: cancel,
customConfig: options.customConfig,
}
// create a scheduler
bindRegistry := frameworkruntime.Registry{
simontype.SimonPluginName: func(configuration runtime.Object, handle framework.Handle) (framework.Plugin, error) {
return simonplugin.NewSimonPlugin(configuration, handle)
},
simontype.OpenGpuSharePluginName: func(configuration runtime.Object, handle framework.Handle) (framework.Plugin, error) {
return simonplugin.NewGpuSharePlugin(configuration, handle, &sim.typicalPods)
},
simontype.RandomScorePluginName: func(configuration runtime.Object, handle framework.Handle) (framework.Plugin, error) {
return simonplugin.NewRandomScorePlugin(configuration, handle)
},
simontype.DotProductScorePluginName: func(configuration runtime.Object, handle framework.Handle) (framework.Plugin, error) {
return simonplugin.NewDotProductScorePlugin(configuration, handle)
},
simontype.GpuClusteringScorePluginName: func(configuration runtime.Object, handle framework.Handle) (framework.Plugin, error) {
return simonplugin.NewGpuClusteringScorePlugin(configuration, handle)
},
simontype.GpuPackingScorePluginName: func(configuration runtime.Object, handle framework.Handle) (framework.Plugin, error) {
return simonplugin.NewGpuPackingScorePlugin(configuration, handle)
},
simontype.BestFitScorePluginName: func(configuration runtime.Object, handle framework.Handle) (framework.Plugin, error) {
return simonplugin.NewBestFitScorePlugin(configuration, handle)
},
simontype.FGDScorePluginName: func(configuration runtime.Object, handle framework.Handle) (framework.Plugin, error) {
return simonplugin.NewFGDScorePlugin(configuration, handle, &sim.typicalPods)
},
}
sim.scheduler, err = scheduler.New(
sim.client,
sim.informerFactory,
GetRecorderFactory(kubeSchedulerConfig),
sim.ctx.Done(),
scheduler.WithProfiles(kubeSchedulerConfig.ComponentConfig.Profiles...),
scheduler.WithAlgorithmSource(kubeSchedulerConfig.ComponentConfig.AlgorithmSource),
scheduler.WithPercentageOfNodesToScore(kubeSchedulerConfig.ComponentConfig.PercentageOfNodesToScore),
scheduler.WithFrameworkOutOfTreeRegistry(bindRegistry),
scheduler.WithPodMaxBackoffSeconds(kubeSchedulerConfig.ComponentConfig.PodMaxBackoffSeconds),
scheduler.WithPodInitialBackoffSeconds(kubeSchedulerConfig.ComponentConfig.PodInitialBackoffSeconds),
scheduler.WithExtenders(kubeSchedulerConfig.ComponentConfig.Extenders...),
)
if err != nil {
return nil, err
}
return sim, nil
}
// RunCluster with real client in a production cluster or fake client in a simulated cluster.
func (sim *Simulator) RunCluster(cluster ResourceTypes) ([]simontype.UnscheduledPod, error) {
// start scheduler
sim.runScheduler()
switch t := sim.client.(type) {
case *externalclientset.Clientset:
return nil, nil
case *fakeclientset.Clientset:
return sim.syncClusterResourceList(cluster)
default:
return nil, fmt.Errorf("unknown client type: %T", t)
}
}
func (sim *Simulator) ScheduleApp(apps AppResource) ([]simontype.UnscheduledPod, error) {
// 由 AppResource 生成 Pods
appPods, err := GenerateValidPodsFromAppResources(sim.client, apps.Name, apps.Resource)
if err != nil {
return nil, err
}
affinityPriority := algo.NewAffinityQueue(appPods)
sort.Sort(affinityPriority)
tolerationPriority := algo.NewTolerationQueue(appPods)
sort.Sort(tolerationPriority)
failedPod := sim.SchedulePods(appPods)
return failedPod, nil
}
func (sim *Simulator) GetCustomConfig() v1alpha1.CustomConfig {
return sim.customConfig
}
func (sim *Simulator) GetClusterNodeStatus() []simontype.NodeStatus {
var nodeStatues []simontype.NodeStatus
var err error
nodes, err := sim.client.CoreV1().Nodes().List(sim.ctx, metav1.ListOptions{})
if err != nil {
return nil
}
pods, err := sim.client.CoreV1().Pods(corev1.NamespaceAll).List(sim.ctx, metav1.ListOptions{})
if err != nil {
return nil
}
nodeToPodListMap := map[string][]*corev1.Pod{}
for _, pod := range pods.Items {
nodeName := pod.Spec.NodeName
if nodeName != "" {
if nodeToPodListMap[nodeName] == nil {
nodeToPodListMap[nodeName] = []*corev1.Pod{}
}
nodeToPodListMap[nodeName] = append(nodeToPodListMap[nodeName], pod.DeepCopy())
}
}
for _, node := range nodes.Items {
nodeStatus := simontype.NodeStatus{}
nodeStatus.Node = node.DeepCopy()
nodeStatus.Pods = nodeToPodListMap[node.Name]
nodeStatues = append(nodeStatues, nodeStatus)
}
return nodeStatues
}
// runScheduler
func (sim *Simulator) runScheduler() {
// Step 1: start all informers.
sim.informerFactory.Start(sim.ctx.Done())
sim.informerFactory.WaitForCacheSync(sim.ctx.Done())
// Step 2: run scheduler
go func() {
sim.scheduler.Run(sim.ctx)
}()
}
func (sim *Simulator) createPod(p *corev1.Pod) error {
if _, err := sim.client.CoreV1().Pods(p.Namespace).Create(sim.ctx, p, metav1.CreateOptions{}); err != nil {
return fmt.Errorf("%s(%s): %s", simontype.CreatePodError, utils.GeneratePodKey(p), err.Error())
}
// synchronization
sim.syncPodCreate(p.Namespace, p.Name, 2*time.Millisecond)
pod, _ := sim.client.CoreV1().Pods(p.Namespace).Get(sim.ctx, p.Name, metav1.GetOptions{})
if pod != nil {
if pod.Spec.NodeName != "" {
sim.syncNodeUpdateOnPodCreate(pod.Spec.NodeName, pod, 2*time.Millisecond)
log.Infof("pod(%s) is scheduled to node(%s)\n", utils.GeneratePodKey(pod), pod.Spec.NodeName)
}
} else {
log.Errorf("[createPod] pod(%s) not created, should not happen", utils.GeneratePodKey(p))
}
return nil
}
func (sim *Simulator) deletePod(p *corev1.Pod) error {
pod, _ := sim.client.CoreV1().Pods(p.Namespace).Get(sim.ctx, p.Name, metav1.GetOptions{})
nodeName := ""
if pod != nil {
nodeName = pod.Spec.NodeName
} else {
log.Debugf("[deletePod] attempt to delete a non-existed pod(%s)\n", utils.GeneratePodKey(p))
return nil
}
// delete the pod
if err := sim.client.CoreV1().Pods(p.Namespace).Delete(sim.ctx, p.Name, metav1.DeleteOptions{}); err != nil {
return fmt.Errorf("%s(%s): %s", simontype.DeletePodError, utils.GeneratePodKey(p), err.Error())
}
// synchronization
sim.syncPodDelete(p.Namespace, p.Name, 500*time.Microsecond)
if nodeName != "" {
sim.syncNodeUpdateOnPodDelete(nodeName, pod, 2*time.Millisecond)
} else {
log.Infof("[deletePod] attempt to delete a non-scheduled pod(%s)\n", utils.GeneratePodKey(p))
}
return nil
}
func (sim *Simulator) assumePod(pod *corev1.Pod) *simontype.UnscheduledPod {
err := sim.createPod(pod)
if err != nil || sim.isPodUnscheduled(pod.Namespace, pod.Name) {
if err = sim.deletePod(pod); err != nil {
log.Errorf("[assumePod] failed to delete pod(%s)\n", utils.GeneratePodKey(pod))
}
return &simontype.UnscheduledPod{Pod: pod}
}
return nil
}
func (sim *Simulator) SchedulePods(pods []*corev1.Pod) []simontype.UnscheduledPod {
var failedPods []simontype.UnscheduledPod
sim.arrPodGpuMilli = 0
for i, pod := range pods {
if IsPodMarkedUnscheduledAnno(pod) {
log.Infof("[%d] pod(%s) has unscheduled annotation\n", i, utils.GeneratePodKey(pod))
failedPods = append(failedPods, simontype.UnscheduledPod{
Pod: pod,
Reason: fmt.Sprintf("pod(%s) has unscheduled annotation", utils.GeneratePodKey(pod)),
})
continue
}
deletionTime := gpushareutils.GetDeletionTimeFromPodAnnotation(pod)
if deletionTime == nil {
podRes := utils.GetPodResource(pod)
sim.arrPodGpuMilli += podRes.TotalMilliGpu()
log.Infof("[%d] attempt to create pod(%s)\n", i, utils.GeneratePodKey(pod))
if unscheduledPod := sim.assumePod(pod); unscheduledPod != nil {
log.Infof("[%d] failed to schedule pod(%s): %s\n", i, utils.GeneratePodKey(pod), utils.GetPodResource(pod).Repr())
failedPods = append(failedPods, *unscheduledPod)
}
} else {
log.Infof("[%d] attempt to delete pod(%s)\n", i, utils.GeneratePodKey(pod))
if err := sim.deletePod(pod); err != nil {
log.Errorf("failed to delete pod(%s)\n", utils.GeneratePodKey(pod))
}
}
sim.ClusterGpuFragReport()
// sim.ReportFragBasedOnSkyline()
}
return failedPods
}
func (sim *Simulator) Close() {
sim.cancelFunc()
}
func (sim *Simulator) isPodScheduled(ns, name string) bool {
pod, _ := sim.client.CoreV1().Pods(ns).Get(sim.ctx, name, metav1.GetOptions{})
return pod != nil && pod.Spec.NodeName != ""
}
func (sim *Simulator) isPodUnscheduled(ns, name string) bool {
pod, _ := sim.client.CoreV1().Pods(ns).Get(sim.ctx, name, metav1.GetOptions{})
if pod != nil && pod.Spec.NodeName == "" {
for _, condition := range pod.Status.Conditions {
if condition.Type == corev1.PodScheduled && condition.Status == corev1.ConditionFalse &&
condition.Reason == corev1.PodReasonUnschedulable {
return true
}
}
}
return false
}
func (sim *Simulator) isPodCreated(ns, name string) bool {
return sim.isPodScheduled(ns, name) || sim.isPodUnscheduled(ns, name)
}
func (sim *Simulator) isPodDeleted(ns, name string) bool {
pod, _ := sim.client.CoreV1().Pods(ns).Get(sim.ctx, name, metav1.GetOptions{})
return pod == nil || (pod.Namespace == "" && pod.Name == "")
}
func (sim *Simulator) isNodeCreated(name string) bool {
node, _ := sim.client.CoreV1().Nodes().Get(sim.ctx, name, metav1.GetOptions{})
return node != nil
}
func (sim *Simulator) isPodFoundInNodeGpuAnno(node *corev1.Node, p *corev1.Pod) bool {
gpuNodeInfoStr, err := utils.GetGpuNodeInfoFromAnnotation(node)
if err != nil {
panic(fmt.Sprintf("failed to parse gpu info annotation on node(%s): %v", node.Name, err.Error()))
}
if gpuNodeInfoStr == nil {
panic(fmt.Sprintf("gpu node(%s) don't have gpu annotation, should not happen", node.Name))
}
for _, dev := range gpuNodeInfoStr.DevsBrief {
for _, pStr := range dev.PodList {
if utils.GeneratePodKey(p) == pStr {
return true
}
}
}
return false
}
func (sim *Simulator) syncPodCreate(ns, name string, d time.Duration) {
for {
if sim.isPodCreated(ns, name) {
break
}
time.Sleep(d)
}
}
func (sim *Simulator) syncPodDelete(ns, name string, d time.Duration) {
for {
log.Debugf("check if pod(%s) has been deleted\n", name)
if sim.isPodDeleted(ns, name) {
break
}
time.Sleep(d)
}
time.Sleep(d)
}
func (sim *Simulator) syncNodeUpdateOnPodCreate(nodeName string, p *corev1.Pod, d time.Duration) {
for {
node, _ := sim.client.CoreV1().Nodes().Get(sim.ctx, nodeName, metav1.GetOptions{})
if node == nil {
log.Errorf("syncNodeUpdateOnPodCreate] failed to get node(%s) when creating pod(%s)\n",
nodeName, utils.GeneratePodKey(p))
break
}
// only check gpu pod because creating it will update node gpu annotation
if gpushareutils.GetGpuMilliFromPodAnnotation(p) > 0 {
if sim.isPodFoundInNodeGpuAnno(node, p) {
break
}
} else {
break
}
time.Sleep(d)
}
}
func (sim *Simulator) syncNodeUpdateOnPodDelete(nodeName string, p *corev1.Pod, d time.Duration) {
for {
node, _ := sim.client.CoreV1().Nodes().Get(sim.ctx, nodeName, metav1.GetOptions{})
if node == nil {
log.Errorf("[syncNodeUpdateOnPodDelete] failed to get node(%s)\n", nodeName)
break
}
// only check gpu pod because deleting it will update node gpu annotation
if gpushareutils.GetGpuMilliFromPodAnnotation(p) > 0 {
if !sim.isPodFoundInNodeGpuAnno(node, p) {
break
}
} else {
break
}
time.Sleep(d)
}
}
func (sim *Simulator) syncNodeCreate(name string, d time.Duration) {
for {
if sim.isNodeCreated(name) {
break
}
time.Sleep(d)
}
log.Debugf("node(%s) has been successfully created\n", name)
time.Sleep(d) // sleep for a while to avoid event channel full
}
// syncClusterResourceList: 1) load Pods into creation and deletion events. 2) schedule and delete these existing Pods.
func (sim *Simulator) syncClusterResourceList(resourceList ResourceTypes) ([]simontype.UnscheduledPod, error) {
//sync node
sort.Slice(resourceList.Nodes, func(i, j int) bool {
return resourceList.Nodes[i].Name < resourceList.Nodes[j].Name
})
randomIndex := rand.Perm(len(resourceList.Nodes))
for i := 0; i < len(randomIndex); i++ {
idx := randomIndex[i]
resourceList.Nodes[i].Name = fmt.Sprintf("%04d-", idx) + resourceList.Nodes[i].Name
}
for i, item := range resourceList.Nodes {
log.Debugf("[%d] attempt to create node(%s)\n", i, item.Name)
if _, err := sim.client.CoreV1().Nodes().Create(sim.ctx, item, metav1.CreateOptions{}); err != nil {
return nil, fmt.Errorf("unable to copy node: %v", err)
}
sim.syncNodeCreate(item.Name, 1*time.Millisecond)
}
//sync pdb
for _, item := range resourceList.PodDisruptionBudgets {
if _, err := sim.client.PolicyV1beta1().PodDisruptionBudgets(item.Namespace).Create(context.TODO(), item, metav1.CreateOptions{}); err != nil {
return nil, fmt.Errorf("unable to copy PDB: %v", err)
}
}
//sync svc
for _, item := range resourceList.Services {
if _, err := sim.client.CoreV1().Services(item.Namespace).Create(context.TODO(), item, metav1.CreateOptions{}); err != nil {
return nil, fmt.Errorf("unable to copy service: %v", err)
}
}
//sync storage class
for _, item := range resourceList.StorageClasss {
if _, err := sim.client.StorageV1().StorageClasses().Create(context.TODO(), item, metav1.CreateOptions{}); err != nil {
return nil, fmt.Errorf("unable to copy storage class: %v", err)
}
}
//sync pvc
for _, item := range resourceList.PersistentVolumeClaims {
if _, err := sim.client.CoreV1().PersistentVolumeClaims(item.Namespace).Create(context.TODO(), item, metav1.CreateOptions{}); err != nil {
return nil, fmt.Errorf("unable to copy pvc: %v", err)
}
}
//sync rc
for _, item := range resourceList.ReplicationControllers {
if _, err := sim.client.CoreV1().ReplicationControllers(item.Namespace).Create(context.TODO(), item, metav1.CreateOptions{}); err != nil {
return nil, fmt.Errorf("unable to copy RC: %v", err)
}
}
//sync deployment
for _, item := range resourceList.Deployments {
if _, err := sim.client.AppsV1().Deployments(item.Namespace).Create(context.TODO(), item, metav1.CreateOptions{}); err != nil {
return nil, fmt.Errorf("unable to copy deployment: %v", err)
}
}
//sync rs
for _, item := range resourceList.ReplicaSets {
if _, err := sim.client.AppsV1().ReplicaSets(item.Namespace).Create(context.TODO(), item, metav1.CreateOptions{}); err != nil {
return nil, fmt.Errorf("unable to copy replica set: %v", err)
}
}
//sync statefulset
for _, item := range resourceList.StatefulSets {
if _, err := sim.client.AppsV1().StatefulSets(item.Namespace).Create(context.TODO(), item, metav1.CreateOptions{}); err != nil {
return nil, fmt.Errorf("unable to copy stateful set: %v", err)
}
}
//sync daemonset
for _, item := range resourceList.DaemonSets {
if _, err := sim.client.AppsV1().DaemonSets(item.Namespace).Create(context.TODO(), item, metav1.CreateOptions{}); err != nil {
return nil, fmt.Errorf("unable to copy daemon set: %v", err)
}
}
// sync pods
var podEvents []*corev1.Pod
for _, p := range resourceList.Pods {
// pod creation event
podCreate := p.DeepCopy()
delete(podCreate.Annotations, gpushareutils.DeletionTime)
podEvents = append(podEvents, podCreate)
// pod deletion event
deletionTime := gpushareutils.GetDeletionTimeFromPodAnnotation(p)
if deletionTime != nil {
podDelete := p.DeepCopy()
delete(podDelete.Annotations, gpushareutils.CreationTime)
podEvents = append(podEvents, podDelete)
}
}
// sort pod creation/deletion events according to timestamp
sort.SliceStable(podEvents, func(i, j int) bool {
// c: creation, d: deletion
ci, di := gpushareutils.GetCreationTimeFromPodAnnotation(podEvents[i]), gpushareutils.GetDeletionTimeFromPodAnnotation(podEvents[i])
cj, dj := gpushareutils.GetCreationTimeFromPodAnnotation(podEvents[j]), gpushareutils.GetDeletionTimeFromPodAnnotation(podEvents[j])
var ti time.Time
if ci != nil {
ti = *ci
} else if di != nil {
ti = *di
}
var tj time.Time
if cj != nil {
tj = *cj
} else if dj != nil {
tj = *dj
}
// undefined goes before all, see simulator.TestSortPodsByTimestamp
return ti.Before(tj)
})
failedPods := sim.SchedulePods(podEvents)
return failedPods, nil
}
// WithKubeConfig sets kubeconfig for Simulator, the default value is ""
func WithKubeConfig(kubeconfig string) Option {
return func(o *simulatorOptions) {
o.kubeconfig = kubeconfig
}
}
// WithSchedulerConfig sets schedulerConfig for Simulator, the default value is ""
func WithSchedulerConfig(schedulerConfig string) Option {
return func(o *simulatorOptions) {
o.schedulerConfig = schedulerConfig
}
}
func WithCustomConfig(customConfig v1alpha1.CustomConfig) Option {
return func(o *simulatorOptions) {
o.customConfig = customConfig
}
}
// CreateClusterResourceFromClient returns a ResourceTypes struct by kube-client that connects a real cluster
func CreateClusterResourceFromClient(client externalclientset.Interface) (ResourceTypes, error) {
var resource ResourceTypes
var err error
nodeItems, err := client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return resource, fmt.Errorf("unable to list nodes: %v", err)
}
for _, item := range nodeItems.Items {
newItem := item
resource.Nodes = append(resource.Nodes, &newItem)
}
// TODO:
// For all pods in the real cluster, we only retain static pods.
// We will regenerate pods of all workloads in the follow-up stage.
podItems, err := client.CoreV1().Pods(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return resource, fmt.Errorf("unable to list pods: %v", err)
}
for _, item := range podItems.Items {
if kubetypes.IsStaticPod(&item) {
newItem := item
resource.Pods = append(resource.Pods, &newItem)
}
}
pdbItems, err := client.PolicyV1beta1().PodDisruptionBudgets(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return resource, fmt.Errorf("unable to list PDBs: %v", err)
}
for _, item := range pdbItems.Items {
newItem := item
resource.PodDisruptionBudgets = append(resource.PodDisruptionBudgets, &newItem)
}
serviceItems, err := client.CoreV1().Services(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return resource, fmt.Errorf("unable to list services: %v", err)
}
for _, item := range serviceItems.Items {
newItem := item
resource.Services = append(resource.Services, &newItem)
}
storageClassesItems, err := client.StorageV1().StorageClasses().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return resource, fmt.Errorf("unable to list storage classes: %v", err)
}
for _, item := range storageClassesItems.Items {
newItem := item
resource.StorageClasss = append(resource.StorageClasss, &newItem)
}
pvcItems, err := client.CoreV1().PersistentVolumeClaims(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return resource, fmt.Errorf("unable to list pvcs: %v", err)
}
for _, item := range pvcItems.Items {
newItem := item
resource.PersistentVolumeClaims = append(resource.PersistentVolumeClaims, &newItem)
}
rcItems, err := client.CoreV1().ReplicationControllers(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return resource, fmt.Errorf("unable to list RCs: %v", err)
}
for _, item := range rcItems.Items {
newItem := item
resource.ReplicationControllers = append(resource.ReplicationControllers, &newItem)
}
deploymentItems, err := client.AppsV1().Deployments(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return resource, fmt.Errorf("unable to list deployment: %v", err)
}
for _, item := range deploymentItems.Items {
newItem := item
resource.Deployments = append(resource.Deployments, &newItem)
}
replicaSetItems, err := client.AppsV1().ReplicaSets(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return resource, fmt.Errorf("unable to list replicas sets: %v", err)
}
for _, item := range replicaSetItems.Items {
if !ownedByDeployment(item.OwnerReferences) {
newItem := item
resource.ReplicaSets = append(resource.ReplicaSets, &newItem)
}
}
statefulSetItems, err := client.AppsV1().StatefulSets(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return resource, fmt.Errorf("unable to list stateful sets: %v", err)
}
for _, item := range statefulSetItems.Items {
newItem := item
resource.StatefulSets = append(resource.StatefulSets, &newItem)
}
daemonSetItems, err := client.AppsV1().DaemonSets(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return resource, fmt.Errorf("unable to list daemon sets: %v", err)
}
for _, item := range daemonSetItems.Items {
newItem := item
resource.DaemonSets = append(resource.DaemonSets, &newItem)
}
cronJobItems, err := client.BatchV1beta1().CronJobs(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return resource, fmt.Errorf("unable to list cronjob: %v", err)
}
for _, item := range cronJobItems.Items {
newItem := item
resource.CronJobs = append(resource.CronJobs, &newItem)
}
jobItems, err := client.BatchV1().Jobs(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return resource, fmt.Errorf("unable to list job: %v", err)
}
for _, item := range jobItems.Items {
if !ownedByCronJob(item.OwnerReferences) {
newItem := item
resource.Jobs = append(resource.Jobs, &newItem)
}
}
return resource, nil
}
// CreateClusterResourceFromClusterConfig return a ResourceTypes struct based on the cluster config
func CreateClusterResourceFromClusterConfig(path string) (ResourceTypes, error) {
var resource ResourceTypes
var content []string
var err error
if content, err = utils.GetYamlContentFromDirectory(path); err != nil {
return ResourceTypes{}, fmt.Errorf("failed to get the yaml content from the cluster directory(%s): %v", path, err)
}
if resource, err = GetObjectFromYamlContent(content); err != nil {
return resource, err
}
MatchAndSetLocalStorageAnnotationOnNode(resource.Nodes, path)
return resource, nil
}
func ownedByDeployment(refs []metav1.OwnerReference) bool {
for _, ref := range refs {
if ref.Kind == simontype.Deployment {
return true
}
}
return false
}
func ownedByCronJob(refs []metav1.OwnerReference) bool {
for _, ref := range refs {
if ref.Kind == simontype.CronJob {
return true
}
}
return false
}
func getPodfromPodMap(podKeys []string, podMap map[string]*corev1.Pod) []*corev1.Pod {
var podList []*corev1.Pod
for _, podKey := range podKeys {
podCopy := MakePodUnassigned(podMap[podKey])
podList = append(podList, podCopy)
}
return podList
}
func (sim *Simulator) getNodeFragAmountList(nodeStatus []simontype.NodeStatus) []utils.FragAmount {
nodeResourceMap := utils.GetNodeResourceMap(nodeStatus)
nodeFragAmountMap := sim.NodeGpuFragAmountMap(nodeResourceMap)
var nodeFragAmountList []utils.FragAmount
for _, v := range nodeFragAmountMap {
nodeFragAmountList = append(nodeFragAmountList, v)
}
sort.Slice(nodeFragAmountList, func(i int, j int) bool {
return nodeFragAmountList[i].FragAmountSumExceptQ3() > nodeFragAmountList[j].FragAmountSumExceptQ3()
})
return nodeFragAmountList
}
func (sim *Simulator) getCurrentPodMap() map[string]*corev1.Pod {
podMap := make(map[string]*corev1.Pod)
podList, _ := sim.client.CoreV1().Pods(metav1.NamespaceAll).List(sim.ctx, metav1.ListOptions{})
for _, pod := range podList.Items {
podMap[utils.GeneratePodKey(&pod)] = pod.DeepCopy()
}
return podMap
}
func (sim *Simulator) SetWorkloadPods(pods []*corev1.Pod) {
sim.workloadPods = []*corev1.Pod{}
for _, p := range pods {
pod := MakePodUnassigned(p.DeepCopy())
if pod.Spec.NodeSelector != nil {
delete(pod.Spec.NodeSelector, simontype.HostName)
delete(pod.Spec.NodeSelector, simontype.NodeIp)
}
if pod.Annotations != nil {
delete(pod.Annotations, gpushareutils.CreationTime)
delete(pod.Annotations, gpushareutils.DeletionTime)
}
ClearPodUnscheduledAnno(pod)
sim.workloadPods = append(sim.workloadPods, pod)
}
// keep the order...
sort.Slice(sim.workloadPods, func(i, j int) bool {
return sim.workloadPods[i].Name < sim.workloadPods[j].Name
})
}
func (sim *Simulator) SortClusterPods(pods []*corev1.Pod) {
var err error
shufflePod := sim.customConfig.ShufflePod
if shufflePod {
sort.Slice(pods, func(i, j int) bool {
return pods[i].Name < pods[j].Name
})
rand.Shuffle(len(pods), func(i, j int) {
pods[i], pods[j] = pods[j], pods[i]
})
} else {
timeNow := time.Now() //.Format(time.RFC3339)
sort.SliceStable(pods, func(i, j int) bool {
var timeI, timeJ time.Time
if timeStr, ok := pods[i].Annotations[gpushareutils.CreationTime]; ok {
timeI, err = time.Parse(time.RFC3339, timeStr)
if err != nil {
log.Errorf("Time Parse %s err: %s\n", timeStr, err.Error())
timeI = timeNow
}
} else {
timeI = timeNow
}
if timeStr, ok := pods[j].Annotations[gpushareutils.CreationTime]; ok {
timeJ, err = time.Parse(time.RFC3339, timeStr)
if err != nil {
log.Errorf("Time Parse %s err: %s\n", timeStr, err.Error())
timeJ = timeNow
}
} else {
timeJ = timeNow
}
return timeI.Before(timeJ) || (timeI.Equal(timeJ) && pods[i].Name < pods[j].Name)
})
}
}
func (sim *Simulator) RunWorkloadInflationEvaluation(tag string) {
// 1. Generate a batch of inflation pods
inflationPods := sim.generateWorkloadInflationPods()
if len(inflationPods) == 0 {
return
}
// 2. Schedule them
failedPods := sim.SchedulePods(inflationPods)
utils.ReportFailedPods(failedPods)
// 3. Analyze the current state of the cluster
sim.ClusterAnalysis(tag)
// 4. Clean up all the inflation pods from the cluster
for _, pod := range inflationPods {
err := sim.deletePod(pod)
if err != nil {
log.Errorf("[RunWorkloadInflationEvaluation] failed to delete inflation pod(%s)\n",
utils.GeneratePodKey(pod))
}
}
}
func (sim *Simulator) generateWorkloadInflationPods() []*corev1.Pod {
n := len(sim.workloadPods)
if n == 0 {
log.Infof("[generateWorkloadInflationPods] original workload is empty\n")
return nil
}
podResCntMap := map[simontype.PodResource]float64{}
podGpuCntMap := map[string]int64{}
for _, v := range utils.GpuNumTypeList {
podGpuCntMap[v] = 0
}
ratio := sim.customConfig.WorkloadInflationConfig.Ratio
if ratio > 1 {
var inflationPods []*corev1.Pod
inflationNum := int(math.Ceil(float64(n)*ratio)) - n
podTotalMilliCpuReq, podTotalMilliGpuReq := sim.podTotalMilliCpuReq, sim.podTotalMilliGpuReq
for i := 0; i < inflationNum; i++ {
idx := rand.Intn(n)
podCloned, err := utils.MakeValidPodByPod(sim.workloadPods[idx].DeepCopy())
if err != nil {
log.Errorf("[generateWorkloadInflationPods] failed to clone pod(%s)\n",
utils.GeneratePodKey(sim.workloadPods[idx]))
continue
}
podCloned.Name = fmt.Sprintf("%s-clone-%d", podCloned.Name, i)
podRes := utils.GetPodResource(podCloned)
// avoid total resource requests of pods exceeding the capacity after adding inflation pods
if podRes.MilliCpu+podTotalMilliCpuReq <= sim.nodeTotalMilliCpu &&
podRes.MilliGpu*int64(podRes.GpuNumber)+podTotalMilliGpuReq <= sim.nodeTotalMilliGpu {
podTotalMilliCpuReq += podRes.MilliCpu
podTotalMilliGpuReq += podRes.TotalMilliGpu()
} else {
log.Infof("Stop early during workload inflation, "+
"because the total resource requests for pods(milli cpu %d, milli gpu %d) have exceeded "+
"the total resources of nodes(milli cpu %d, milli gpu %d).\n",
podRes.MilliCpu+podTotalMilliCpuReq, podRes.MilliGpu*int64(podRes.GpuNumber)+podTotalMilliGpuReq,
sim.nodeTotalMilliCpu, sim.nodeTotalMilliGpu)
break
}
inflationPods = append(inflationPods, podCloned)
// Accounting
if cnt, ok := podResCntMap[podRes]; ok {
podResCntMap[podRes] = cnt + 1
} else {
podResCntMap[podRes] = 1
}
switch podRes.GpuNumber {
case 0:
podGpuCntMap[utils.GpuNumTypeList[0]] += 1 // CPU
case 1:
if podRes.MilliGpu < gpushareutils.MILLI {
podGpuCntMap[utils.GpuNumTypeList[1]] += 1 // ShareGpu
} else {
podGpuCntMap[utils.GpuNumTypeList[2]] += 1 // OneGpu
}
case 2:
podGpuCntMap[utils.GpuNumTypeList[3]] += 1 // TwoGpu
case 4:
podGpuCntMap[utils.GpuNumTypeList[4]] += 1 // FourGpu
case 8:
podGpuCntMap[utils.GpuNumTypeList[5]] += 1 // EightGpu
default:
podGpuCntMap[utils.GpuNumTypeList[6]] += 1 // Others
}
}
tgtPodList := utils.SortTargetPodInDecreasingCount(podResCntMap)
log.Infof("(Inflation) Num of Total Pods: %d\n", len(inflationPods))
for _, k := range utils.GpuNumTypeList { // iter List, instead of Map, to guarantee order
ratioPct := 100.0 * float64(podGpuCntMap[k]) / float64(len(inflationPods))
log.Infof("(Inflation) %s Pods: %d (%.2f%%)\n", k, podGpuCntMap[k], ratioPct)
}
log.Infof("(Inflation) Num of Total Pod Sepc: %d\n", len(tgtPodList))
var cumRatioPct float64
for i := 0; i < len(tgtPodList); i++ {
podNumber := tgtPodList[i].Percentage
tgtPodList[i].Percentage = podNumber / float64(len(inflationPods)) // normalized to 0.0-1.0
ratioPct := 100.0 * tgtPodList[i].Percentage
cumRatioPct += ratioPct
log.Infof("[%d] %s: %.0f (%.2f%%, cumsum: %.2f%%)\n", i, tgtPodList[i].TargetPodResource.Repr(), podNumber, ratioPct, cumRatioPct)
}
log.Infof("workload inflation ratio: %.4f, "+
"the expected number of inflation pods: %d, "+
"The actual number of inflation pods: %d, "+
"total resource requests for pods(milli cpu %d, milli gpu %d), "+
"total resources of nodes(milli cpu %d, milli gpu %d)\n",
ratio, inflationNum, len(inflationPods), podTotalMilliCpuReq, podTotalMilliGpuReq, sim.nodeTotalMilliCpu, sim.nodeTotalMilliGpu)
return inflationPods
}
return nil
}
func displaySchedulerConfig(config *config.CompletedConfig) {