volcano.sh/volcano@v1.9.0/pkg/cli/queue/operate.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package queue
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/spf13/cobra"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  
    26  	"k8s.io/apimachinery/pkg/types"
    27  
    28  	"volcano.sh/apis/pkg/apis/bus/v1alpha1"
    29  	"volcano.sh/apis/pkg/client/clientset/versioned"
    30  )
    31  
    32  const (
    33  	// ActionOpen is `open` action
    34  	ActionOpen = "open"
    35  	// ActionClose is `close` action
    36  	ActionClose = "close"
    37  	// ActionUpdate is `update` action
    38  	ActionUpdate = "update"
    39  )
    40  
    41  type operateFlags struct {
    42  	commonFlags
    43  
    44  	// Name is name of queue
    45  	Name string
    46  	// Weight is weight of queue
    47  	Weight int32
    48  	// Action is operation action of queue
    49  	Action string
    50  }
    51  
    52  var operateQueueFlags = &operateFlags{}
    53  
    54  // InitOperateFlags is used to init all flags during queue operating
    55  func InitOperateFlags(cmd *cobra.Command) {
    56  	initFlags(cmd, &operateQueueFlags.commonFlags)
    57  
    58  	cmd.Flags().StringVarP(&operateQueueFlags.Name, "name", "n", "", "the name of queue")
    59  	cmd.Flags().Int32VarP(&operateQueueFlags.Weight, "weight", "w", 0, "the weight of the queue")
    60  	cmd.Flags().StringVarP(&operateQueueFlags.Action, "action", "a", "",
    61  		"operate action to queue, valid actions are open, close, update")
    62  }
    63  
    64  // OperateQueue operates queue
    65  func OperateQueue() error {
    66  	config, err := buildConfig(operateQueueFlags.Master, operateQueueFlags.Kubeconfig)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	if len(operateQueueFlags.Name) == 0 {
    72  		return fmt.Errorf("queue name must be specified")
    73  	}
    74  
    75  	var action v1alpha1.Action
    76  
    77  	switch operateQueueFlags.Action {
    78  	case ActionOpen:
    79  		action = v1alpha1.OpenQueueAction
    80  	case ActionClose:
    81  		action = v1alpha1.CloseQueueAction
    82  	case ActionUpdate:
    83  		if operateQueueFlags.Weight == 0 {
    84  			return fmt.Errorf("when %s queue %s, weight must be specified, "+
    85  				"the value must be greater than 0", ActionUpdate, operateQueueFlags.Name)
    86  		}
    87  
    88  		queueClient := versioned.NewForConfigOrDie(config)
    89  		patchBytes := []byte(fmt.Sprintf(`{"spec":{"weight":%d}}`, operateQueueFlags.Weight))
    90  		_, err := queueClient.SchedulingV1beta1().Queues().Patch(context.TODO(),
    91  			operateQueueFlags.Name, types.MergePatchType, patchBytes, metav1.PatchOptions{})
    92  
    93  		return err
    94  	case "":
    95  		return fmt.Errorf("action can not be null")
    96  	default:
    97  		return fmt.Errorf("action %s invalid, valid actions are %s, %s and %s",
    98  			operateQueueFlags.Action, ActionOpen, ActionClose, ActionUpdate)
    99  	}
   100  
   101  	return createQueueCommand(config, action)
   102  }