volcano.sh/volcano@v1.9.0/pkg/controllers/job/helpers/helpers.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 helpers 18 19 import ( 20 "fmt" 21 "math/rand" 22 "strconv" 23 "strings" 24 "time" 25 26 v1 "k8s.io/api/core/v1" 27 28 batch "volcano.sh/apis/pkg/apis/batch/v1alpha1" 29 "volcano.sh/volcano/pkg/controllers/apis" 30 "volcano.sh/volcano/pkg/scheduler/api" 31 ) 32 33 const ( 34 // PodNameFmt pod name format 35 PodNameFmt = "%s-%s-%d" 36 // persistentVolumeClaimFmt represents persistent volume claim name format 37 persistentVolumeClaimFmt = "%s-pvc-%s" 38 ) 39 40 // GetPodIndexUnderTask returns task Index. 41 func GetPodIndexUnderTask(pod *v1.Pod) string { 42 num := strings.Split(pod.Name, "-") 43 if len(num) >= 3 { 44 return num[len(num)-1] 45 } 46 47 return "" 48 } 49 50 // CompareTask by pod index 51 func CompareTask(lv, rv *api.TaskInfo) bool { 52 lStr := GetPodIndexUnderTask(lv.Pod) 53 rStr := GetPodIndexUnderTask(rv.Pod) 54 lIndex, lErr := strconv.Atoi(lStr) 55 rIndex, rErr := strconv.Atoi(rStr) 56 if lErr != nil || rErr != nil || lIndex == rIndex { 57 return lv.Pod.CreationTimestamp.Before(&rv.Pod.CreationTimestamp) 58 } 59 if lIndex > rIndex { 60 return false 61 } 62 return true 63 } 64 65 // GetTaskKey returns task key/name 66 func GetTaskKey(pod *v1.Pod) string { 67 if pod.Annotations == nil || pod.Annotations[batch.TaskSpecKey] == "" { 68 return batch.DefaultTaskSpec 69 } 70 return pod.Annotations[batch.TaskSpecKey] 71 } 72 73 // GetTaskSpec returns task spec 74 func GetTaskSpec(job *batch.Job, taskName string) (batch.TaskSpec, bool) { 75 for _, ts := range job.Spec.Tasks { 76 if ts.Name == taskName { 77 return ts, true 78 } 79 } 80 return batch.TaskSpec{}, false 81 } 82 83 // MakeDomainName creates task domain name 84 func MakeDomainName(ts batch.TaskSpec, job *batch.Job, index int) string { 85 hostName := ts.Template.Spec.Hostname 86 subdomain := ts.Template.Spec.Subdomain 87 if len(hostName) == 0 { 88 hostName = MakePodName(job.Name, ts.Name, index) 89 } 90 if len(subdomain) == 0 { 91 subdomain = job.Name 92 } 93 return hostName + "." + subdomain 94 } 95 96 // MakePodName creates pod name. 97 func MakePodName(jobName string, taskName string, index int) string { 98 return fmt.Sprintf(PodNameFmt, jobName, taskName, index) 99 } 100 101 // GenRandomStr generate random str with specified length l. 102 func GenRandomStr(l int) string { 103 str := "0123456789abcdefghijklmnopqrstuvwxyz" 104 bytes := []byte(str) 105 var result []byte 106 r := rand.New(rand.NewSource(time.Now().UnixNano())) 107 for i := 0; i < l; i++ { 108 result = append(result, bytes[r.Intn(len(bytes))]) 109 } 110 return string(result) 111 } 112 113 // GenPVCName generates pvc name with job name. 114 func GenPVCName(jobName string) string { 115 return fmt.Sprintf(persistentVolumeClaimFmt, jobName, GenRandomStr(12)) 116 } 117 118 // GetJobKeyByReq gets the key for the job request. 119 func GetJobKeyByReq(req *apis.Request) string { 120 return fmt.Sprintf("%s/%s", req.Namespace, req.JobName) 121 } 122 123 // GetTaskIndexUnderJob return index of the task in the job. 124 func GetTaskIndexUnderJob(taskName string, job *batch.Job) int { 125 for index, task := range job.Spec.Tasks { 126 if task.Name == taskName { 127 return index 128 } 129 } 130 return -1 131 } 132 133 // GetPodsNameUnderTask return names of all pods in the task. 134 func GetPodsNameUnderTask(taskName string, job *batch.Job) []string { 135 var res []string 136 for _, task := range job.Spec.Tasks { 137 if task.Name == taskName { 138 for index := 0; index < int(task.Replicas); index++ { 139 res = append(res, MakePodName(job.Name, taskName, index)) 140 } 141 break 142 } 143 } 144 return res 145 }