Skip to content

Commit

Permalink
Adding subcommand queue create and list to vkctl
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajadeepan committed May 6, 2019
1 parent a69c217 commit 672a165
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 0 deletions.
52 changes: 52 additions & 0 deletions cmd/cli/queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2019 The Kubernetes 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
}
1 change: 1 addition & 0 deletions cmd/cli/vkctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions pkg/cli/queue/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2019 The Kubernetes 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")
}
}
68 changes: 68 additions & 0 deletions pkg/cli/queue/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2019 The Kubernetes 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
}
87 changes: 87 additions & 0 deletions pkg/cli/queue/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Copyright 2019 The Kubernetes 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)
}
}

}
35 changes: 35 additions & 0 deletions pkg/cli/queue/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2019 The Kubernetes 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)
}

0 comments on commit 672a165

Please sign in to comment.