volcano.sh/volcano@v1.9.0/pkg/cli/job/list.go (about) 1 /* 2 Copyright 2018 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 job 18 19 import ( 20 "context" 21 "fmt" 22 "io" 23 "os" 24 "strings" 25 26 "github.com/spf13/cobra" 27 28 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 29 30 "volcano.sh/apis/pkg/apis/batch/v1alpha1" 31 "volcano.sh/apis/pkg/client/clientset/versioned" 32 "volcano.sh/volcano/pkg/cli/util" 33 ) 34 35 type listFlags struct { 36 commonFlags 37 38 Namespace string 39 SchedulerName string 40 allNamespace bool 41 selector string 42 } 43 44 const ( 45 46 // Name name etc below key words are used in job print format 47 Name string = "Name" 48 // Creation create 49 Creation string = "Creation" 50 // Phase phase 51 Phase string = "Phase" 52 // Replicas replicas 53 Replicas string = "Replicas" 54 // Min minimum 55 Min string = "Min" 56 // Scheduler scheduler 57 Scheduler string = "Scheduler" 58 // Pending pending 59 Pending string = "Pending" 60 // Running running 61 Running string = "Running" 62 // Succeeded success 63 Succeeded string = "Succeeded" 64 // Terminating terminating 65 Terminating string = "Terminating" 66 // Version version 67 Version string = "Version" 68 // Failed failed 69 Failed string = "Failed" 70 // Unknown pod 71 Unknown string = "Unknown" 72 // RetryCount retry count 73 RetryCount string = "RetryCount" 74 // JobType job type 75 JobType string = "JobType" 76 // Namespace job namespace 77 Namespace string = "Namespace" 78 ) 79 80 var listJobFlags = &listFlags{} 81 82 // InitListFlags init list command flags. 83 func InitListFlags(cmd *cobra.Command) { 84 initFlags(cmd, &listJobFlags.commonFlags) 85 86 cmd.Flags().StringVarP(&listJobFlags.Namespace, "namespace", "n", "default", "the namespace of job") 87 cmd.Flags().StringVarP(&listJobFlags.SchedulerName, "scheduler", "S", "", "list job with specified scheduler name") 88 cmd.Flags().BoolVarP(&listJobFlags.allNamespace, "all-namespaces", "", false, "list jobs in all namespaces") 89 cmd.Flags().StringVarP(&listJobFlags.selector, "selector", "", "", "fuzzy matching jobName") 90 } 91 92 // ListJobs lists all jobs details. 93 func ListJobs() error { 94 config, err := util.BuildConfig(listJobFlags.Master, listJobFlags.Kubeconfig) 95 if err != nil { 96 return err 97 } 98 if listJobFlags.allNamespace { 99 listJobFlags.Namespace = "" 100 } 101 jobClient := versioned.NewForConfigOrDie(config) 102 jobs, err := jobClient.BatchV1alpha1().Jobs(listJobFlags.Namespace).List(context.TODO(), metav1.ListOptions{}) 103 if err != nil { 104 return err 105 } 106 107 if len(jobs.Items) == 0 { 108 fmt.Printf("No resources found\n") 109 return nil 110 } 111 PrintJobs(jobs, os.Stdout) 112 113 return nil 114 } 115 116 // PrintJobs prints all jobs details. 117 func PrintJobs(jobs *v1alpha1.JobList, writer io.Writer) { 118 maxLenInfo := getMaxLen(jobs) 119 120 titleFormat := "%%-%ds%%-15s%%-12s%%-12s%%-12s%%-6s%%-10s%%-10s%%-12s%%-10s%%-12s%%-10s\n" 121 contentFormat := "%%-%ds%%-15s%%-12s%%-12s%%-12d%%-6d%%-10d%%-10d%%-12d%%-10d%%-12d%%-10d\n" 122 123 var err error 124 if listJobFlags.allNamespace { 125 _, err = fmt.Fprintf(writer, fmt.Sprintf("%%-%ds"+titleFormat, maxLenInfo[1], maxLenInfo[0]), 126 Namespace, Name, Creation, Phase, JobType, Replicas, Min, Pending, Running, Succeeded, Failed, Unknown, RetryCount) 127 } else { 128 _, err = fmt.Fprintf(writer, fmt.Sprintf(titleFormat, maxLenInfo[0]), 129 Name, Creation, Phase, JobType, Replicas, Min, Pending, Running, Succeeded, Failed, Unknown, RetryCount) 130 } 131 if err != nil { 132 fmt.Printf("Failed to print list command result: %s.\n", err) 133 } 134 135 for _, job := range jobs.Items { 136 if listJobFlags.SchedulerName != "" && listJobFlags.SchedulerName != job.Spec.SchedulerName { 137 continue 138 } 139 if !strings.Contains(job.Name, listJobFlags.selector) { 140 continue 141 } 142 replicas := int32(0) 143 for _, ts := range job.Spec.Tasks { 144 replicas += ts.Replicas 145 } 146 jobType := job.ObjectMeta.Labels[v1alpha1.JobTypeKey] 147 if jobType == "" { 148 jobType = "Batch" 149 } 150 151 if listJobFlags.allNamespace { 152 _, err = fmt.Fprintf(writer, fmt.Sprintf("%%-%ds"+contentFormat, maxLenInfo[1], maxLenInfo[0]), 153 job.Namespace, job.Name, job.CreationTimestamp.Format("2006-01-02"), job.Status.State.Phase, jobType, replicas, 154 job.Status.MinAvailable, job.Status.Pending, job.Status.Running, job.Status.Succeeded, job.Status.Failed, job.Status.Unknown, job.Status.RetryCount) 155 } else { 156 _, err = fmt.Fprintf(writer, fmt.Sprintf(contentFormat, maxLenInfo[0]), 157 job.Name, job.CreationTimestamp.Format("2006-01-02"), job.Status.State.Phase, jobType, replicas, 158 job.Status.MinAvailable, job.Status.Pending, job.Status.Running, job.Status.Succeeded, job.Status.Failed, job.Status.Unknown, job.Status.RetryCount) 159 } 160 if err != nil { 161 fmt.Printf("Failed to print list command result: %s.\n", err) 162 } 163 } 164 } 165 166 func getMaxLen(jobs *v1alpha1.JobList) []int { 167 maxNameLen := len(Name) 168 maxNamespaceLen := len(Namespace) 169 for _, job := range jobs.Items { 170 if len(job.Name) > maxNameLen { 171 maxNameLen = len(job.Name) 172 } 173 if len(job.Namespace) > maxNamespaceLen { 174 maxNamespaceLen = len(job.Namespace) 175 } 176 } 177 178 return []int{maxNameLen + 3, maxNamespaceLen + 3} 179 }