/
kube_job.go
1056 lines (969 loc) · 34.3 KB
/
kube_job.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
/*
Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserve.
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 kuberuntime
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"reflect"
"strings"
kubeflowv1 "github.com/kubeflow/common/pkg/apis/common/v1"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
kubeschema "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
schedulingv1beta1 "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
"github.com/PaddlePaddle/PaddleFlow/pkg/common/config"
"github.com/PaddlePaddle/PaddleFlow/pkg/common/k8s"
"github.com/PaddlePaddle/PaddleFlow/pkg/common/resources"
"github.com/PaddlePaddle/PaddleFlow/pkg/common/schema"
"github.com/PaddlePaddle/PaddleFlow/pkg/common/utils"
locationAwareness "github.com/PaddlePaddle/PaddleFlow/pkg/fs/location-awareness"
"github.com/PaddlePaddle/PaddleFlow/pkg/job/api"
"github.com/PaddlePaddle/PaddleFlow/pkg/job/runtime_v2/framework"
"github.com/PaddlePaddle/PaddleFlow/pkg/storage"
)
const (
DefaultReplicas = 1
)
type KubeBaseJob struct {
GVK kubeschema.GroupVersionKind
FrameworkVersion schema.FrameworkVersion
RuntimeClient framework.RuntimeClientInterface
JobQueue workqueue.RateLimitingInterface
getStatusFunc api.GetStatusFunc
}
func NewKubeBaseJob(gvk kubeschema.GroupVersionKind, fv schema.FrameworkVersion, c framework.RuntimeClientInterface) KubeBaseJob {
return KubeBaseJob{
GVK: gvk,
FrameworkVersion: fv,
RuntimeClient: c,
}
}
func (kj *KubeBaseJob) String(name string) string {
return fmt.Sprintf("%s job %s on %s", kj.GVK.String(), name, kj.RuntimeClient.Cluster())
}
func (kj *KubeBaseJob) Stop(ctx context.Context, job *api.PFJob) error {
if job == nil {
return fmt.Errorf("job is nil")
}
jobName := job.NamespacedName()
log.Infof("begin to stop %s", kj.String(jobName))
if err := kj.RuntimeClient.Delete(job.Namespace, job.ID, kj.FrameworkVersion); err != nil {
log.Errorf("stop %s failed, err: %v", kj.String(jobName), err)
return err
}
return nil
}
func (kj *KubeBaseJob) Update(ctx context.Context, job *api.PFJob) error {
if job == nil {
return fmt.Errorf("job is nil")
}
jobName := job.NamespacedName()
log.Infof("begin to update %s", kj.String(jobName))
if err := UpdateKubeJob(job, kj.RuntimeClient, kj.FrameworkVersion); err != nil {
log.Errorf("update %s failed, err: %v", kj.String(jobName), err)
return err
}
return nil
}
func (kj *KubeBaseJob) Delete(ctx context.Context, job *api.PFJob) error {
if job == nil {
return fmt.Errorf("job is nil")
}
jobName := job.NamespacedName()
log.Infof("begin to delete %s ", kj.String(jobName))
if err := kj.RuntimeClient.Delete(job.Namespace, job.ID, kj.FrameworkVersion); err != nil {
log.Errorf("delete %s failed, err %v", kj.String(jobName), err)
return err
}
return nil
}
func (kj *KubeBaseJob) GetLog(ctx context.Context, jobLogRequest schema.JobLogRequest) (schema.JobLogInfo, error) {
// TODO: add get log logic
return schema.JobLogInfo{}, nil
}
func (kj *KubeBaseJob) AddJobEventListener(ctx context.Context,
jobQueue workqueue.RateLimitingInterface,
listener interface{},
jobStatus api.GetStatusFunc,
filterFunc func(interface{}) bool) error {
if jobQueue == nil || listener == nil || jobStatus == nil {
return fmt.Errorf("add job event listener failed, err: listener is nil")
}
if filterFunc == nil {
filterFunc = ResponsibleForJob
}
kj.JobQueue = jobQueue
kj.getStatusFunc = jobStatus
informer := listener.(cache.SharedIndexInformer)
informer.AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: filterFunc,
Handler: cache.ResourceEventHandlerFuncs{
AddFunc: kj.add,
UpdateFunc: kj.update,
DeleteFunc: kj.delete,
},
})
return nil
}
func (kj *KubeBaseJob) add(obj interface{}) {
jobSyncInfo, err := JobAddFunc(obj, kj.getStatusFunc)
if err != nil {
return
}
kj.JobQueue.Add(jobSyncInfo)
}
func (kj *KubeBaseJob) update(old, new interface{}) {
jobSyncInfo, err := JobUpdateFunc(old, new, kj.getStatusFunc)
if err != nil {
return
}
kj.JobQueue.Add(jobSyncInfo)
}
func (kj *KubeBaseJob) delete(obj interface{}) {
jobSyncInfo, err := JobDeleteFunc(obj, kj.getStatusFunc)
if err != nil {
return
}
kj.JobQueue.Add(jobSyncInfo)
}
// ResponsibleForJob filter job belong to PaddleFlow
func ResponsibleForJob(obj interface{}) bool {
job := obj.(*unstructured.Unstructured)
labels := job.GetLabels()
if labels != nil && labels[schema.JobOwnerLabel] == schema.JobOwnerValue {
log.Debugf("responsible for handle job. jobName:[%s]", job.GetName())
return true
}
log.Debugf("responsible for skip job. jobName:[%s]", job.GetName())
return false
}
// getDefaultTemplate get default template from file
func getDefaultTemplate(framework schema.Framework, jobType schema.JobType, jobMode string) ([]byte, error) {
// jobTemplateName corresponds to the footer comment of yaml file `config/server/default/job/job_template.yaml`
jobTemplateName := ""
// the footer comment of all type job as the follow:
// single -> single-job, workflow -> workflow-job,
// spark -> spark-job, ray -> ray-job
// paddle with ps mode -> paddle-ps-job
// paddle with collective mode -> paddle-collective-job
// tensorflow with ps mode -> tensorflow-ps-job
// pytorch with ps mode -> pytorch-ps-job
switch jobType {
case schema.TypeSingle, schema.TypeWorkflow:
jobTemplateName = fmt.Sprintf("%s-job", jobType)
case schema.TypeDistributed:
if framework == schema.FrameworkSpark || framework == schema.FrameworkRay || framework == schema.FrameworkMPI {
jobTemplateName = fmt.Sprintf("%s-job", framework)
} else {
jobTemplateName = fmt.Sprintf("%s-%s-job", framework, strings.ToLower(jobMode))
}
default:
return []byte{}, fmt.Errorf("job type %s is not supported", jobType)
}
log.Infof("get default template for job, and template name is %s", jobTemplateName)
jobTemplate, find := config.DefaultJobTemplate[jobTemplateName]
if !find {
return []byte{}, fmt.Errorf("job template %s is not found", jobTemplateName)
}
return jobTemplate, nil
}
func CreateKubeJobFromYaml(jobEntity interface{}, groupVersionKind kubeschema.GroupVersionKind, job *api.PFJob) error {
if job == nil {
return fmt.Errorf("job is nil")
}
log.Debugf("createJobFromYaml jobEntity[%+v] %v", jobEntity, reflect.TypeOf(jobEntity))
// get extensionTemplate
if len(job.ExtensionTemplate) == 0 {
// get builtin template
var err error
job.IsCustomYaml = false
job.ExtensionTemplate, err = getDefaultTemplate(job.Framework, job.JobType, job.JobMode)
if err != nil {
return fmt.Errorf("get default template failed, err: %v", err)
}
} else {
// get template from user
job.IsCustomYaml = true
}
// decode []byte into unstructured.Unstructured
dec := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
unstructuredObj := &unstructured.Unstructured{}
if _, _, err := dec.Decode(job.ExtensionTemplate, nil, unstructuredObj); err != nil {
log.Errorf("Decode from yamlFile[%s] failed! err:[%v]\n", string(job.ExtensionTemplate), err)
return err
}
parsedGVK := unstructuredObj.GroupVersionKind()
log.Debugf("unstructuredObj=%v, GroupVersionKind=[%v]", unstructuredObj, parsedGVK)
if parsedGVK.String() != groupVersionKind.String() {
err := fmt.Errorf("expect GroupVersionKind is %s, but got %s", groupVersionKind.String(), parsedGVK.String())
log.Errorf("Decode from yamlFile[%s] failed! err:[%v]\n", string(job.ExtensionTemplate), err)
return err
}
// convert unstructuredObj.Object into entity
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredObj.Object, jobEntity); err != nil {
log.Errorf("convert map struct object[%+v] to acutal job type failed: %v", unstructuredObj.Object, err)
return err
}
log.Debugf("get jobEntity[%+v] from yamlContent[%s]job", jobEntity, job.ExtensionTemplate)
return nil
}
func ValidatePodResources(spec *corev1.PodSpec) error {
for index, container := range spec.Containers {
pfRes := k8s.NewResource(container.Resources.Requests)
if pfRes.IsZero() {
spec.Containers[index].Resources.Requests = k8s.NewMinResourceList()
log.Warnf("podSpec %v container %d requests is empty, Resources: %v, set default", spec, index, spec.Containers[index].Resources.Requests)
}
}
return nil
}
// In the bellow, build kubernetes job metadata, spec and so on.
// BuildJobMetadata build metadata for kubernetes job
func BuildJobMetadata(metadata *metav1.ObjectMeta, job *api.PFJob) {
if metadata == nil || job == nil {
return
}
metadata.Name = job.ID
metadata.Namespace = job.Namespace
metadata.Annotations = appendMapsIfAbsent(metadata.Annotations, job.Annotations)
metadata.Labels = appendMapsIfAbsent(metadata.Labels, job.Labels)
metadata.Labels[schema.JobOwnerLabel] = schema.JobOwnerValue
metadata.Labels[schema.JobIDLabel] = job.ID
if len(job.QueueName) > 0 {
metadata.Annotations[schema.QueueLabelKey] = job.QueueName
metadata.Labels[schema.QueueLabelKey] = job.QueueName
}
}
func BuildTaskMetadata(metadata *metav1.ObjectMeta, jobID string, taskConf *schema.Conf) {
if metadata == nil || taskConf == nil {
return
}
if taskConf.GetName() != "" {
metadata.Name = taskConf.GetName()
}
if taskConf.GetNamespace() != "" {
metadata.Namespace = taskConf.GetNamespace()
}
metadata.Annotations = appendMapsIfAbsent(metadata.Annotations, taskConf.GetAnnotations())
metadata.Labels = appendMapsIfAbsent(metadata.Labels, taskConf.GetLabels())
metadata.Labels[schema.JobIDLabel] = jobID
metadata.Labels[schema.JobOwnerLabel] = schema.JobOwnerValue
// TODO: add more metadata for task
}
// appendMapsIfAbsent append Maps if absent, only support string type
func appendMapsIfAbsent(Maps map[string]string, addMaps map[string]string) map[string]string {
if Maps == nil {
Maps = make(map[string]string)
}
for key, value := range addMaps {
if _, ok := Maps[key]; !ok {
Maps[key] = value
}
}
return Maps
}
func BuildPodSpec(podSpec *corev1.PodSpec, task schema.Member) error {
if podSpec == nil {
return fmt.Errorf("build pod spec failed, err: podSpec or task is nil")
}
// fill priorityClassName and schedulerName
err := buildPriorityAndScheduler(podSpec, task.Priority)
if err != nil {
log.Errorln(err)
return err
}
// fill volumes
fileSystems := task.Conf.GetAllFileSystem()
podSpec.Volumes = BuildVolumes(podSpec.Volumes, fileSystems)
// fill affinity
if len(fileSystems) != 0 {
var fsIDs []string
for _, fs := range fileSystems {
fsIDs = append(fsIDs, fs.ID)
}
podSpec.Affinity, err = generateAffinity(podSpec.Affinity, fsIDs)
if err != nil {
return err
}
}
// fill restartPolicy
patchRestartPolicy(podSpec, task)
// build containers
if err = buildPodContainers(podSpec, task); err != nil {
log.Errorf("failed to fill containers, err=%v", err)
return err
}
return nil
}
func buildPriorityAndScheduler(podSpec *corev1.PodSpec, priorityName string) error {
if podSpec == nil {
return fmt.Errorf("build scheduling policy failed, err: podSpec is nil")
}
// fill priorityClassName
podSpec.PriorityClassName = KubePriorityClass(priorityName)
// fill SchedulerName
podSpec.SchedulerName = config.GlobalServerConfig.Job.SchedulerName
return nil
}
func BuildPod(pod *corev1.Pod, task schema.Member) error {
if pod == nil {
return fmt.Errorf("build pod failed, err: podSpec is nil")
}
// fill priorityClassName and schedulerName
err := buildPriorityAndScheduler(&pod.Spec, task.Priority)
if err != nil {
log.Errorln(err)
return err
}
// patch node name
nodeName := task.Conf.GetEnvValue(schema.ENVK3SNodeName)
if nodeName != "" {
pod.Spec.NodeName = nodeName
}
// fill volumes
fileSystems := task.Conf.GetAllFileSystem()
if len(task.Conf.GetProcessedFileSystem()) > 0 {
fileSystems = task.Conf.GetProcessedFileSystem()
}
pod.Spec.Volumes = BuildVolumes(pod.Spec.Volumes, fileSystems)
// fill fs affinity
if len(fileSystems) != 0 {
var fsIDs []string
for _, fs := range fileSystems {
fsIDs = append(fsIDs, fs.ID)
}
pod.Spec.Affinity, err = generateAffinity(pod.Spec.Affinity, fsIDs)
if err != nil {
return err
}
}
// fill restartPolicy
patchRestartPolicy(&pod.Spec, task)
// patch config for Paddle Para
_, find := task.Env[schema.EnvPaddleParaJob]
if find {
if err = patchPaddlePara(pod, task.Name, task); err != nil {
log.Errorf("patch parameters for paddle para job failed, err: %v", err)
return err
}
}
// build containers
if err = buildPodContainers(&pod.Spec, task); err != nil {
log.Errorf("failed to fill containers, err=%v", err)
return err
}
return nil
}
func patchRestartPolicy(podSpec *corev1.PodSpec, task schema.Member) {
if podSpec == nil {
return
}
// fill restartPolicy
restartPolicy := task.GetRestartPolicy()
if restartPolicy == string(corev1.RestartPolicyAlways) ||
restartPolicy == string(corev1.RestartPolicyOnFailure) {
podSpec.RestartPolicy = corev1.RestartPolicy(restartPolicy)
} else {
podSpec.RestartPolicy = corev1.RestartPolicyNever
}
}
func generateAffinity(affinity *corev1.Affinity, fsIDs []string) (*corev1.Affinity, error) {
nodeAffinity, err := locationAwareness.FsNodeAffinity(fsIDs)
if err != nil {
err = fmt.Errorf("KubeJob generateAffinity err: %v", err)
log.Errorf(err.Error())
return nil, err
}
return mergeNodeAffinity(affinity, nodeAffinity), nil
}
func mergeNodeAffinity(former, new *corev1.Affinity) *corev1.Affinity {
if new == nil {
log.Infof("mergeNodeAffinity new affinity is nil")
return former
}
if former == nil {
log.Infof("mergeNodeAffinity former affinity is nil")
return new
}
if former.NodeAffinity == nil {
former.NodeAffinity = new.NodeAffinity
return former
}
// merge required
newRequired := new.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution
if newRequired != nil && len(newRequired.NodeSelectorTerms) != 0 {
formerRequired := former.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution
if formerRequired == nil || len(formerRequired.NodeSelectorTerms) == 0 {
formerRequired = newRequired
} else {
formerRequired.NodeSelectorTerms = append(formerRequired.NodeSelectorTerms, newRequired.NodeSelectorTerms...)
}
}
// merge preferred
former.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution = append(
former.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution,
new.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution...)
return former
}
func buildPodContainers(podSpec *corev1.PodSpec, task schema.Member) error {
log.Debugf("fillContainersInPod for job[%s]", task.Name)
if podSpec.Containers == nil || len(podSpec.Containers) == 0 {
podSpec.Containers = []corev1.Container{{}}
}
// only fill the first container
index := 0
if err := fillContainer(&podSpec.Containers[index], task.Name, task); err != nil {
log.Errorf("fillContainer occur a err[%v]", err)
return err
}
log.Debugf("job[%s].Spec.Tasks=[%+v]", task.Name, podSpec.Containers)
return nil
}
func fillContainer(container *corev1.Container, podName string, task schema.Member) error {
log.Debugf("fillContainer for job[%s]", podName)
// fill name
if task.Name != "" {
container.Name = task.Name
}
// fill image
if task.Image != "" {
container.Image = task.Image
}
// fill command
fileSystems := task.Conf.GetAllFileSystem()
if len(task.Conf.GetProcessedFileSystem()) > 0 {
fileSystems = task.Conf.GetProcessedFileSystem()
}
if task.Command != "" {
workDir := getWorkDir(&task, fileSystems, task.Env)
container.Command = generateContainerCommand(task.Command, workDir)
}
// container.Args would be passed
// fill resource
var err error
container.Resources, err = GenerateResourceRequirements(task.Flavour, task.LimitFlavour)
if err != nil {
log.Errorf("generate resource requirements failed, err: %v", err)
return err
}
// fill env
container.Env = BuildEnvVars(container.Env, task.Env)
// fill volumeMount
container.VolumeMounts = BuildVolumeMounts(container.VolumeMounts, fileSystems)
log.Debugf("fillContainer completed: pod[%s]-container[%s]", podName, container.Name)
return nil
}
func getWorkDir(task *schema.Member, fileSystems []schema.FileSystem, envs map[string]string) string {
// prepare fs and envs
if task != nil {
fileSystems = task.Conf.GetAllFileSystem()
if len(task.Conf.GetProcessedFileSystem()) > 0 {
fileSystems = task.Conf.GetProcessedFileSystem()
}
envs = task.Env
}
if len(envs) == 0 {
envs = make(map[string]string)
}
// check workdir, which exist only if there is more than one file system and env.'EnvMountPath' is not NONE
hasWorkDir := len(fileSystems) != 0 && strings.ToUpper(envs[schema.EnvMountPath]) != "NONE"
if !hasWorkDir {
return ""
}
workdir := ""
mountPath := utils.MountPathClean(fileSystems[0].MountPath)
log.Infof("getWorkDir by hasWorkDir: true,mountPath: %s, task: %v", mountPath, task)
if mountPath != "/" {
workdir = fileSystems[0].MountPath
} else {
workdir = filepath.Join(schema.DefaultFSMountPath, fileSystems[0].ID)
}
envs[schema.EnvJobWorkDir] = workdir
return workdir
}
// generateContainerCommand if task is not nil, prefer to using info in task, otherwise using job's
func generateContainerCommand(command string, workdir string) []string {
command = strings.TrimPrefix(command, "bash -c")
command = strings.TrimPrefix(command, "sh -c")
if workdir != "" {
// if command is not empty
if command != "" {
command = fmt.Sprintf("cd %s; %s", workdir, command)
} else {
command = fmt.Sprintf("cd %s", workdir)
}
}
commands := []string{"sh", "-c", command}
return commands
}
func GenerateResourceRequirements(request, limitFlavour schema.Flavour) (corev1.ResourceRequirements, error) {
log.Infof("GenerateResourceRequirements by request:[%+v]", request)
flavourResource, err := resources.NewResourceFromMap(request.ToMap())
if err != nil {
log.Errorf("GenerateResourceRequirements by request:[%+v] error:%v", request, err)
return corev1.ResourceRequirements{}, err
}
limitFlavourResource, err := resources.NewResourceFromMap(limitFlavour.ToMap())
if err != nil {
log.Errorf("GenerateResourceRequirements by limitFlavour:[%+v] error:%v", request, err)
return corev1.ResourceRequirements{}, err
}
resources := corev1.ResourceRequirements{
Requests: k8s.NewResourceList(flavourResource),
}
if strings.ToUpper(limitFlavour.Name) == schema.EnvJobLimitFlavourNone {
resources.Limits = nil
} else if limitFlavourResource.CPU() == 0 || limitFlavourResource.Memory() == 0 {
// limit set zero, patch the same value as request
resources.Limits = k8s.NewResourceList(flavourResource)
} else {
// limit set specified value
resources.Limits = k8s.NewResourceList(limitFlavourResource)
}
return resources, nil
}
// appendEnvIfAbsent append new env if not exist in baseEnvs
func appendEnvIfAbsent(baseEnvs []corev1.EnvVar, addEnvs []corev1.EnvVar) []corev1.EnvVar {
if baseEnvs == nil {
return addEnvs
}
keySet := make(map[string]bool)
for _, cur := range baseEnvs {
keySet[cur.Name] = true
}
for _, cur := range addEnvs {
if _, ok := keySet[cur.Name]; !ok {
baseEnvs = append(baseEnvs, cur)
}
}
return baseEnvs
}
func generateEnvVars(EnvVars map[string]string) []corev1.EnvVar {
envs := make([]corev1.EnvVar, 0)
for key, value := range EnvVars {
env := corev1.EnvVar{
Name: key,
Value: value,
}
envs = append(envs, env)
}
return envs
}
// BuildEnvVars merge EnvVars
func BuildEnvVars(baseEnvs []corev1.EnvVar, EnvVars map[string]string) []corev1.EnvVar {
return appendEnvIfAbsent(baseEnvs, generateEnvVars(EnvVars))
}
// BuildVolumes convert PaddleFlow FileSystem to kubernetes volumes
func BuildVolumes(volumes []corev1.Volume, fileSystem []schema.FileSystem) []corev1.Volume {
return appendVolumesIfAbsent(volumes, generateVolumes(fileSystem))
}
// BuildVolumeMounts covert PaddleFlow FileSystem to kubernetes VolumeMount
func BuildVolumeMounts(volumeMounts []corev1.VolumeMount, fileSystem []schema.FileSystem) []corev1.VolumeMount {
return appendMountsIfAbsent(volumeMounts, generateVolumeMounts(fileSystem))
}
// appendVolumesIfAbsent append newElements if not exist in volumes
// if job with tasks, it should be like
// `Volumes = appendVolumesIfAbsent(Volumes, generateVolumes(taskFs))`
// otherwise,
// `Volumes = appendVolumesIfAbsent(Volumes, generateVolumes(kubeJob.FileSystems))`
func appendVolumesIfAbsent(volumes []corev1.Volume, newElements []corev1.Volume) []corev1.Volume {
log.Infof("appendVolumesIfAbsent volumes=%+v, newElements=%+v", volumes, newElements)
if len(newElements) == 0 {
return volumes
}
if len(volumes) == 0 {
volumes = []corev1.Volume{}
}
volumesDict := make(map[string]bool)
for _, v := range volumes {
volumesDict[v.Name] = true
}
for _, cur := range newElements {
if volumesDict[cur.Name] {
log.Debugf("volume %s has been created in jobTemplate", cur.Name)
continue
}
volumesDict[cur.Name] = true
volumes = append(volumes, cur)
}
return volumes
}
// TODO: add TransferFS interface on runtime
// generateVolumes generate kubernetes volumes with schema.FileSystem
func generateVolumes(fileSystem []schema.FileSystem) []corev1.Volume {
log.Debugf("generateVolumes FileSystems[%+v]", fileSystem)
var vs []corev1.Volume
if len(fileSystem) == 0 {
log.Debugf("found len(fileSystem) is 0 when calling generateVolumes(fs), fs: %+v", fileSystem)
return vs
}
for _, fs := range fileSystem {
volume := corev1.Volume{
Name: fs.Name,
}
log.Infof("build volumes, fs info is %v", fs)
if fs.Type == schema.PFSTypeLocal {
// use hostPath
volume.VolumeSource = corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: fs.HostPath,
},
}
} else {
// use pvc
volume.VolumeSource = corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: schema.ConcatenatePVCName(fs.ID),
},
}
}
vs = append(vs, volume)
}
return vs
}
// appendMountsIfAbsent append volumeMount if not exist in volumeMounts
// if job with tasks, it should be like
// `VolumeMounts = appendMountsIfAbsent(VolumeMounts, generateVolumeMounts(taskFs))`
// otherwise,
// `VolumeMounts = appendMountsIfAbsent(VolumeMounts, generateVolumeMounts(kubeJob.FileSystems))`
func appendMountsIfAbsent(volumeMounts []corev1.VolumeMount, newElements []corev1.VolumeMount) []corev1.VolumeMount {
log.Infof("appendMountsIfAbsent volumeMounts=%+v, newElements=%+v", volumeMounts, newElements)
if volumeMounts == nil {
volumeMounts = []corev1.VolumeMount{}
}
if len(newElements) == 0 {
return volumeMounts
}
// deduplication
volumeMountsDict := make(map[string]string)
for _, cur := range volumeMounts {
mountPath := utils.MountPathClean(cur.MountPath)
volumeMountsDict[mountPath] = cur.Name
}
for _, cur := range newElements {
mountPath := utils.MountPathClean(cur.MountPath)
if _, exist := volumeMountsDict[mountPath]; exist {
log.Debugf("moutPath %s in volumeMount %s has been created in jobTemplate", cur.MountPath, cur.Name)
continue
}
volumeMountsDict[mountPath] = cur.Name
volumeMounts = append(volumeMounts, cur)
}
return volumeMounts
}
// TODO: add TransferFS interface on runtime
// generateVolumeMounts generate kubernetes volumeMounts with schema.FileSystem
func generateVolumeMounts(fileSystems []schema.FileSystem) []corev1.VolumeMount {
log.Infof("generateVolumeMounts fileSystems:%+v", fileSystems)
var vms []corev1.VolumeMount
if len(fileSystems) == 0 {
log.Debug("generateVolumeMounts fileSystems len is 0")
return vms
}
for _, fs := range fileSystems {
log.Debugf("generateVolumeMounts walking fileSystem %+v", fs)
mountPath := utils.MountPathClean(fs.MountPath)
if mountPath == "/" {
fs.MountPath = filepath.Join(schema.DefaultFSMountPath, fs.ID)
}
mp := corev1.MountPropagationHostToContainer
volumeMount := corev1.VolumeMount{
Name: fs.Name,
ReadOnly: fs.ReadOnly,
MountPath: fs.MountPath,
SubPath: fs.SubPath,
}
if fs.Type != schema.PFSTypeLocal {
volumeMount.MountPropagation = &mp
}
vms = append(vms, volumeMount)
}
return vms
}
func KubePriorityClass(priority string) string {
switch priority {
case schema.EnvJobVeryLowPriority:
return schema.PriorityClassVeryLow
case schema.EnvJobLowPriority:
return schema.PriorityClassLow
case schema.EnvJobNormalPriority:
return schema.PriorityClassNormal
case schema.EnvJobHighPriority:
return schema.PriorityClassHigh
case schema.EnvJobVeryHighPriority:
return schema.PriorityClassVeryHigh
default:
return schema.PriorityClassNormal
}
}
// patchPaddlePara patch some parameters for paddle para job, and must be work with a shared gpu device plugin
// environments for paddle para job:
//
// PF_PADDLE_PARA_JOB: defines the job is a paddle para job
// PF_PADDLE_PARA_PRIORITY: defines the priority of paddle para job, 0 is high, and 1 is low.
// PF_PADDLE_PARA_CONFIG_FILE: defines the config of paddle para job
func patchPaddlePara(podTemplate *corev1.Pod, jobName string, task schema.Member) error {
// get parameters from user's job config
var paddleParaPriority string
// TODO: use task.Conf.Getxxx method
p := task.Env[schema.EnvPaddleParaPriority]
switch strings.ToLower(p) {
case schema.PriorityClassHigh:
paddleParaPriority = "0"
case schema.PriorityClassLow, "":
paddleParaPriority = "1"
default:
return fmt.Errorf("priority %s for paddle para job is invalid", p)
}
// the config path of paddle para gpu job on host os, which will be mounted to job
gpuConfigFile := schema.PaddleParaGPUConfigFilePath
value, find := task.Env[schema.EnvPaddleParaConfigHostFile]
if find {
gpuConfigFile = value
}
gpuConfigDirPath := filepath.Dir(gpuConfigFile)
if gpuConfigDirPath == "/" {
return fmt.Errorf("the directory of gpu config file %s cannot be mounted", gpuConfigFile)
}
// 1. patch jobName and priority in Annotations
if podTemplate.ObjectMeta.Annotations == nil {
podTemplate.ObjectMeta.Annotations = make(map[string]string)
}
podTemplate.ObjectMeta.Annotations[schema.PaddleParaAnnotationKeyJobName] = jobName
podTemplate.ObjectMeta.Annotations[schema.PaddleParaAnnotationKeyPriority] = paddleParaPriority
// 2. patch env, including config file and job name
env := podTemplate.Spec.Containers[0].Env
podTemplate.Spec.Containers[0].Env = append([]corev1.EnvVar{
{
Name: schema.PaddleParaEnvJobName,
Value: jobName,
},
{
Name: schema.PaddleParaEnvGPUConfigFile,
Value: gpuConfigFile,
},
}, env...)
// 3. patch volumes and volumeMounts
dirType := corev1.HostPathDirectory
volumes := podTemplate.Spec.Volumes
podTemplate.Spec.Volumes = append([]corev1.Volume{
{
Name: schema.PaddleParaVolumeName,
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: gpuConfigDirPath,
Type: &dirType,
},
},
},
}, volumes...)
volumeMounts := podTemplate.Spec.Containers[0].VolumeMounts
podTemplate.Spec.Containers[0].VolumeMounts = append([]corev1.VolumeMount{
{
Name: schema.PaddleParaVolumeName,
MountPath: gpuConfigDirPath,
},
}, volumeMounts...)
return nil
}
// GetKubeflowJobStatus covert job status of kubeflow application to paddleflow job status
func GetKubeflowJobStatus(jobCond kubeflowv1.JobCondition) (schema.JobStatus, string, error) {
status := schema.JobStatus("")
msg := jobCond.Message
switch jobCond.Type {
case kubeflowv1.JobCreated:
status = schema.StatusJobPending
case kubeflowv1.JobRunning, kubeflowv1.JobRestarting:
status = schema.StatusJobRunning
case kubeflowv1.JobSucceeded:
status = schema.StatusJobSucceeded
case kubeflowv1.JobFailed:
status = schema.StatusJobFailed
default:
return status, msg, fmt.Errorf("unexpected job status: %s", jobCond.Type)
}
return status, msg, nil
}
// BuildPodTemplateSpec build PodTemplateSpec for built-in distributed job, such as PaddleJob, PyTorchJob, TFJob and so on
func BuildPodTemplateSpec(podSpec *corev1.PodTemplateSpec, jobID string, task *schema.Member) error {
if podSpec == nil || task == nil {
return fmt.Errorf("podTemplateSpec or task is nil")
}
// build task metadata
BuildTaskMetadata(&podSpec.ObjectMeta, jobID, &schema.Conf{})
// build pod spec
err := BuildPodSpec(&podSpec.Spec, *task)
if err != nil {
log.Errorf("build pod spec failed, err: %v", err)
return err
}
return nil
}
// KubeflowReplicaSpec build ReplicaSpec for kubeflow job, such as PyTorchJob, TFJob and so on.
func KubeflowReplicaSpec(replicaSpec *kubeflowv1.ReplicaSpec, jobID string, task *schema.Member) error {
if replicaSpec == nil || task == nil {
return fmt.Errorf("build kubeflow replica spec failed, err: replicaSpec or task is nil")
}
// set Replicas for job
replicas := int32(task.Replicas)
replicaSpec.Replicas = &replicas
// set RestartPolicy
// TODO: make RestartPolicy configurable
replicaSpec.RestartPolicy = kubeflowv1.RestartPolicyNever
// TODO: remove hard coded schedulerName when upstream package is fixed
// HARD CODE schedulerName to default scheduler, fix KubeFlow training operator bug at volcano scheduler TEMPERATELY
// see issue https://github.com/kubeflow/training-operator/issues/1630
replicaSpec.Template.Spec.SchedulerName = "default-scheduler"
// set PodTemplate
return BuildPodTemplateSpec(&replicaSpec.Template, jobID, task)
}
// KubeflowRunPolicy build RunPolicy for kubeflow job, such as PyTorchJob, TFJob and so on.
func KubeflowRunPolicy(runPolicy *kubeflowv1.RunPolicy, minResources *corev1.ResourceList, queueName, priority string) error {
if runPolicy == nil {
return fmt.Errorf("build run policy for kubeflow job faield, err: runPolicy is nil")
}
// TODO set cleanPolicy
// set SchedulingPolicy
if runPolicy.SchedulingPolicy == nil {
runPolicy.SchedulingPolicy = &kubeflowv1.SchedulingPolicy{}
}
runPolicy.SchedulingPolicy.Queue = queueName
runPolicy.SchedulingPolicy.PriorityClass = KubePriorityClass(priority)
if minResources != nil {
runPolicy.SchedulingPolicy.MinResources = minResources
}
return nil
}
// Operations for kubernetes job, including single, paddle, sparkapp, tensorflow, pytorch, mpi jobs and so on.
// getPodGroupName get the name of pod group
func getPodGroupName(jobID string) string {
job, err := storage.Job.GetJobByID(jobID)
if err != nil {
log.Errorf("get job %s failed, err %v", jobID, err)
return ""
}
// TODO: remove job type TypeVcJob
if job.Type == string(schema.TypeVcJob) {
return jobID
}
pgName := ""
switch job.Framework {
case schema.FrameworkPaddle, schema.FrameworkPytorch, schema.FrameworkTF, schema.FrameworkMXNet:
pgName = jobID
case schema.FrameworkSpark:
pgName = fmt.Sprintf("spark-%s-pg", jobID)
case schema.FrameworkStandalone, "":
runtimeInfo := job.RuntimeInfo.(map[string]interface{})
jobObj := &unstructured.Unstructured{}
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(runtimeInfo, jobObj); err != nil {
log.Errorf("convert obj to unstructed.Unstructed failed, err %v", err)
return ""
}
anno := jobObj.GetAnnotations()
if anno != nil {
pgName = anno[schedulingv1beta1.KubeGroupNameAnnotationKey]
}
if pgName == "" {
pgName = fmt.Sprintf("podgroup-%s", jobObj.GetUID())
}
default:
log.Warningf("the framework[%s] of job is not supported", job.Framework)
pgName = jobID
}
return pgName
}
func updateKubeJobPriority(jobInfo *api.PFJob, runtimeClient framework.RuntimeClientInterface) error {
// get pod group name for job
pgName := getPodGroupName(jobInfo.ID)
if len(pgName) == 0 {
err := fmt.Errorf("update priority for job %s failed, pod group not found", jobInfo.ID)
log.Errorln(err)
return err
}
frameworkVersion := schema.NewFrameworkVersion(k8s.PodGroupGVK.Kind, k8s.PodGroupGVK.GroupVersion().String())
obj, err := runtimeClient.Get(jobInfo.Namespace, pgName, frameworkVersion)
if err != nil {
log.Errorf("get pod group for job %s failed, err: %v", jobInfo.ID, err)
return err
}
unObj := obj.(*unstructured.Unstructured)
oldPG := &schedulingv1beta1.PodGroup{}
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(unObj.Object, oldPG); err != nil {
log.Errorf("convert unstructured object [%v] to pod group failed. err: %v", obj, err)
return err
}
if oldPG.Status.Phase != schedulingv1beta1.PodGroupInqueue &&
oldPG.Status.Phase != schedulingv1beta1.PodGroupPending {
errmsg := fmt.Errorf("the job %s is already scheduled", jobInfo.ID)
log.Errorln(errmsg)
return errmsg