diff --git a/cmd/cli/vkctl.go b/cmd/cli/vkctl.go index 2860894dba7..965220bb6d9 100644 --- a/cmd/cli/vkctl.go +++ b/cmd/cli/vkctl.go @@ -24,6 +24,7 @@ import ( "github.com/spf13/pflag" "k8s.io/apimachinery/pkg/util/wait" + "volcano.sh/volcano/pkg/version" ) diff --git a/pkg/cli/job/delete.go b/pkg/cli/job/delete.go index 6662a2c0fa5..7b6eb3be388 100644 --- a/pkg/cli/job/delete.go +++ b/pkg/cli/job/delete.go @@ -39,8 +39,8 @@ var deleteJobFlags = &deleteFlags{} func InitDeleteFlags(cmd *cobra.Command) { initFlags(cmd, &deleteJobFlags.commonFlags) - cmd.Flags().StringVarP(&deleteJobFlags.Namespace, "namespace", "N", "default", "the namespace of job") - cmd.Flags().StringVarP(&deleteJobFlags.JobName, "name", "n", "", "the name of job") + cmd.Flags().StringVarP(&deleteJobFlags.Namespace, "namespace", "n", "default", "the namespace of job") + cmd.Flags().StringVarP(&deleteJobFlags.JobName, "name", "N", "", "the name of job") } // DeleteJob delete the job diff --git a/pkg/cli/job/list.go b/pkg/cli/job/list.go index 7a89d6d357a..3ef742f1e91 100644 --- a/pkg/cli/job/list.go +++ b/pkg/cli/job/list.go @@ -21,6 +21,7 @@ import ( "io" "os" "strings" + "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -34,8 +35,8 @@ type listFlags struct { Namespace string SchedulerName string - allNamespace bool - selector string + allNamespace bool + selector string } const ( diff --git a/pkg/cli/job/run.go b/pkg/cli/job/run.go index b472d0b8080..d3f9a57094e 100644 --- a/pkg/cli/job/run.go +++ b/pkg/cli/job/run.go @@ -17,10 +17,15 @@ limitations under the License. package job import ( + "fmt" + "io/ioutil" + "strings" + "github.com/spf13/cobra" "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/yaml" vkapi "volcano.sh/volcano/pkg/apis/batch/v1alpha1" "volcano.sh/volcano/pkg/client/clientset/versioned" @@ -38,6 +43,7 @@ type runFlags struct { Requests string Limits string SchedulerName string + FileName string } var launchJobFlags = &runFlags{} @@ -54,6 +60,7 @@ func InitRunFlags(cmd *cobra.Command) { 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(&launchJobFlags.SchedulerName, "scheduler", "S", "kube-batch", "the scheduler for this job") + cmd.Flags().StringVarP(&launchJobFlags.FileName, "filename", "f", "", "the yaml file of job") } var jobName = "job.volcano.sh" @@ -75,13 +82,56 @@ func RunJob() error { return err } - job := &vkapi.Job{ + job, err := readFile(launchJobFlags.FileName) + if err != nil { + return err + } + + if job == nil { + job = constructLaunchJobFlagsJob(launchJobFlags, req, limit) + } + + jobClient := versioned.NewForConfigOrDie(config) + newJob, err := jobClient.BatchV1alpha1().Jobs(launchJobFlags.Namespace).Create(job) + if err != nil { + return err + } + + fmt.Printf("run job %v successfully\n", newJob.Name) + + return nil +} + +func readFile(filename string) (*vkapi.Job, error) { + if filename == "" { + return nil, nil + } + + if !strings.Contains(filename, ".yaml") && !strings.Contains(filename, ".yml") { + return nil, fmt.Errorf("Only support yaml file.") + } + + file, err := ioutil.ReadFile(filename) + if err != nil { + return nil, fmt.Errorf("Failed to read file, err: %v", err) + } + + var job vkapi.Job + if err := yaml.Unmarshal(file, &job); err != nil { + return nil, fmt.Errorf("Failed to unmarshal file, err: %v", err) + } + + return &job, nil +} + +func constructLaunchJobFlagsJob(launchJobFlags *runFlags, req, limit v1.ResourceList) *vkapi.Job { + return &vkapi.Job{ ObjectMeta: metav1.ObjectMeta{ Name: launchJobFlags.Name, Namespace: launchJobFlags.Namespace, }, Spec: vkapi.JobSpec{ - MinAvailable: int32(launchJobFlags.MinAvailable), + MinAvailable: int32(launchJobFlags.MinAvailable), SchedulerName: launchJobFlags.SchedulerName, Tasks: []vkapi.TaskSpec{ { @@ -111,11 +161,4 @@ func RunJob() error { }, }, } - - jobClient := versioned.NewForConfigOrDie(config) - if _, err := jobClient.BatchV1alpha1().Jobs(launchJobFlags.Namespace).Create(job); err != nil { - return err - } - - return nil } diff --git a/pkg/cli/job/util.go b/pkg/cli/job/util.go index 2654bcd2089..de9542e17c0 100644 --- a/pkg/cli/job/util.go +++ b/pkg/cli/job/util.go @@ -99,7 +99,6 @@ func createJobCommand(config *rest.Config, ns, name string, action vkbatchv1.Act return nil } - func translateTimestampSince(timestamp metav1.Time) string { if timestamp.IsZero() { return "" diff --git a/pkg/cli/job/view.go b/pkg/cli/job/view.go index 2e7a8033c35..75f2a1bc0c1 100644 --- a/pkg/cli/job/view.go +++ b/pkg/cli/job/view.go @@ -17,18 +17,18 @@ limitations under the License. package job import ( + "encoding/json" "fmt" "io" "os" "strings" - "encoding/json" "github.com/spf13/cobra" + coreV1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/rest" "k8s.io/client-go/kubernetes" - coreV1 "k8s.io/api/core/v1" + "k8s.io/client-go/rest" "volcano.sh/volcano/pkg/apis/batch/v1alpha1" "volcano.sh/volcano/pkg/client/clientset/versioned" @@ -64,7 +64,7 @@ func ViewJob() error { return err } if viewJobFlags.JobName == "" { - err := fmt.Errorf("job name (specified by --name or -n) is mandatory to view a particular job") + err := fmt.Errorf("job name (specified by --name or -N) is mandaorty to view a particular job") return err } @@ -115,45 +115,45 @@ func PrintJobInfo(job *v1alpha1.Job, writer io.Writer) { WriteLine(writer, LEVEL_2, "Ssh:\t%v\n", job.Spec.Plugins["ssh"]) WriteLine(writer, LEVEL_1, "Scheduler Name: \t%s\n", job.Spec.SchedulerName) WriteLine(writer, LEVEL_1, "Tasks:\n") - for i := 0; i 0 { WriteLine(writer, LEVEL_1, "Controlled Resources:\n") - for key, value := range job.Status.ControlledResources { + for key, value := range job.Status.ControlledResources { WriteLine(writer, LEVEL_2, "%s: \t%s\n", key, value) } } @@ -207,7 +207,7 @@ func PrintEvents(events []coreV1.Event, writer io.Writer) { if len(e.Source.Host) > 0 { EventSourceString = append(EventSourceString, e.Source.Host) } - WriteLine(writer,LEVEL_0, "%-15v\t%-40v\t%-30s\t%-40s\t%v\n", + WriteLine(writer, LEVEL_0, "%-15v\t%-40v\t%-30s\t%-40s\t%v\n", e.Type, e.Reason, interval, @@ -224,13 +224,13 @@ func PrintEvents(events []coreV1.Event, writer io.Writer) { func GetEvents(config *rest.Config, job *v1alpha1.Job) []coreV1.Event { kubernetes, err := kubernetes.NewForConfig(config) if err != nil { - fmt.Printf("%v\n",err) + fmt.Printf("%v\n", err) return nil } events, _ := kubernetes.CoreV1().Events(viewJobFlags.Namespace).List(metav1.ListOptions{}) - var jobEvents []coreV1.Event + var jobEvents []coreV1.Event for _, v := range events.Items { - if strings.HasPrefix(v.ObjectMeta.Name, job.Name + ".") { + if strings.HasPrefix(v.ObjectMeta.Name, job.Name+".") { jobEvents = append(jobEvents, v) } } @@ -242,6 +242,5 @@ func WriteLine(writer io.Writer, spaces int, content string, params ...interface for i := 0; i < spaces; i++ { prefix += " " } - fmt.Fprintf(writer, prefix + content, params...) + fmt.Fprintf(writer, prefix+content, params...) } -