volcano.sh/volcano@v1.9.0/pkg/scheduler/api/helpers.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 api 18 19 import ( 20 "fmt" 21 22 v1 "k8s.io/api/core/v1" 23 clientcache "k8s.io/client-go/tools/cache" 24 ) 25 26 // PodKey returns the string key of a pod. 27 func PodKey(pod *v1.Pod) TaskID { 28 key, err := clientcache.MetaNamespaceKeyFunc(pod) 29 if err != nil { 30 return TaskID(fmt.Sprintf("%v/%v", pod.Namespace, pod.Name)) 31 } 32 return TaskID(key) 33 } 34 35 func getTaskStatus(pod *v1.Pod) TaskStatus { 36 switch pod.Status.Phase { 37 case v1.PodRunning: 38 if pod.DeletionTimestamp != nil { 39 return Releasing 40 } 41 42 return Running 43 case v1.PodPending: 44 if pod.DeletionTimestamp != nil { 45 return Releasing 46 } 47 48 if len(pod.Spec.NodeName) == 0 { 49 return Pending 50 } 51 return Bound 52 case v1.PodUnknown: 53 return Unknown 54 case v1.PodSucceeded: 55 return Succeeded 56 case v1.PodFailed: 57 return Failed 58 } 59 60 return Unknown 61 } 62 63 // PreemptableStatus checks whether the task can be preempted 64 func PreemptableStatus(status TaskStatus) bool { 65 switch status { 66 case Bound, Running: 67 return true 68 default: 69 return false 70 } 71 } 72 73 // AllocatedStatus checks whether the tasks has AllocatedStatus 74 func AllocatedStatus(status TaskStatus) bool { 75 switch status { 76 case Bound, Binding, Running, Allocated: 77 return true 78 default: 79 return false 80 } 81 } 82 83 // MergeErrors is used to merge multiple errors into single error 84 func MergeErrors(errs ...error) error { 85 msg := "errors: " 86 87 foundErr := false 88 i := 1 89 90 for _, e := range errs { 91 if e != nil { 92 if foundErr { 93 msg = fmt.Sprintf("%s, %d: ", msg, i) 94 } else { 95 msg = fmt.Sprintf("%s %d: ", msg, i) 96 } 97 98 msg = fmt.Sprintf("%s%v", msg, e) 99 foundErr = true 100 i++ 101 } 102 } 103 104 if foundErr { 105 return fmt.Errorf("%s", msg) 106 } 107 108 return nil 109 } 110 111 // JobTerminated checks whether job was terminated. 112 func JobTerminated(job *JobInfo) bool { 113 return job.PodGroup == nil && len(job.Tasks) == 0 114 }