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

Support queue action by vcctl #590

Merged
merged 1 commit into from
Dec 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
34 changes: 27 additions & 7 deletions cmd/cli/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,40 @@ import (
)

func buildQueueCmd() *cobra.Command {
jobCmd := &cobra.Command{
queueCmd := &cobra.Command{
Use: "queue",
Short: "Queue Operations",
}

jobRunCmd := &cobra.Command{
queueCreateCmd := &cobra.Command{
Use: "create",
Short: "creates queue",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, queue.CreateQueue())
},
}
queue.InitRunFlags(jobRunCmd)
jobCmd.AddCommand(jobRunCmd)
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())
},
}
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())
},
}
queue.InitOperateFlags(queueOperateCmd)
queueCmd.AddCommand(queueOperateCmd)

queueListCmd := &cobra.Command{
Use: "list",
Expand All @@ -46,7 +66,7 @@ func buildQueueCmd() *cobra.Command {
},
}
queue.InitListFlags(queueListCmd)
jobCmd.AddCommand(queueListCmd)
queueCmd.AddCommand(queueListCmd)

queueGetCmd := &cobra.Command{
Use: "get",
Expand All @@ -56,7 +76,7 @@ func buildQueueCmd() *cobra.Command {
},
}
queue.InitGetFlags(queueGetCmd)
jobCmd.AddCommand(queueGetCmd)
queueCmd.AddCommand(queueGetCmd)

return jobCmd
return queueCmd
}
4 changes: 4 additions & 0 deletions pkg/apis/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (

vcbatch "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
vcbus "volcano.sh/volcano/pkg/apis/bus/v1alpha1"
schedulerv1alpha2 "volcano.sh/volcano/pkg/apis/scheduling/v1alpha2"
)

// JobKind creates job GroupVersionKind
Expand All @@ -48,6 +49,9 @@ var JobKind = vcbatch.SchemeGroupVersion.WithKind("Job")
// CommandKind creates command GroupVersionKind
var CommandKind = vcbus.SchemeGroupVersion.WithKind("Command")

// V1alpha2QueueKind is queue kind with v1alpha2 version
var V1alpha2QueueKind = schedulerv1alpha2.SchemeGroupVersion.WithKind("Queue")

// GetController returns the controller uid
func GetController(obj interface{}) types.UID {
accessor, err := meta.Accessor(obj)
Expand Down
10 changes: 7 additions & 3 deletions pkg/cli/queue/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,23 @@ type createFlags struct {

Name string
Weight int32
// State is state of Queue
State string
}

var createQueueFlags = &createFlags{}

// InitRunFlags is used to init all run flags
func InitRunFlags(cmd *cobra.Command) {
// InitCreateFlags is used to init all flags during queue creating
func InitCreateFlags(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")

cmd.Flags().StringVarP(&createQueueFlags.State, "state", "S", "Open", "the state of queue")
}

// CreateQueue creates queue
// CreateQueue create queue
func CreateQueue() error {
config, err := buildConfig(createQueueFlags.Master, createQueueFlags.Kubeconfig)
if err != nil {
Expand All @@ -56,6 +59,7 @@ func CreateQueue() error {
},
Spec: schedulingV1alpha2.QueueSpec{
Weight: int32(createQueueFlags.Weight),
State: schedulingV1alpha2.QueueState(createQueueFlags.State),
},
}

Expand Down
58 changes: 58 additions & 0 deletions pkg/cli/queue/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2017 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"

"volcano.sh/volcano/pkg/client/clientset/versioned"

"github.com/spf13/cobra"

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

type deleteFlags struct {
commonFlags

// Name is name of queue
Name string
}

var deleteQueueFlags = &deleteFlags{}

// InitDeleteFlags is used to init all flags during queue deleting
func InitDeleteFlags(cmd *cobra.Command) {
initFlags(cmd, &deleteQueueFlags.commonFlags)

cmd.Flags().StringVarP(&deleteQueueFlags.Name, "name", "n", "", "the name of queue")
}

// DeleteQueue delete queue
func DeleteQueue() error {
config, err := buildConfig(deleteQueueFlags.Master, deleteQueueFlags.Kubeconfig)
if err != nil {
return err
}

if len(deleteQueueFlags.Name) == 0 {
return fmt.Errorf("Queue name must be specified")
}

queueClient := versioned.NewForConfigOrDie(config)
return queueClient.SchedulingV1alpha2().Queues().Delete(deleteQueueFlags.Name, &metav1.DeleteOptions{})
}
87 changes: 87 additions & 0 deletions pkg/cli/queue/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Copyright 2017 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 (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"

"volcano.sh/volcano/pkg/apis/scheduling/v1alpha2"

"github.com/spf13/cobra"

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

func TestDeleteQueue(t *testing.T) {
response := v1alpha2.Queue{
ObjectMeta: metav1.ObjectMeta{
Name: "test-queue",
},
}

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
val, err := json.Marshal(response)
if err == nil {
w.Write(val)
}
})

server := httptest.NewServer(handler)
defer server.Close()

deleteQueueFlags.Master = server.URL
testCases := []struct {
Name string
QueueName string
ExpectValue error
}{
{
Name: "Normal Case Delete Queue Succeed",
QueueName: "normal-case",
ExpectValue: nil,
},
{
Name: "Abnormal Case Delete Queue Failed For Name Not Specified",
QueueName: "",
ExpectValue: fmt.Errorf("Queue name must be specified"),
},
}

for _, testCase := range testCases {
deleteQueueFlags.Name = testCase.QueueName

err := DeleteQueue()
if false == reflect.DeepEqual(err, testCase.ExpectValue) {
t.Errorf("Case '%s' failed, expected: '%v', got '%v'", testCase.Name, testCase.ExpectValue, err)
}
}
}

func TestInitDeleteFlags(t *testing.T) {
var cmd cobra.Command
InitDeleteFlags(&cmd)

if cmd.Flag("name") == nil {
t.Errorf("Could not find the flag name")
}
}
9 changes: 5 additions & 4 deletions pkg/cli/queue/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ func GetQueue() error {

// PrintQueue prints queue information
func PrintQueue(queue *v1alpha2.Queue, writer io.Writer) {
_, err := fmt.Fprintf(writer, "%-25s%-8s%-8s%-8s%-8s\n",
Name, Weight, Pending, Running, Unknown)
_, err := fmt.Fprintf(writer, "%-25s%-8s%-8s%-8s%-8s%-8s%-8s\n",
Name, Weight, State, Inqueue, Pending, Running, Unknown)
if err != nil {
fmt.Printf("Failed to print queue command result: %s.\n", err)
}
_, err = fmt.Fprintf(writer, "%-25s%-8d%-8d%-8d%-8d\n",
queue.Name, queue.Spec.Weight, queue.Status.Pending, queue.Status.Running, queue.Status.Unknown)
_, err = fmt.Fprintf(writer, "%-25s%-8d%-8s%-8d%-8d%-8d%-8d\n",
queue.Name, queue.Spec.Weight, queue.Status.State, queue.Status.Inqueue,
queue.Status.Pending, queue.Status.Running, queue.Status.Unknown)
if err != nil {
fmt.Printf("Failed to print queue command result: %s.\n", err)
}
Expand Down
15 changes: 11 additions & 4 deletions pkg/cli/queue/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ const (

// Unknown status of the queue
Unknown string = "Unknown"

// Inqueue status of queue
Inqueue string = "Inqueue"

// State is state of queue
State string = "State"
)

var listQueueFlags = &listFlags{}
Expand Down Expand Up @@ -81,14 +87,15 @@ func ListQueue() error {

// PrintQueues prints queue information
func PrintQueues(queues *v1alpha2.QueueList, writer io.Writer) {
_, err := fmt.Fprintf(writer, "%-25s%-8s%-8s%-8s%-8s\n",
Name, Weight, Pending, Running, Unknown)
_, err := fmt.Fprintf(writer, "%-25s%-8s%-8s%-8s%-8s%-8s%-8s\n",
Name, Weight, State, Inqueue, Pending, Running, Unknown)
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%-8d%-8d%-8d\n",
queue.Name, queue.Spec.Weight, queue.Status.Pending, queue.Status.Running, queue.Status.Unknown)
_, err = fmt.Fprintf(writer, "%-25s%-8d%-8s%-8d%-8d%-8d%-8d\n",
queue.Name, queue.Spec.Weight, queue.Status.State, queue.Status.Inqueue,
queue.Status.Pending, queue.Status.Running, queue.Status.Unknown)
if err != nil {
fmt.Printf("Failed to print queue command result: %s.\n", err)
}
Expand Down
Loading