Skip to content

Commit ed0d1ab

Browse files
authored
fix bug: error for getting training job logs when no chief pod (#501)
1 parent 0195afc commit ed0d1ab

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

pkg/training/logs.go

+36-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ package training
1616

1717
import (
1818
"fmt"
19+
"strings"
1920

2021
"github.com/kubeflow/arena/pkg/apis/types"
2122
"github.com/kubeflow/arena/pkg/apis/utils"
2223
podlogs "github.com/kubeflow/arena/pkg/podlogs"
24+
v1 "k8s.io/api/core/v1"
2325
)
2426

2527
// AcceptJobLog is used for arena-go-sdk
@@ -34,10 +36,13 @@ func AcceptJobLog(jobName string, trainingType types.TrainingJobType, args *type
3436
if err != nil {
3537
return err
3638
}
37-
chiefPod := job.ChiefPod()
3839
// 3.if instance name not set,set the chief pod name to instance name
39-
if args.InstanceName == "" && chiefPod != nil {
40-
args.InstanceName = chiefPod.Name
40+
if args.InstanceName == "" {
41+
name, err := getInstanceName(job)
42+
if err != nil {
43+
return err
44+
}
45+
args.InstanceName = name
4146
}
4247
podStatuses := map[string]string{}
4348
for _, pod := range job.AllPods() {
@@ -72,3 +77,31 @@ func getTrainingJobTypes() []string {
7277
}
7378
return jobTypes
7479
}
80+
81+
func getInstanceName(job TrainingJob) (string, error) {
82+
pods := job.AllPods()
83+
// if not found pods,return an error
84+
if pods == nil || len(pods) == 0 {
85+
return "", fmt.Errorf("not found instances of the job %v", job.Name())
86+
}
87+
// if the job has only one pod,return its' name
88+
if len(pods) == 1 {
89+
return pods[0].Name, nil
90+
}
91+
// if job has many pods and the chief pod name is existed,return it
92+
if job.ChiefPod() != nil && job.ChiefPod().Name != "" {
93+
return job.ChiefPod().Name, nil
94+
}
95+
// return an error
96+
return "", fmt.Errorf("%v", moreThanOneInstanceHelpInfo(pods))
97+
}
98+
99+
func moreThanOneInstanceHelpInfo(pods []*v1.Pod) string {
100+
header := fmt.Sprintf("There is %d instances have been found:", len(pods))
101+
lines := []string{}
102+
footer := fmt.Sprintf("please use '-i' or '--instance' to filter.")
103+
for _, p := range pods {
104+
lines = append(lines, fmt.Sprintf("%v", p.Name))
105+
}
106+
return fmt.Sprintf("%s\n\n%s\n\n%s\n", header, strings.Join(lines, "\n"), footer)
107+
}

0 commit comments

Comments
 (0)