From 60b4c07172cfcafe329cfc5cd993e6c0c8d14197 Mon Sep 17 00:00:00 2001 From: Rajadeepan Date: Mon, 6 May 2019 17:46:25 +0530 Subject: [PATCH] Adding subcommand queue create and list to vkctl --- cmd/cli/queue.go | 52 ++++++++++++++++++++++++ cmd/cli/vkctl.go | 1 + pkg/cli/queue/common.go | 40 +++++++++++++++++++ pkg/cli/queue/create.go | 68 ++++++++++++++++++++++++++++++++ pkg/cli/queue/list.go | 87 +++++++++++++++++++++++++++++++++++++++++ pkg/cli/queue/util.go | 35 +++++++++++++++++ 6 files changed, 283 insertions(+) create mode 100644 cmd/cli/queue.go create mode 100644 pkg/cli/queue/common.go create mode 100644 pkg/cli/queue/create.go create mode 100644 pkg/cli/queue/list.go create mode 100644 pkg/cli/queue/util.go diff --git a/cmd/cli/queue.go b/cmd/cli/queue.go new file mode 100644 index 0000000000..c4b3795f00 --- /dev/null +++ b/cmd/cli/queue.go @@ -0,0 +1,52 @@ +/* +Copyright 2019 The Volcano 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 main + +import ( + "github.com/spf13/cobra" + + "volcano.sh/volcano/pkg/cli/queue" +) + +func buildQueueCmd() *cobra.Command { + jobCmd := &cobra.Command{ + Use: "queue", + Short: "Queue Operations", + } + + jobRunCmd := &cobra.Command{ + Use: "create", + Short: "creates queue", + Run: func(cmd *cobra.Command, args []string) { + checkError(cmd, queue.CreateQueue()) + }, + } + queue.InitRunFlags(jobRunCmd) + jobCmd.AddCommand(jobRunCmd) + + queueListCmd := &cobra.Command{ + Use: "list", + Short: "lists all the queue", + Run: func(cmd *cobra.Command, args []string) { + checkError(cmd, queue.ListQueue()) + }, + } + queue.InitListFlags(queueListCmd) + jobCmd.AddCommand(queueListCmd) + + return jobCmd +} diff --git a/cmd/cli/vkctl.go b/cmd/cli/vkctl.go index b86ca88c34..52ae5fc5f9 100644 --- a/cmd/cli/vkctl.go +++ b/cmd/cli/vkctl.go @@ -40,6 +40,7 @@ func main() { } rootCmd.AddCommand(buildJobCmd()) + rootCmd.AddCommand(buildQueueCmd()) if err := rootCmd.Execute(); err != nil { fmt.Printf("Failed to execute command: %v", err) diff --git a/pkg/cli/queue/common.go b/pkg/cli/queue/common.go new file mode 100644 index 0000000000..304a7e3d69 --- /dev/null +++ b/pkg/cli/queue/common.go @@ -0,0 +1,40 @@ +/* +Copyright 2019 The Volcano 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 queue + +import ( + "path/filepath" + + "github.com/spf13/cobra" +) + +type commonFlags struct { + Master string + Kubeconfig string + SchedulerName string +} + +func initFlags(cmd *cobra.Command, cf *commonFlags) { + cmd.Flags().StringVarP(&cf.SchedulerName, "scheduler", "", "kube-batch", "the scheduler for this job") + cmd.Flags().StringVarP(&cf.Master, "master", "s", "", "the address of apiserver") + + if home := homeDir(); home != "" { + cmd.Flags().StringVarP(&cf.Kubeconfig, "kubeconfig", "", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") + } else { + cmd.Flags().StringVarP(&cf.Kubeconfig, "kubeconfig", "", "", "(optional) absolute path to the kubeconfig file") + } +} diff --git a/pkg/cli/queue/create.go b/pkg/cli/queue/create.go new file mode 100644 index 0000000000..b830cbad54 --- /dev/null +++ b/pkg/cli/queue/create.go @@ -0,0 +1,68 @@ +/* +Copyright 2019 The Volcano 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 queue + +import ( + "github.com/spf13/cobra" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + vkapi "github.com/kubernetes-sigs/kube-batch/pkg/apis/scheduling/v1alpha1" + "github.com/kubernetes-sigs/kube-batch/pkg/client/clientset/versioned" +) + +type createFlags struct { + commonFlags + + Name string + Weight int32 +} + +var createQueueFlags = &createFlags{} + +// InitRunFlags is used to init all run flags +func InitRunFlags(cmd *cobra.Command) { + initFlags(cmd, &createQueueFlags.commonFlags) + + cmd.Flags().StringVarP(&createQueueFlags.Name, "name", "n", "test", "the name of queue") + cmd.Flags().Int32VarP(&createQueueFlags.Weight, "weight", "w", 1, "the weight of the queue") + +} + +// CreateQueue creates queue +func CreateQueue() error { + config, err := buildConfig(createQueueFlags.Master, createQueueFlags.Kubeconfig) + if err != nil { + return err + } + + queue := &vkapi.Queue{ + ObjectMeta: metav1.ObjectMeta{ + Name: createQueueFlags.Name, + }, + Spec: vkapi.QueueSpec{ + Weight: int32(createQueueFlags.Weight), + }, + } + + queueClient := versioned.NewForConfigOrDie(config) + if _, err := queueClient.SchedulingV1alpha1().Queues().Create(queue); err != nil { + return err + } + + return nil +} diff --git a/pkg/cli/queue/list.go b/pkg/cli/queue/list.go new file mode 100644 index 0000000000..35e5a59346 --- /dev/null +++ b/pkg/cli/queue/list.go @@ -0,0 +1,87 @@ +/* +Copyright 2019 The Volcano 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 queue + +import ( + "fmt" + "io" + "os" + + "github.com/spf13/cobra" + + "github.com/kubernetes-sigs/kube-batch/pkg/apis/scheduling/v1alpha1" + "github.com/kubernetes-sigs/kube-batch/pkg/client/clientset/versioned" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +type listFlags struct { + commonFlags +} + +const ( + // Weight of the queue + Weight string = "Weight" + + // Name of queue + Name string = "Name" +) + +var listQueueFlags = &listFlags{} + +// InitListFlags inits all flags +func InitListFlags(cmd *cobra.Command) { + initFlags(cmd, &listQueueFlags.commonFlags) +} + +// ListQueue lists all the queue +func ListQueue() error { + config, err := buildConfig(listQueueFlags.Master, listQueueFlags.Kubeconfig) + if err != nil { + return err + } + + jobClient := versioned.NewForConfigOrDie(config) + queues, err := jobClient.SchedulingV1alpha1().Queues().List(metav1.ListOptions{}) + if err != nil { + return err + } + + if len(queues.Items) == 0 { + fmt.Printf("No resources found\n") + return nil + } + PrintQueues(queues, os.Stdout) + + return nil +} + +// PrintQueues prints queue information +func PrintQueues(queues *v1alpha1.QueueList, writer io.Writer) { + _, err := fmt.Fprintf(writer, "%-25s%-8s\n", + Name, Weight) + if err != nil { + fmt.Printf("Failed to print queue command result: %s.\n", err) + } + for _, queue := range queues.Items { + _, err = fmt.Fprintf(writer, "%-25s%-8d\n", + queue.Name, queue.Spec.Weight) + if err != nil { + fmt.Printf("Failed to print queue command result: %s.\n", err) + } + } + +} diff --git a/pkg/cli/queue/util.go b/pkg/cli/queue/util.go new file mode 100644 index 0000000000..5081c3fb95 --- /dev/null +++ b/pkg/cli/queue/util.go @@ -0,0 +1,35 @@ +/* +Copyright 2019 The Volcano 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 queue + +import ( + "os" + + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" +) + +func homeDir() string { + if h := os.Getenv("HOME"); h != "" { + return h + } + return os.Getenv("USERPROFILE") // windows +} + +func buildConfig(master, kubeconfig string) (*rest.Config, error) { + return clientcmd.BuildConfigFromFlags(master, kubeconfig) +}