Skip to content

Commit

Permalink
refactor vcli
Browse files Browse the repository at this point in the history
Signed-off-by: googs1025 <googs1025@gmail.com>
  • Loading branch information
googs1025 committed May 26, 2024
1 parent 70a483b commit 964c6cf
Show file tree
Hide file tree
Showing 44 changed files with 211 additions and 297 deletions.
102 changes: 51 additions & 51 deletions cmd/cli/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"github.com/spf13/cobra"

"volcano.sh/volcano/cmd/cli/util"
"volcano.sh/volcano/pkg/cli/job"
)

Expand All @@ -12,65 +13,64 @@ func buildJobCmd() *cobra.Command {
Short: "vcctl command line operation job",
}

jobRunCmd := &cobra.Command{
Use: "run",
Short: "run job by parameters from the command line",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.RunJob())
jobCommandMap := map[string]struct {
Short string
RunFunction func(cmd *cobra.Command, args []string)
InitFlags func(cmd *cobra.Command)
}{
"run": {
Short: "run job by parameters from the command line",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, job.RunJob(cmd.Context()))
},
InitFlags: job.InitRunFlags,
},
}
job.InitRunFlags(jobRunCmd)
jobCmd.AddCommand(jobRunCmd)

jobListCmd := &cobra.Command{
Use: "list",
Short: "list job information",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.ListJobs())
"list": {
Short: "list job information",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, job.ListJobs(cmd.Context()))
},
InitFlags: job.InitListFlags,
},
}
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())
"view": {
Short: "show job information",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, job.ViewJob(cmd.Context()))
},
InitFlags: job.InitViewFlags,
},
}
job.InitViewFlags(jobViewCmd)
jobCmd.AddCommand(jobViewCmd)

jobSuspendCmd := &cobra.Command{
Use: "suspend",
Short: "abort a job",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.SuspendJob())
"suspend": {
Short: "abort a job",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, job.SuspendJob(cmd.Context()))
},
InitFlags: job.InitSuspendFlags,
},
}
job.InitSuspendFlags(jobSuspendCmd)
jobCmd.AddCommand(jobSuspendCmd)

jobResumeCmd := &cobra.Command{
Use: "resume",
Short: "resume a job",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.ResumeJob())
"resume": {
Short: "resume a job",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, job.ResumeJob(cmd.Context()))
},
InitFlags: job.InitResumeFlags,
},
"delete": {
Short: "delete a job",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, job.DeleteJob(cmd.Context()))
},
InitFlags: job.InitDeleteFlags,
},
}
job.InitResumeFlags(jobResumeCmd)
jobCmd.AddCommand(jobResumeCmd)

jobDelCmd := &cobra.Command{
Use: "delete",
Short: "delete a job",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.DeleteJob())
},
for command, config := range jobCommandMap {
cmd := &cobra.Command{
Use: command,
Short: config.Short,
Run: config.RunFunction,
}
config.InitFlags(cmd)
jobCmd.AddCommand(cmd)
}
job.InitDeleteFlags(jobDelCmd)
jobCmd.AddCommand(jobDelCmd)

return jobCmd
}
93 changes: 51 additions & 42 deletions cmd/cli/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"github.com/spf13/cobra"

"volcano.sh/volcano/cmd/cli/util"
"volcano.sh/volcano/pkg/cli/queue"
)

Expand All @@ -28,55 +29,63 @@ func buildQueueCmd() *cobra.Command {
Short: "Queue Operations",
}

queueCreateCmd := &cobra.Command{
Use: "create",
Short: "creates queue",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, queue.CreateQueue())
commands := []struct {
Use string
Short string
RunFunction func(cmd *cobra.Command, args []string)
InitFlags func(cmd *cobra.Command)
}{
{
Use: "create",
Short: "creates queue",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, queue.CreateQueue(cmd.Context()))
},
InitFlags: queue.InitCreateFlags,
},
}
queue.InitCreateFlags(queueCreateCmd)
queueCmd.AddCommand(queueCreateCmd)

queueDeleteCmd := &cobra.Command{
Use: "delete",
Short: "delete queue",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, queue.DeleteQueue())
{
Use: "delete",
Short: "delete queue",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, queue.DeleteQueue(cmd.Context()))
},
InitFlags: queue.InitDeleteFlags,
},
}
queue.InitDeleteFlags(queueDeleteCmd)
queueCmd.AddCommand(queueDeleteCmd)

queueOperateCmd := &cobra.Command{
Use: "operate queue",
Short: "operate queue",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, queue.OperateQueue())
{
Use: "operate",
Short: "operate queue",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, queue.OperateQueue(cmd.Context()))
},
InitFlags: queue.InitOperateFlags,
},
}
queue.InitOperateFlags(queueOperateCmd)
queueCmd.AddCommand(queueOperateCmd)

queueListCmd := &cobra.Command{
Use: "list",
Short: "lists all the queue",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, queue.ListQueue())
{
Use: "list",
Short: "lists all the queue",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, queue.ListQueue(cmd.Context()))
},
InitFlags: queue.InitListFlags,
},
{
Use: "get",
Short: "get a queue",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, queue.GetQueue(cmd.Context()))
},
InitFlags: queue.InitGetFlags,
},
}
queue.InitListFlags(queueListCmd)
queueCmd.AddCommand(queueListCmd)

queueGetCmd := &cobra.Command{
Use: "get",
Short: "get a queue",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, queue.GetQueue())
},
for _, command := range commands {
cmd := &cobra.Command{
Use: command.Use,
Short: command.Short,
Run: command.RunFunction,
}
command.InitFlags(cmd)
queueCmd.AddCommand(cmd)
}
queue.InitGetFlags(queueGetCmd)
queueCmd.AddCommand(queueGetCmd)

return queueCmd
}
2 changes: 1 addition & 1 deletion cmd/cli/vcancel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
Short: "cancel a job",
Long: `cancel a running, pending, or aborted job with specified name in default or specified namespace`,
Run: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, vcancel.CancelJob())
util.CheckError(cmd, vcancel.CancelJob(cmd.Context()))
},
}

Expand Down
15 changes: 0 additions & 15 deletions cmd/cli/vcctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
package main

import (
"fmt"
"os"

"github.com/spf13/cobra"
Expand All @@ -42,20 +41,6 @@ func main() {
os.Exit(code)
}

func checkError(cmd *cobra.Command, err error) {
if err != nil {
msg := "Failed to"

// Ignore the root command.
for cur := cmd; cur.Parent() != nil; cur = cur.Parent() {
msg += fmt.Sprintf(" %s", cur.Name())
}

fmt.Printf("%s: %v\n", msg, err)
os.Exit(2)
}
}

var versionExample = `vcctl version`

func versionCommand() *cobra.Command {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/vjobs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
Short: "view job information",
Long: `view information of a job with specified name or jobs from the same namespace`,
Run: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, vjobs.ViewJob())
util.CheckError(cmd, vjobs.ViewJob(cmd.Context()))
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/vqueues/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
Short: "view queue information",
Long: `view information of a queue with specified name or queues from the same namespace`,
Run: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, vqueues.GetQueue())
util.CheckError(cmd, vqueues.GetQueue(cmd.Context()))
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/vresume/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
Short: "resume a job",
Long: `resume an aborted job with specified name in default or specified namespace`,
Run: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, vresume.ResumeJob())
util.CheckError(cmd, vresume.ResumeJob(cmd.Context()))
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/vsub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
Short: "submit a job",
Long: `submit a job with specified name and arguments. yaml/json file is not accepted, you may refer to kubectl.`,
Run: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, vsub.RunJob())
util.CheckError(cmd, vsub.RunJob(cmd.Context()))
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/vsuspend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
Short: "suspend a job",
Long: `suspend a running or pending job with specified name in default or specified namespace`,
Run: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, vsuspend.SuspendJob())
util.CheckError(cmd, vsuspend.SuspendJob(cmd.Context()))
},
}

Expand Down
41 changes: 0 additions & 41 deletions pkg/cli/job/common.go

This file was deleted.

9 changes: 4 additions & 5 deletions pkg/cli/job/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

type deleteFlags struct {
commonFlags
util.CommonFlags

Namespace string
JobName string
Expand All @@ -39,14 +39,13 @@ var deleteJobFlags = &deleteFlags{}

// InitDeleteFlags init the delete command flags.
func InitDeleteFlags(cmd *cobra.Command) {
initFlags(cmd, &deleteJobFlags.commonFlags)

util.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")
}

// DeleteJob delete the job.
func DeleteJob() error {
func DeleteJob(ctx context.Context) error {
config, err := util.BuildConfig(deleteJobFlags.Master, deleteJobFlags.Kubeconfig)
if err != nil {
return err
Expand All @@ -58,7 +57,7 @@ func DeleteJob() error {
}

jobClient := versioned.NewForConfigOrDie(config)
err = jobClient.BatchV1alpha1().Jobs(deleteJobFlags.Namespace).Delete(context.TODO(), deleteJobFlags.JobName, metav1.DeleteOptions{})
err = jobClient.BatchV1alpha1().Jobs(deleteJobFlags.Namespace).Delete(ctx, deleteJobFlags.JobName, metav1.DeleteOptions{})
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 964c6cf

Please sign in to comment.