Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving vkctrl queue to Volcano from kube-batch #136

Merged
merged 4 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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
}
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 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")
}
}
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 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
}
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 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)
}
}

}
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 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)
}