Skip to content

Commit

Permalink
Merge branch 'master' into feature/admission_job
Browse files Browse the repository at this point in the history
  • Loading branch information
k82cn authored May 20, 2019
2 parents 3ae3a29 + 4839f14 commit ac82e79
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 20 deletions.
10 changes: 10 additions & 0 deletions cmd/cli/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ func buildJobCmd() *cobra.Command {
job.InitListFlags(jobListCmd)
jobCmd.AddCommand(jobListCmd)

jobViewCmd := &cobra.Command{
Use: "view",
Short: "show job information",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.ViewJob())
},
}
job.InitViewFlags(jobViewCmd)
jobCmd.AddCommand(jobViewCmd)

jobSuspendCmd := &cobra.Command{
Use: "suspend",
Short: "abort a job",
Expand Down
6 changes: 2 additions & 4 deletions pkg/cli/job/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ import (
)

type commonFlags struct {
Master string
Kubeconfig string
SchedulerName string
Master string
Kubeconfig string
}

func initFlags(cmd *cobra.Command, cf *commonFlags) {
cmd.Flags().StringVarP(&cf.SchedulerName, "scheduler", "S", "kube-batch", "the scheduler for this job")
cmd.Flags().StringVarP(&cf.Master, "master", "s", "", "the address of apiserver")

if home := homeDir(); home != "" {
Expand Down
32 changes: 20 additions & 12 deletions pkg/cli/job/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,25 @@ import (
type listFlags struct {
commonFlags

Namespace string
Namespace string
SchedulerName string
}

const (
Name string = "Name"
Creation string = "Creation"
Phase string = "Phase"
Replicas string = "Replicas"
Min string = "Min"
Pending string = "Pending"
Running string = "Running"
Succeeded string = "Succeeded"
Failed string = "Failed"
RetryCount string = "RetryCount"
JobType string = "JobType"
Name string = "Name"
Creation string = "Creation"
Phase string = "Phase"
Replicas string = "Replicas"
Min string = "Min"
Scheduler string = "Scheduler"
Pending string = "Pending"
Running string = "Running"
Succeeded string = "Succeeded"
Terminating string = "Terminating"
Version string = "Version"
Failed string = "Failed"
RetryCount string = "RetryCount"
JobType string = "JobType"
)

var listJobFlags = &listFlags{}
Expand All @@ -54,6 +58,7 @@ func InitListFlags(cmd *cobra.Command) {
initFlags(cmd, &listJobFlags.commonFlags)

cmd.Flags().StringVarP(&listJobFlags.Namespace, "namespace", "N", "default", "the namespace of job")
cmd.Flags().StringVarP(&listJobFlags.SchedulerName, "scheduler", "S", "", "list job with specified scheduler name")
}

func ListJobs() error {
Expand Down Expand Up @@ -86,6 +91,9 @@ func PrintJobs(jobs *v1alpha1.JobList, writer io.Writer) {
}

for _, job := range jobs.Items {
if listJobFlags.SchedulerName != "" && listJobFlags.SchedulerName != job.Spec.SchedulerName {
continue
}
replicas := int32(0)
for _, ts := range job.Spec.Tasks {
replicas += ts.Replicas
Expand Down
10 changes: 6 additions & 4 deletions pkg/cli/job/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ type runFlags struct {
Namespace string
Image string

MinAvailable int
Replicas int
Requests string
Limits string
MinAvailable int
Replicas int
Requests string
Limits string
SchedulerName string
}

var launchJobFlags = &runFlags{}
Expand All @@ -50,6 +51,7 @@ func InitRunFlags(cmd *cobra.Command) {
cmd.Flags().IntVarP(&launchJobFlags.Replicas, "replicas", "r", 1, "the total tasks of job")
cmd.Flags().StringVarP(&launchJobFlags.Requests, "requests", "R", "cpu=1000m,memory=100Mi", "the resource request of the task")
cmd.Flags().StringVarP(&launchJobFlags.Limits, "limits", "L", "cpu=1000m,memory=100Mi", "the resource limit of the task")
cmd.Flags().StringVarP(&listJobFlags.SchedulerName, "scheduler", "S", "kube-batch", "the scheduler for this job")
}

var jobName = "job.volcano.sh"
Expand Down
82 changes: 82 additions & 0 deletions pkg/cli/job/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package job

import (
"fmt"
"io"
"os"
"strings"

"github.com/spf13/cobra"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"volcano.sh/volcano/pkg/apis/batch/v1alpha1"
"volcano.sh/volcano/pkg/client/clientset/versioned"
)

type viewFlags struct {
commonFlags

Namespace string
JobName string
}

var viewJobFlags = &viewFlags{}

func InitViewFlags(cmd *cobra.Command) {
initFlags(cmd, &viewJobFlags.commonFlags)

cmd.Flags().StringVarP(&viewJobFlags.Namespace, "namespace", "N", "default", "the namespace of job")
cmd.Flags().StringVarP(&viewJobFlags.JobName, "name", "n", "", "the name of job")
}

func ViewJob() error {
config, err := buildConfig(viewJobFlags.Master, viewJobFlags.Kubeconfig)
if err != nil {
return err
}
if viewJobFlags.JobName == "" {
err := fmt.Errorf("job name (specified by --name or -n) is mandaorty to view a particular job")
return err
}

jobClient := versioned.NewForConfigOrDie(config)
job, err := jobClient.BatchV1alpha1().Jobs(viewJobFlags.Namespace).Get(viewJobFlags.JobName, metav1.GetOptions{})
if err != nil {
return err
}
if job == nil {
fmt.Printf("No resources found\n")
return nil
}
PrintJob(job, os.Stdout)

return nil
}

func PrintJob(job *v1alpha1.Job, writer io.Writer) {
replicas := int32(0)
for _, ts := range job.Spec.Tasks {
replicas += ts.Replicas
}
lines := []string{
fmt.Sprintf("%s:\t\t%s", Name, job.Name),
fmt.Sprintf("%s:\t%s", Creation, job.CreationTimestamp.Format("2006-01-02 15:04:05")),
fmt.Sprintf("%s:\t%d", Replicas, replicas),
fmt.Sprintf("%s:\t\t%d", Min, job.Status.MinAvailable),
fmt.Sprintf("%s:\t%s", Scheduler, job.Spec.SchedulerName),
"Status",
fmt.Sprintf(" %s:\t%s", Phase, job.Status.State.Phase),
fmt.Sprintf(" %s:\t%d", Version, job.Status.Version),
fmt.Sprintf(" %s:\t%d", RetryCount, job.Status.RetryCount),
fmt.Sprintf(" %s:\t%d", Pending, job.Status.Pending),
fmt.Sprintf(" %s:\t%d", Running, job.Status.Running),
fmt.Sprintf(" %s:\t%d", Succeeded, job.Status.Succeeded),
fmt.Sprintf(" %s:\t%d", Failed, job.Status.Failed),
fmt.Sprintf(" %s:\t%d", Terminating, job.Status.Terminating),
}
_, err := fmt.Fprint(writer, strings.Join(lines, "\n"), "\n")
if err != nil {
fmt.Printf("Failed to print view command result: %s.\n", err)
}
}

0 comments on commit ac82e79

Please sign in to comment.