Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support vkctl job view #188

Merged
merged 2 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 14 additions & 11 deletions pkg/cli/job/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,20 @@ type listFlags struct {
}

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 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)
}
}