volcano.sh/volcano@v1.9.0/pkg/cli/queue/list.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  	"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  )
    32  
    33  type listFlags struct {
    34  	commonFlags
    35  }
    36  
    37  const (
    38  	// Weight of the queue
    39  	Weight string = "Weight"
    40  
    41  	// Name of queue
    42  	Name string = "Name"
    43  
    44  	// Pending status of the queue
    45  	Pending string = "Pending"
    46  
    47  	// Running status of the queue
    48  	Running string = "Running"
    49  
    50  	// Unknown status of the queue
    51  	Unknown string = "Unknown"
    52  
    53  	// Inqueue status of queue
    54  	Inqueue string = "Inqueue"
    55  
    56  	// State is state of queue
    57  	State string = "State"
    58  )
    59  
    60  var listQueueFlags = &listFlags{}
    61  
    62  // InitListFlags inits all flags.
    63  func InitListFlags(cmd *cobra.Command) {
    64  	initFlags(cmd, &listQueueFlags.commonFlags)
    65  }
    66  
    67  // ListQueue lists all the queue.
    68  func ListQueue() error {
    69  	config, err := buildConfig(listQueueFlags.Master, listQueueFlags.Kubeconfig)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	jobClient := versioned.NewForConfigOrDie(config)
    75  	queues, err := jobClient.SchedulingV1beta1().Queues().List(context.TODO(), metav1.ListOptions{})
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	if len(queues.Items) == 0 {
    81  		fmt.Printf("No resources found\n")
    82  		return nil
    83  	}
    84  	PrintQueues(queues, os.Stdout)
    85  
    86  	return nil
    87  }
    88  
    89  // PrintQueues prints queue information.
    90  func PrintQueues(queues *v1beta1.QueueList, writer io.Writer) {
    91  	_, err := fmt.Fprintf(writer, "%-25s%-8s%-8s%-8s%-8s%-8s%-8s\n",
    92  		Name, Weight, State, Inqueue, Pending, Running, Unknown)
    93  	if err != nil {
    94  		fmt.Printf("Failed to print queue command result: %s.\n", err)
    95  	}
    96  	for _, queue := range queues.Items {
    97  		_, err = fmt.Fprintf(writer, "%-25s%-8d%-8s%-8d%-8d%-8d%-8d\n",
    98  			queue.Name, queue.Spec.Weight, queue.Status.State, queue.Status.Inqueue,
    99  			queue.Status.Pending, queue.Status.Running, queue.Status.Unknown)
   100  		if err != nil {
   101  			fmt.Printf("Failed to print queue command result: %s.\n", err)
   102  		}
   103  	}
   104  }