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

     1  /*
     2  Copyright 2019 The Volcano 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  
    22  	"github.com/spf13/cobra"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  
    26  	schedulingv1beta1 "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
    27  	"volcano.sh/apis/pkg/client/clientset/versioned"
    28  )
    29  
    30  type createFlags struct {
    31  	commonFlags
    32  
    33  	Name   string
    34  	Weight int32
    35  	// State is state of Queue
    36  	State string
    37  }
    38  
    39  var createQueueFlags = &createFlags{}
    40  
    41  // InitCreateFlags is used to init all flags during queue creating.
    42  func InitCreateFlags(cmd *cobra.Command) {
    43  	initFlags(cmd, &createQueueFlags.commonFlags)
    44  
    45  	cmd.Flags().StringVarP(&createQueueFlags.Name, "name", "n", "test", "the name of queue")
    46  	cmd.Flags().Int32VarP(&createQueueFlags.Weight, "weight", "w", 1, "the weight of the queue")
    47  
    48  	cmd.Flags().StringVarP(&createQueueFlags.State, "state", "S", "Open", "the state of queue")
    49  }
    50  
    51  // CreateQueue create queue.
    52  func CreateQueue() error {
    53  	config, err := buildConfig(createQueueFlags.Master, createQueueFlags.Kubeconfig)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	queue := &schedulingv1beta1.Queue{
    59  		ObjectMeta: metav1.ObjectMeta{
    60  			Name: createQueueFlags.Name,
    61  		},
    62  		Spec: schedulingv1beta1.QueueSpec{
    63  			Weight: createQueueFlags.Weight,
    64  		},
    65  		Status: schedulingv1beta1.QueueStatus{
    66  			State: schedulingv1beta1.QueueState(createQueueFlags.State),
    67  		},
    68  	}
    69  
    70  	queueClient := versioned.NewForConfigOrDie(config)
    71  	if _, err := queueClient.SchedulingV1beta1().Queues().Create(context.TODO(), queue, metav1.CreateOptions{}); err != nil {
    72  		return err
    73  	}
    74  
    75  	return nil
    76  }