forked from volcano-sh/volcano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operate.go
99 lines (78 loc) · 2.83 KB
/
operate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
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"
schedulingv1alpha2 "volcano.sh/volcano/pkg/apis/scheduling/v1alpha2"
"volcano.sh/volcano/pkg/client/clientset/versioned"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
)
const (
// ActionOpen is `open` action
ActionOpen = "open"
// ActionClose is `close` action
ActionClose = "close"
// ActionUpdate is `update` action
ActionUpdate = "update"
)
type operateFlags struct {
commonFlags
// Name is name of queue
Name string
// Weight is weight of queue
Weight int32
// Action is operation action of queue
Action string
}
var operateQueueFlags = &operateFlags{}
// InitOperateFlags is used to init all flags during queue operating
func InitOperateFlags(cmd *cobra.Command) {
initFlags(cmd, &operateQueueFlags.commonFlags)
cmd.Flags().StringVarP(&operateQueueFlags.Name, "name", "n", "", "the name of queue")
cmd.Flags().Int32VarP(&operateQueueFlags.Weight, "weight", "w", 0, "the weight of the queue")
cmd.Flags().StringVarP(&operateQueueFlags.Action, "action", "a", "",
"operate action to queue, valid actions are open, close, update")
}
// OperateQueue operates queue
func OperateQueue() error {
config, err := buildConfig(operateQueueFlags.Master, operateQueueFlags.Kubeconfig)
if err != nil {
return err
}
if len(operateQueueFlags.Name) == 0 {
return fmt.Errorf("Queue name must be specified")
}
var action schedulingv1alpha2.QueueAction
switch operateQueueFlags.Action {
case ActionOpen:
action = schedulingv1alpha2.OpenQueueAction
case ActionClose:
action = schedulingv1alpha2.CloseQueueAction
case ActionUpdate:
if operateQueueFlags.Weight == 0 {
return fmt.Errorf("When %s queue %s, weight must be specified, "+
"the value must be greater than 0", ActionUpdate, operateQueueFlags.Name)
}
queueClient := versioned.NewForConfigOrDie(config)
patchBytes := []byte(fmt.Sprintf(`{"spec":{"weight":%d}}`, operateQueueFlags.Weight))
_, err := queueClient.SchedulingV1alpha2().Queues().Patch(operateQueueFlags.Name, types.MergePatchType, patchBytes)
return err
case "":
return fmt.Errorf("Action can not be null")
default:
return fmt.Errorf("Action %s invalid, valid actions are %s, %s and %s",
operateQueueFlags.Action, ActionOpen, ActionClose, ActionUpdate)
}
return createQueueCommand(config, action)
}