volcano.sh/volcano@v1.9.0/pkg/cli/vqueues/get.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 vqueues
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"io"
    23  	"os"
    24  
    25  	"github.com/spf13/cobra"
    26  
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  
    29  	"volcano.sh/apis/pkg/apis/scheduling/v1beta1"
    30  	"volcano.sh/apis/pkg/client/clientset/versioned"
    31  	"volcano.sh/volcano/pkg/cli/util"
    32  )
    33  
    34  type getFlags struct {
    35  	util.CommonFlags
    36  
    37  	Name string
    38  }
    39  
    40  const (
    41  	// Weight of the queue
    42  	Weight string = "Weight"
    43  
    44  	// Name of queue
    45  	Name string = "Name"
    46  
    47  	// Pending status of the queue
    48  	Pending string = "Pending"
    49  
    50  	// Running status of the queue
    51  	Running string = "Running"
    52  
    53  	// Unknown status of the queue
    54  	Unknown string = "Unknown"
    55  
    56  	// Inqueue status of queue
    57  	Inqueue string = "Inqueue"
    58  
    59  	// State is state of queue
    60  	State string = "State"
    61  )
    62  
    63  var getQueueFlags = &getFlags{}
    64  
    65  // InitGetFlags is used to init all flags.
    66  func InitGetFlags(cmd *cobra.Command) {
    67  	util.InitFlags(cmd, &getQueueFlags.CommonFlags)
    68  
    69  	cmd.Flags().StringVarP(&getQueueFlags.Name, "name", "n", "", "the name of queue")
    70  }
    71  
    72  // ListQueue lists all the queue.
    73  func ListQueue() error {
    74  	config, err := util.BuildConfig(getQueueFlags.Master, getQueueFlags.Kubeconfig)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	jobClient := versioned.NewForConfigOrDie(config)
    80  	queues, err := jobClient.SchedulingV1beta1().Queues().List(context.TODO(), metav1.ListOptions{})
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	if len(queues.Items) == 0 {
    86  		fmt.Printf("No resources found\n")
    87  		return nil
    88  	}
    89  	PrintQueues(queues, os.Stdout)
    90  
    91  	return nil
    92  }
    93  
    94  // PrintQueues prints queue information.
    95  func PrintQueues(queues *v1beta1.QueueList, writer io.Writer) {
    96  	_, err := fmt.Fprintf(writer, "%-25s%-8s%-8s%-8s%-8s%-8s%-8s\n",
    97  		Name, Weight, State, Inqueue, Pending, Running, Unknown)
    98  	if err != nil {
    99  		fmt.Printf("Failed to print queue command result: %s.\n", err)
   100  	}
   101  	for _, queue := range queues.Items {
   102  		_, err = fmt.Fprintf(writer, "%-25s%-8d%-8s%-8d%-8d%-8d%-8d\n",
   103  			queue.Name, queue.Spec.Weight, queue.Status.State, queue.Status.Inqueue,
   104  			queue.Status.Pending, queue.Status.Running, queue.Status.Unknown)
   105  		if err != nil {
   106  			fmt.Printf("Failed to print queue command result: %s.\n", err)
   107  		}
   108  	}
   109  }
   110  
   111  // GetQueue gets a queue.
   112  func GetQueue() error {
   113  	config, err := util.BuildConfig(getQueueFlags.Master, getQueueFlags.Kubeconfig)
   114  	if err != nil {
   115  		return err
   116  	}
   117  
   118  	if getQueueFlags.Name == "" {
   119  		err := ListQueue()
   120  		return err
   121  	}
   122  
   123  	queueClient := versioned.NewForConfigOrDie(config)
   124  	queue, err := queueClient.SchedulingV1beta1().Queues().Get(context.TODO(), getQueueFlags.Name, metav1.GetOptions{})
   125  	if err != nil {
   126  		return err
   127  	}
   128  
   129  	PrintQueue(queue, os.Stdout)
   130  
   131  	return nil
   132  }
   133  
   134  // PrintQueue prints queue information.
   135  func PrintQueue(queue *v1beta1.Queue, writer io.Writer) {
   136  	_, err := fmt.Fprintf(writer, "%-25s%-8s%-8s%-8s%-8s%-8s%-8s\n",
   137  		Name, Weight, State, Inqueue, Pending, Running, Unknown)
   138  	if err != nil {
   139  		fmt.Printf("Failed to print queue command result: %s.\n", err)
   140  	}
   141  	_, err = fmt.Fprintf(writer, "%-25s%-8d%-8s%-8d%-8d%-8d%-8d\n",
   142  		queue.Name, queue.Spec.Weight, queue.Status.State, queue.Status.Inqueue,
   143  		queue.Status.Pending, queue.Status.Running, queue.Status.Unknown)
   144  	if err != nil {
   145  		fmt.Printf("Failed to print queue command result: %s.\n", err)
   146  	}
   147  }