-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer_standalone.go
333 lines (282 loc) · 8.19 KB
/
trainer_standalone.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright 2018 The Kubeflow Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package commands
import (
"fmt"
"github.com/kubeflow/arena/pkg/types"
log "github.com/sirupsen/logrus"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// Standalone Job Information
type StandaloneJob struct {
*JobInfo
}
// Get Dashboard url of the job
func (sj *StandaloneJob) GetJobDashboards(client *kubernetes.Clientset) ([]string, error) {
urls := []string{}
// dashboardURL, err := dashboard(client, "kube-system", "kubernetes-dashboard")
dashboardURL, err := dashboard(client, namespace, "kubernetes-dashboard")
if err != nil {
log.Debugf("Get dashboard failed due to %v", err)
// retry for the existing customers, will be deprecated in the future
dashboardURL, err = dashboard(client, arenaNamespace, "kubernetes-dashboard")
if err != nil {
log.Debugf("Get dashboard failed due to %v", err)
}
}
if err != nil {
log.Debugf("Get dashboard failed due to %v", err)
// retry for the existing customers, will be deprecated in the future
dashboardURL, err = dashboard(client, "kube-system", "kubernetes-dashboard")
if err != nil {
log.Debugf("Get dashboard failed due to %v", err)
}
}
if dashboardURL == "" {
return urls, fmt.Errorf("No LOGVIEWER Installed.")
}
spec := sj.jobPod.Spec
job := sj.job
url := fmt.Sprintf("%s/#!/log/%s/%s/%s?namespace=%s\n",
dashboardURL,
job.Namespace,
sj.jobPod.Name,
spec.Containers[0].Name,
job.Namespace)
urls = append(urls, url)
return urls, nil
}
// Standalone Job trainer
type StandaloneJobTrainer struct {
client *kubernetes.Clientset
trainerType string
}
func NewStandaloneJobTrainer(client *kubernetes.Clientset) Trainer {
log.Debugf("Init standalone job trainer")
return &StandaloneJobTrainer{
client: client,
trainerType: "standalonejob",
}
}
func (s *StandaloneJobTrainer) Type() string {
return s.trainerType
}
// check if it's Standalone job
func (s *StandaloneJobTrainer) IsSupported(name, ns string) bool {
supported := false
if useCache {
for _, job := range allJobs {
if isStandaloneJob(name, ns, job) {
supported = true
log.Debugf("the job %s for %s in namespace %s is found.", job.Name, name, ns)
break
}
}
} else {
jobList, err := s.client.BatchV1().Jobs(ns).List(metav1.ListOptions{
TypeMeta: metav1.TypeMeta{
Kind: "ListOptions",
APIVersion: "v1",
}, LabelSelector: fmt.Sprintf("release=%s", name),
})
if err != nil {
log.Debugf("failed to search job %s in namespace %s due to %v", name, ns, err)
}
if len(jobList.Items) > 0 {
supported = true
}
}
return supported
}
func (s *StandaloneJobTrainer) GetTrainingJob(name, namespace string) (tj TrainingJob, err error) {
if useCache {
tj, err = s.getTrainingJobFromCache(name, namespace)
} else {
tj, err = s.getTrainingJob(name, namespace)
}
return tj, err
}
func (s *StandaloneJobTrainer) getTrainingJob(name, namespace string) (TrainingJob, error) {
var (
jobPod v1.Pod
job batchv1.Job
latest metav1.Time
)
// 1. Get the batchJob of training Job
pods := []v1.Pod{}
jobList, err := s.client.BatchV1().Jobs(namespace).List(metav1.ListOptions{
TypeMeta: metav1.TypeMeta{
Kind: "ListOptions",
APIVersion: "v1",
}, LabelSelector: fmt.Sprintf("release=%s", name),
})
if err != nil {
return nil, err
}
if len(jobList.Items) == 0 {
return nil, fmt.Errorf("Failed to find the job for %s", name)
} else {
job = jobList.Items[0]
}
// 2. Find the pod list, and determine the pod of the job
podList, err := s.client.CoreV1().Pods(namespace).List(metav1.ListOptions{
TypeMeta: metav1.TypeMeta{
Kind: "ListOptions",
APIVersion: "v1",
}, LabelSelector: fmt.Sprintf("release=%s", name),
})
if err != nil {
return nil, err
}
for _, item := range podList.Items {
if jobPod.Name == "" {
latest = item.CreationTimestamp
jobPod = item
log.Debugf("set pod %s as first jobpod, and it's time is %v", jobPod.Name, jobPod.CreationTimestamp)
} else {
log.Debugf("current jobpod %s , and it's time is %v", jobPod.Name, latest)
log.Debugf("candidate jobpod %s , and it's time is %v", item.Name, item.CreationTimestamp)
current := item.CreationTimestamp
if latest.Before(¤t) {
jobPod = item
latest = current
log.Debugf("replace")
} else {
log.Debugf("no replace")
}
}
}
pods = append(pods, jobPod)
return &StandaloneJob{
JobInfo: &JobInfo{
job: job,
jobPod: jobPod,
pods: pods,
name: name,
trainerType: s.Type(),
},
}, nil
}
// get the training job from cache
func (s *StandaloneJobTrainer) getTrainingJobFromCache(name, ns string) (TrainingJob, error) {
var (
jobPod v1.Pod
job batchv1.Job
latest metav1.Time
)
pods := []v1.Pod{}
for _, item := range allJobs {
if isStandaloneJob(name, ns, item) {
job = item
break
}
}
for _, item := range allPods {
if !isStandalonePod(name, ns, item) {
continue
}
if jobPod.Name == "" {
latest = item.CreationTimestamp
jobPod = item
log.Debugf("set pod %s as first jobpod, and it's time is %v", jobPod.Name, jobPod.CreationTimestamp)
} else {
log.Debugf("current jobpod %s , and it's time is %v", jobPod.Name, latest)
log.Debugf("candidate jobpod %s , and it's time is %v", item.Name, item.CreationTimestamp)
current := item.CreationTimestamp
if latest.Before(¤t) {
jobPod = item
latest = current
log.Debugf("replace")
} else {
log.Debugf("no replace")
}
}
}
pods = append(pods, jobPod)
return &StandaloneJob{
JobInfo: &JobInfo{
job: job,
jobPod: jobPod,
pods: pods,
name: name,
trainerType: s.Type(),
},
}, nil
}
/**
* List Training jobs
*/
func (s *StandaloneJobTrainer) ListTrainingJobs() (jobs []TrainingJob, err error) {
jobs = []TrainingJob{}
jobInfos := []types.TrainingJobInfo{}
innerLoop:
for _, standaloneJob := range allJobs {
jobInfo := types.TrainingJobInfo{}
log.Debugf("find standaloneJob %s in %s", standaloneJob.Name, standaloneJob.Namespace)
if val, ok := standaloneJob.Labels["release"]; ok && (standaloneJob.Name == fmt.Sprintf("%s-training", val)) {
log.Debugf("the standaloneJob %s with labels %s found in List", standaloneJob.Name, val)
jobInfo.Name = val
} else {
log.Debugf("the jobs %s with labels %s is not standaloneJob in List", standaloneJob.Name, val)
continue innerLoop
}
jobInfo.Namespace = standaloneJob.Namespace
jobInfos = append(jobInfos, jobInfo)
// jobInfos = append(jobInfos, types.TrainingJobInfo{Name: standaloneJob.})
}
log.Debugf("jobInfos %v", jobInfos)
for _, jobInfo := range jobInfos {
job, err := s.getTrainingJobFromCache(jobInfo.Name, jobInfo.Namespace)
if err != nil {
return jobs, err
}
jobs = append(jobs, job)
}
return jobs, err
}
func isStandaloneJob(name, ns string, item batchv1.Job) bool {
if val, ok := item.Labels["release"]; ok && (val == name) {
log.Debugf("the job %s with labels %s", item.Name, val)
} else {
return false
}
if val, ok := item.Labels["app"]; ok && (val == "training") {
log.Debugf("the job %s with labels %s is found.", item.Name, val)
} else {
return false
}
if item.Namespace != ns {
return false
}
return true
}
func isStandalonePod(name, ns string, item v1.Pod) bool {
if val, ok := item.Labels["release"]; ok && (val == name) {
log.Debugf("the pod %s with labels %s", item.Name, val)
} else {
return false
}
if val, ok := item.Labels["app"]; ok && (val == "training") {
log.Debugf("the pod %s with labels %s is found.", item.Name, val)
} else {
return false
}
if item.Namespace != ns {
return false
}
return true
}