Skip to content

Commit

Permalink
command support vkctl job run -f filename.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyuqing4 authored and lminzhw committed Jun 26, 2019
1 parent a3563c4 commit 6fff5cc
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 54 deletions.
1 change: 1 addition & 0 deletions cmd/cli/vkctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/spf13/pflag"

"k8s.io/apimachinery/pkg/util/wait"

"volcano.sh/volcano/pkg/version"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/job/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions pkg/cli/job/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"os"
"strings"

"github.com/spf13/cobra"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -34,8 +35,8 @@ type listFlags struct {

Namespace string
SchedulerName string
allNamespace bool
selector string
allNamespace bool
selector string
}

const (
Expand Down
61 changes: 52 additions & 9 deletions pkg/cli/job/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -38,6 +43,7 @@ type runFlags struct {
Requests string
Limits string
SchedulerName string
FileName string
}

var launchJobFlags = &runFlags{}
Expand All @@ -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"
Expand All @@ -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{
{
Expand Down Expand Up @@ -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
}
1 change: 0 additions & 1 deletion pkg/cli/job/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<unknown>"
Expand Down
79 changes: 39 additions & 40 deletions pkg/cli/job/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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<len(job.Spec.Tasks); i++ {
for i := 0; i < len(job.Spec.Tasks); i++ {
WriteLine(writer, LEVEL_2, "Name:\t%s\n", job.Spec.Tasks[i].Name)
WriteLine(writer, LEVEL_2, "Replicas:\t%d\n", job.Spec.Tasks[i].Replicas)
WriteLine(writer, LEVEL_2, "Template:\n")
WriteLine(writer, LEVEL_2 + 1, "Metadata:\n")
WriteLine(writer, LEVEL_2 + 2, "Annotations:\n")
WriteLine(writer, LEVEL_2 + 3, "Cri . Cci . Io / Container - Type: \t%s\n", job.Spec.Tasks[i].Template.ObjectMeta.Annotations["cri.cci.io/container-type"])
WriteLine(writer, LEVEL_2 + 3, "Kubernetes . Io / Availablezone: \t%s\n", job.Spec.Tasks[i].Template.ObjectMeta.Annotations["kubernetes.io/availablezone"])
WriteLine(writer, LEVEL_2 + 3, "Network . Alpha . Kubernetes . Io / Network:\t%s\n", job.Spec.Tasks[i].Template.ObjectMeta.Annotations["network.alpha.kubernetes.io/network"])
WriteLine(writer, LEVEL_2 + 2, "Creation Timestamp:\t%s\n", job.Spec.Tasks[i].Template.ObjectMeta.CreationTimestamp)

WriteLine(writer, LEVEL_2 + 1, "Spec:\n")
WriteLine(writer, LEVEL_2 + 2, "Containers:\n")
for j := 0; j < len( job.Spec.Tasks[i].Template.Spec.Containers); j++ {
WriteLine(writer, LEVEL_2 + 3, "Command:\n")
WriteLine(writer, LEVEL_2+1, "Metadata:\n")
WriteLine(writer, LEVEL_2+2, "Annotations:\n")
WriteLine(writer, LEVEL_2+3, "Cri . Cci . Io / Container - Type: \t%s\n", job.Spec.Tasks[i].Template.ObjectMeta.Annotations["cri.cci.io/container-type"])
WriteLine(writer, LEVEL_2+3, "Kubernetes . Io / Availablezone: \t%s\n", job.Spec.Tasks[i].Template.ObjectMeta.Annotations["kubernetes.io/availablezone"])
WriteLine(writer, LEVEL_2+3, "Network . Alpha . Kubernetes . Io / Network:\t%s\n", job.Spec.Tasks[i].Template.ObjectMeta.Annotations["network.alpha.kubernetes.io/network"])
WriteLine(writer, LEVEL_2+2, "Creation Timestamp:\t%s\n", job.Spec.Tasks[i].Template.ObjectMeta.CreationTimestamp)

WriteLine(writer, LEVEL_2+1, "Spec:\n")
WriteLine(writer, LEVEL_2+2, "Containers:\n")
for j := 0; j < len(job.Spec.Tasks[i].Template.Spec.Containers); j++ {
WriteLine(writer, LEVEL_2+3, "Command:\n")
for k := 0; k < len(job.Spec.Tasks[i].Template.Spec.Containers[j].Command); k++ {
WriteLine(writer, LEVEL_2 + 4, "%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Command[k])
WriteLine(writer, LEVEL_2+4, "%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Command[k])
}
WriteLine(writer, LEVEL_2 + 3, "Image:\t%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Image)
WriteLine(writer, LEVEL_2 + 3, "Name: \t%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Name)
WriteLine(writer, LEVEL_2 + 3, "Ports:\n")
WriteLine(writer, LEVEL_2+3, "Image:\t%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Image)
WriteLine(writer, LEVEL_2+3, "Name: \t%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Name)
WriteLine(writer, LEVEL_2+3, "Ports:\n")
for k := 0; k < len(job.Spec.Tasks[i].Template.Spec.Containers[j].Ports); k++ {
WriteLine(writer, LEVEL_2 + 4, "Container Port:\t%d\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Ports[k].ContainerPort)
WriteLine(writer, LEVEL_2 + 4, "Name: \t%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Ports[k].Name)
WriteLine(writer, LEVEL_2+4, "Container Port:\t%d\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Ports[k].ContainerPort)
WriteLine(writer, LEVEL_2+4, "Name: \t%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Ports[k].Name)
}
WriteLine(writer, LEVEL_2 + 3, "Resources:\n")
WriteLine(writer, LEVEL_2 + 4, "Limits:\n")
WriteLine(writer, LEVEL_2 + 5, "Cpu: \t%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Resources.Limits.Cpu())
WriteLine(writer, LEVEL_2 + 5, "Memory:\t%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Resources.Limits.Memory())
WriteLine(writer, LEVEL_2 + 4, "Requests:\n")
WriteLine(writer, LEVEL_2 + 5, "Cpu: \t%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Resources.Requests.Cpu())
WriteLine(writer, LEVEL_2 + 5, "Memory:\t%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Resources.Requests.Memory())
WriteLine(writer, LEVEL_2 + 4, "Working Dir:\t%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].WorkingDir)
WriteLine(writer, LEVEL_2+3, "Resources:\n")
WriteLine(writer, LEVEL_2+4, "Limits:\n")
WriteLine(writer, LEVEL_2+5, "Cpu: \t%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Resources.Limits.Cpu())
WriteLine(writer, LEVEL_2+5, "Memory:\t%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Resources.Limits.Memory())
WriteLine(writer, LEVEL_2+4, "Requests:\n")
WriteLine(writer, LEVEL_2+5, "Cpu: \t%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Resources.Requests.Cpu())
WriteLine(writer, LEVEL_2+5, "Memory:\t%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].Resources.Requests.Memory())
WriteLine(writer, LEVEL_2+4, "Working Dir:\t%s\n", job.Spec.Tasks[i].Template.Spec.Containers[j].WorkingDir)
}
WriteLine(writer, LEVEL_2 + 2, "Image Pull Secrets:\n")
WriteLine(writer, LEVEL_2+2, "Image Pull Secrets:\n")
for j := 0; j < len(job.Spec.Tasks[i].Template.Spec.ImagePullSecrets); j++ {
WriteLine(writer, LEVEL_2 + 3, "Name: \t%s\n", job.Spec.Tasks[i].Template.Spec.ImagePullSecrets[j].Name)
WriteLine(writer, LEVEL_2+3, "Name: \t%s\n", job.Spec.Tasks[i].Template.Spec.ImagePullSecrets[j].Name)
}
WriteLine(writer, LEVEL_2 + 2, "Restart Policy: \t%s\n", job.Spec.Tasks[i].Template.Spec.RestartPolicy)
WriteLine(writer, LEVEL_2+2, "Restart Policy: \t%s\n", job.Spec.Tasks[i].Template.Spec.RestartPolicy)
}

WriteLine(writer, LEVEL_0, "Status:\n")
Expand Down Expand Up @@ -186,7 +186,7 @@ func PrintJobInfo(job *v1alpha1.Job, writer io.Writer) {
WriteLine(writer, LEVEL_2, "Phase:\t%s\n", job.Status.State.Phase)
if len(job.Status.ControlledResources) > 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)
}
}
Expand All @@ -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,
Expand All @@ -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)
}
}
Expand All @@ -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...)
}

0 comments on commit 6fff5cc

Please sign in to comment.