Skip to content

Commit

Permalink
Merge pull request volcano-sh#9 from k82cn/cli_s_r
Browse files Browse the repository at this point in the history
  • Loading branch information
Klaus Ma committed Jan 22, 2019
2 parents 2eb6c11 + 6fcbd52 commit b80f220
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 59 deletions.
51 changes: 51 additions & 0 deletions cmd/cli/job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"github.com/spf13/cobra"

"hpw.cloud/volcano/pkg/cli/job"
)

func buildJobCmd() *cobra.Command {
jobCmd := &cobra.Command{
Use: "job",
}

jobRunCmd := &cobra.Command{
Use: "run",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.RunJob())
},
}
job.InitRunFlags(jobRunCmd)
jobCmd.AddCommand(jobRunCmd)

jobListCmd := &cobra.Command{
Use: "list",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.ListJobs())
},
}
job.InitListFlags(jobListCmd)
jobCmd.AddCommand(jobListCmd)

jobSuspendCmd := &cobra.Command{
Use: "suspend",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.SuspendJob())
},
}
job.InitSuspendFlags(jobSuspendCmd)
jobCmd.AddCommand(jobSuspendCmd)

jobResumeCmd := &cobra.Command{
Use: "resume",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.ResumeJob())
},
}
job.InitResumeFlags(jobResumeCmd)
jobCmd.AddCommand(jobResumeCmd)

return jobCmd
}
28 changes: 2 additions & 26 deletions cmd/cli/vkctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"github.com/spf13/pflag"

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

"hpw.cloud/volcano/pkg/cli/job"
)

var logFlushFreq = pflag.Duration("log-flush-frequency", 5*time.Second, "Maximum number of seconds between log flushes")
Expand All @@ -38,32 +36,10 @@ func main() {
defer glog.Flush()

rootCmd := cobra.Command{
Use: "vncli",
}

jobCmd := &cobra.Command{
Use: "job",
}

jobRunCmd := &cobra.Command{
Use: "run",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.RunJob())
},
}
job.InitRunFlags(jobRunCmd)
jobCmd.AddCommand(jobRunCmd)

jobListCmd := &cobra.Command{
Use: "list",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.ListJobs())
},
Use: "vkctl",
}
job.InitListFlags(jobListCmd)
jobCmd.AddCommand(jobListCmd)

rootCmd.AddCommand(jobCmd)
rootCmd.AddCommand(buildJobCmd())

if err := rootCmd.Execute(); err != nil {
fmt.Printf("Failed to execute command: %v", err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/cli/job/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ func ListJobs() error {
return nil
}

fmt.Printf("%-30s%-25s%-12s%-8s%-12s%-12s%-12s%-12s\n",
"Name", "Creation", "Replicas", "Min", "Pending", "Running", "Succeeded", "Failed")
fmt.Printf("%-25s%-25s%-12s%-12s%-6s%-10s%-10s%-12s%-10s\n",
"Name", "Creation", "Phase", "Replicas", "Min", "Pending", "Running", "Succeeded", "Failed")
for _, job := range jobs.Items {
replicas := int32(0)
for _, ts := range job.Spec.Tasks {
replicas += ts.Replicas
}

fmt.Printf("%-30s%-25s%-12d%-8d%-12d%-12d%-12d%-12d\n",
job.Name, job.CreationTimestamp.Format("2006-01-02 15:04:05"), replicas,
fmt.Printf("%-25s%-25s%-12s%-12d%-6d%-10d%-10d%-12d%-10d\n",
job.Name, job.CreationTimestamp.Format("2006-01-02 15:04:05"), job.Status.State.Phase, replicas,
job.Status.MinAvailable, job.Status.Pending, job.Status.Running, job.Status.Succeeded, job.Status.Failed)
}

Expand Down
49 changes: 49 additions & 0 deletions pkg/cli/job/resume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2018 The Vulcan Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package job

import (
"github.com/spf13/cobra"

"hpw.cloud/volcano/pkg/apis/batch/v1alpha1"
)

type resumeFlags struct {
commonFlags

Namespace string
JobName string
}

var resumeJobFlags = &resumeFlags{}

func InitResumeFlags(cmd *cobra.Command) {
initFlags(cmd, &resumeJobFlags.commonFlags)

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

func ResumeJob() error {
config, err := buildConfig(resumeJobFlags.Master, resumeJobFlags.Kubeconfig)
if err != nil {
return err
}

return createJobCommand(config,
resumeJobFlags.Namespace, resumeJobFlags.JobName,
v1alpha1.ResumeJobAction)
}
49 changes: 49 additions & 0 deletions pkg/cli/job/suspend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2018 The Vulcan Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package job

import (
"github.com/spf13/cobra"

"hpw.cloud/volcano/pkg/apis/batch/v1alpha1"
)

type suspendFlags struct {
commonFlags

Namespace string
JobName string
}

var suspendJobFlags = &suspendFlags{}

func InitSuspendFlags(cmd *cobra.Command) {
initFlags(cmd, &suspendJobFlags.commonFlags)

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

func SuspendJob() error {
config, err := buildConfig(suspendJobFlags.Master, suspendJobFlags.Kubeconfig)
if err != nil {
return err
}

return createJobCommand(config,
suspendJobFlags.Namespace, suspendJobFlags.JobName,
v1alpha1.AbortJobAction)
}
35 changes: 35 additions & 0 deletions pkg/cli/job/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ import (
"os"
"strings"

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

"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

vkbatchv1 "hpw.cloud/volcano/pkg/apis/batch/v1alpha1"
vkbusv1 "hpw.cloud/volcano/pkg/apis/bus/v1alpha1"
"hpw.cloud/volcano/pkg/apis/helpers"
"hpw.cloud/volcano/pkg/client/clientset/versioned"
)

func homeDir() string {
Expand Down Expand Up @@ -61,3 +68,31 @@ func populateResourceListV1(spec string) (v1.ResourceList, error) {
}
return result, nil
}

func createJobCommand(config *rest.Config, ns, name string, action vkbatchv1.Action) error {
jobClient := versioned.NewForConfigOrDie(config)
job, err := jobClient.BatchV1alpha1().Jobs(ns).Get(name, metav1.GetOptions{})
if err != nil {
return err
}

ctrlRef := metav1.NewControllerRef(job, helpers.JobKind)
cmd := &vkbusv1.Command{
ObjectMeta: metav1.ObjectMeta{
GenerateName: fmt.Sprintf("%s-%s-",
job.Name, strings.ToLower(string(action))),
Namespace: job.Namespace,
OwnerReferences: []metav1.OwnerReference{
*ctrlRef,
},
},
TargetObject: ctrlRef,
Action: string(action),
}

if _, err := jobClient.BusV1alpha1().Commands(ns).Create(cmd); err != nil {
return err
}

return nil
}
8 changes: 5 additions & 3 deletions pkg/controllers/job/job_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package job

import (
"fmt"

"github.com/golang/glog"

"k8s.io/api/core/v1"
Expand Down Expand Up @@ -114,8 +113,8 @@ func NewJobController(config *rest.Config) *Controller {
case *v1corev1.Command:
return helpers.ControlledBy(t, helpers.JobKind)
case cache.DeletedFinalStateUnknown:
if pod, ok := t.Obj.(*v1corev1.Command); ok {
return helpers.ControlledBy(pod, helpers.JobKind)
if cmd, ok := t.Obj.(*v1corev1.Command); ok {
return helpers.ControlledBy(cmd, helpers.JobKind)
}
runtime.HandleError(fmt.Errorf("unable to convert object %T to *v1.Pod", obj))
return false
Expand Down Expand Up @@ -237,6 +236,9 @@ func (cc *Controller) worker() {
action = applyPolicies(req.Event, job, pod)
}

glog.V(3).Infof("Execute <%v> on Job <%s/%s> in <%s> by <%T>.",
action, req.Namespace, req.JobName, job.Status.State.Phase, st)

if err := st.Execute(action, req.Reason, req.Message); err != nil {
glog.Errorf("Failed to handle Job <%s/%s>: %v",
job.Namespace, job.Name, err)
Expand Down
Loading

0 comments on commit b80f220

Please sign in to comment.