Skip to content

Commit

Permalink
fix bug: error for getting training job logs when no chief pod (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
happy2048 authored Mar 15, 2021
1 parent 0195afc commit ed0d1ab
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions pkg/training/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ 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
Expand All @@ -34,10 +36,13 @@ func AcceptJobLog(jobName string, trainingType types.TrainingJobType, args *type
if err != nil {
return err
}
chiefPod := job.ChiefPod()
// 3.if instance name not set,set the chief pod name to instance name
if args.InstanceName == "" && chiefPod != nil {
args.InstanceName = chiefPod.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() {
Expand Down Expand Up @@ -72,3 +77,31 @@ func getTrainingJobTypes() []string {
}
return jobTypes
}

func getInstanceName(job TrainingJob) (string, error) {
pods := job.AllPods()
// if not found pods,return an error
if pods == nil || 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 := fmt.Sprintf("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)
}

0 comments on commit ed0d1ab

Please sign in to comment.