forked from kubeflow/arena
/
logs.go
97 lines (91 loc) · 3.06 KB
/
logs.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
// Copyright 2018 The Kubeflow Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package training
import (
"fmt"
"strings"
"github.com/kubeflow/arena/pkg/apis/types"
"github.com/kubeflow/arena/pkg/apis/utils"
podlogs "github.com/kubeflow/arena/pkg/podlogs"
v1 "k8s.io/api/core/v1"
)
// AcceptJobLog is used for arena-go-sdk
func AcceptJobLog(jobName string, trainingType types.TrainingJobType, args *types.LogArgs) error {
namespace := args.Namespace
// 1.transfer the training job type
if trainingType == types.UnknownTrainingJob {
return fmt.Errorf("Unsupport job type,arena only supports: [%v]", utils.GetSupportTrainingJobTypesInfo())
}
// 2.search the training job
job, err := SearchTrainingJob(jobName, namespace, trainingType)
if err != nil {
return err
}
// 3.if instance name not set,set the chief pod name to instance name
if args.InstanceName == "" {
name, err := getInstanceName(job)
if err != nil {
return err
}
args.InstanceName = name
}
podStatuses := map[string]string{}
for _, pod := range job.AllPods() {
status, _, _, _ := utils.DefinePodPhaseStatus(*pod)
podStatuses[pod.Name] = status
}
// 4.if the instance name is invalid,return error
status, ok := podStatuses[args.InstanceName]
if !ok {
return fmt.Errorf("invalid instance name %v in job %v,please use 'arena get %v' to make sure instance name.",
args.InstanceName,
jobName,
jobName,
)
}
if strings.Index(status, "Init:") == 0 || strings.Index(status, "PodInitializing") == 0 {
if args.ContainerName == "" {
args.ContainerName = "init-code"
}
}
logger := podlogs.NewPodLogger(args)
_, err = logger.AcceptLogs()
return err
}
func getInstanceName(job TrainingJob) (string, error) {
pods := job.AllPods()
// if not found pods,return an error
if len(pods) == 0 {
return "", fmt.Errorf("not found instances of the job %v", job.Name())
}
// if the job has only one pod,return its' name
if len(pods) == 1 {
return pods[0].Name, nil
}
// if job has many pods and the chief pod name is existed,return it
if job.ChiefPod() != nil && job.ChiefPod().Name != "" {
return job.ChiefPod().Name, nil
}
// return an error
return "", fmt.Errorf("%v", moreThanOneInstanceHelpInfo(pods))
}
func moreThanOneInstanceHelpInfo(pods []*v1.Pod) string {
header := fmt.Sprintf("There is %d instances have been found:", len(pods))
lines := []string{}
footer := "please use '-i' or '--instance' to filter."
for _, p := range pods {
lines = append(lines, fmt.Sprintf("%v", p.Name))
}
return fmt.Sprintf("%s\n\n%s\n\n%s\n", header, strings.Join(lines, "\n"), footer)
}