forked from kubeflow/katib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.go
441 lines (385 loc) · 13.6 KB
/
backend.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
Copyright 2021 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 v1beta1
import (
"encoding/json"
"log"
"net/http"
"path/filepath"
"google.golang.org/grpc"
"sigs.k8s.io/controller-runtime/pkg/client"
experimentv1beta1 "github.com/kubeflow/katib/pkg/apis/controller/experiments/v1beta1"
api_pb_v1beta1 "github.com/kubeflow/katib/pkg/apis/manager/v1beta1"
"github.com/kubeflow/katib/pkg/util/v1beta1/katibclient"
)
func NewKatibUIHandler(dbManagerAddr string) *KatibUIHandler {
kclient, err := katibclient.NewClient(client.Options{})
if err != nil {
log.Printf("NewClient for Katib failed: %v", err)
panic(err)
}
return &KatibUIHandler{
katibClient: kclient,
dbManagerAddr: dbManagerAddr,
}
}
// ServeIndex will return index.html for any non-API URL
func (k *KatibUIHandler) ServeIndex(buildDir string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
fp := filepath.Join(buildDir, "static/index.html")
log.Printf("Sending file %s for url: %s", fp, r.URL)
// never cache index.html
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0")
// return the contents of index.html
http.ServeFile(w, r, fp)
}
}
func (k *KatibUIHandler) connectManager() (*grpc.ClientConn, api_pb_v1beta1.DBManagerClient) {
conn, err := grpc.Dial(k.dbManagerAddr, grpc.WithInsecure())
if err != nil {
log.Printf("Dial to GRPC failed: %v", err)
return nil, nil
}
c := api_pb_v1beta1.NewDBManagerClient(conn)
return conn, c
}
func (k *KatibUIHandler) CreateExperiment(w http.ResponseWriter, r *http.Request) {
var data map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
log.Printf("Failed to decode body: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
dataJSON, ok := data["postData"]
if !ok {
msg := "Couldn't load the 'postData' field of the request's data"
log.Println(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
jsonbody, err := json.Marshal(dataJSON)
if err != nil {
log.Printf("Marshal data for experiment failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
job := experimentv1beta1.Experiment{}
if err := json.Unmarshal(jsonbody, &job); err != nil {
log.Printf("Unmarshal experiment failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = k.katibClient.CreateRuntimeObject(&job)
if err != nil {
log.Printf("CreateRuntimeObject from parameters failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// FetchAllExperiments gets HP and NAS experiments in all namespaces.
func (k *KatibUIHandler) FetchAllExperiments(w http.ResponseWriter, r *http.Request) {
// At first, try to list experiments in cluster scope
experiments, err := k.getExperiments([]string{""})
if err != nil {
// If failed, just try to list experiments from own namespace
experiments, err = k.getExperiments([]string{})
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response, err := json.Marshal(experiments)
if err != nil {
log.Printf("Marshal experiments failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err = w.Write(response); err != nil {
log.Printf("Write experiments failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (k *KatibUIHandler) DeleteExperiment(w http.ResponseWriter, r *http.Request) {
experimentName := r.URL.Query()["experimentName"][0]
namespace := r.URL.Query()["namespace"][0]
experiment, err := k.katibClient.GetExperiment(experimentName, namespace)
if err != nil {
log.Printf("GetExperiment failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = k.katibClient.DeleteRuntimeObject(experiment)
if err != nil {
log.Printf("DeleteRuntimeObject failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
isExperimentDeleted := false
var experiments []ExperimentView
// Waiting until experiment will be deleted
for !isExperimentDeleted {
// At first, try to list experiments in cluster scope
experiments, err = k.getExperiments([]string{""})
if err != nil {
// If failed, just try to list experiments from own namespace
experiments, err = k.getExperiments([]string{})
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
isExperimentDeleted = true
for _, experiment := range experiments {
if experiment.Name == experimentName {
isExperimentDeleted = false
break
}
}
}
response, err := json.Marshal(experiments)
if err != nil {
log.Printf("Marshal HP and NAS experiments failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err = w.Write(response); err != nil {
log.Printf("Write HP and NAS experiments failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// FetchTrialTemplates gets all trial templates in all namespaces
func (k *KatibUIHandler) FetchTrialTemplates(w http.ResponseWriter, r *http.Request) {
trialTemplatesViewList, err := k.getTrialTemplatesViewList()
if err != nil {
log.Printf("getTrialTemplatesViewList failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
TrialTemplatesResponse := TrialTemplatesResponse{
Data: trialTemplatesViewList,
}
response, err := json.Marshal(TrialTemplatesResponse)
if err != nil {
log.Printf("Marshal templates failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err = w.Write(response); err != nil {
log.Printf("Write template failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
//AddTemplate adds template to ConfigMap
func (k *KatibUIHandler) AddTemplate(w http.ResponseWriter, r *http.Request) {
var data map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
log.Printf("Failed to decode body: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
updatedConfigMapNamespace := data["updatedConfigMapNamespace"].(string)
updatedConfigMapName := data["updatedConfigMapName"].(string)
updatedConfigMapPath := data["updatedConfigMapPath"].(string)
updatedTemplateYaml := data["updatedTemplateYaml"].(string)
newTemplates, err := k.updateTrialTemplates(updatedConfigMapNamespace, updatedConfigMapName, "", updatedConfigMapPath, updatedTemplateYaml, ActionTypeAdd)
if err != nil {
log.Printf("updateTrialTemplates failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
TrialTemplatesResponse := TrialTemplatesResponse{
Data: newTemplates,
}
response, err := json.Marshal(TrialTemplatesResponse)
if err != nil {
log.Printf("Marhal failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err = w.Write(response); err != nil {
log.Printf("Write failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// EditTemplate edits template in ConfigMap
func (k *KatibUIHandler) EditTemplate(w http.ResponseWriter, r *http.Request) {
var data map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
log.Printf("Failed to decode body: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
updatedConfigMapNamespace := data["updatedConfigMapNamespace"].(string)
updatedConfigMapName := data["updatedConfigMapName"].(string)
configMapPath := data["configMapPath"].(string)
updatedConfigMapPath := data["updatedConfigMapPath"].(string)
updatedTemplateYaml := data["updatedTemplateYaml"].(string)
newTemplates, err := k.updateTrialTemplates(updatedConfigMapNamespace, updatedConfigMapName, configMapPath, updatedConfigMapPath, updatedTemplateYaml, ActionTypeEdit)
if err != nil {
log.Printf("updateTrialTemplates failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
TrialTemplatesResponse := TrialTemplatesResponse{
Data: newTemplates,
}
response, err := json.Marshal(TrialTemplatesResponse)
if err != nil {
log.Printf("Marhal failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err = w.Write(response); err != nil {
log.Printf("Write failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// DeleteTemplate deletes template in ConfigMap
func (k *KatibUIHandler) DeleteTemplate(w http.ResponseWriter, r *http.Request) {
var data map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
log.Printf("Failed to decode body: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
updatedConfigMapNamespace := data["updatedConfigMapNamespace"].(string)
updatedConfigMapName := data["updatedConfigMapName"].(string)
updatedConfigMapPath := data["updatedConfigMapPath"].(string)
newTemplates, err := k.updateTrialTemplates(updatedConfigMapNamespace, updatedConfigMapName, "", updatedConfigMapPath, "", ActionTypeDelete)
if err != nil {
log.Printf("updateTrialTemplates failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
TrialTemplatesResponse := TrialTemplatesResponse{
Data: newTemplates,
}
response, err := json.Marshal(TrialTemplatesResponse)
if err != nil {
log.Printf("Marhal failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err = w.Write(response); err != nil {
log.Printf("Write failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (k *KatibUIHandler) FetchNamespaces(w http.ResponseWriter, r *http.Request) {
// Get all available namespaces
namespaces, err := k.getAvailableNamespaces()
if err != nil {
log.Printf("GetAvailableNamespaces failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response, err := json.Marshal(namespaces)
if err != nil {
log.Printf("Marshal namespaces failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err = w.Write(response); err != nil {
log.Printf("Write namespaces failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// FetchExperiment gets experiment in specific namespace.
func (k *KatibUIHandler) FetchExperiment(w http.ResponseWriter, r *http.Request) {
experimentName := r.URL.Query()["experimentName"][0]
namespace := r.URL.Query()["namespace"][0]
experiment, err := k.katibClient.GetExperiment(experimentName, namespace)
if err != nil {
log.Printf("GetExperiment failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response, err := json.Marshal(experiment)
if err != nil {
log.Printf("Marshal Experiment failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err = w.Write(response); err != nil {
log.Printf("Write Experiment failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// FetchSuggestion gets suggestion in specific namespace
func (k *KatibUIHandler) FetchSuggestion(w http.ResponseWriter, r *http.Request) {
suggestionName := r.URL.Query()["suggestionName"][0]
namespace := r.URL.Query()["namespace"][0]
suggestion, err := k.katibClient.GetSuggestion(suggestionName, namespace)
if err != nil {
log.Printf("GetSuggestion failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response, err := json.Marshal(suggestion)
if err != nil {
log.Printf("Marshal Suggestion failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err = w.Write(response); err != nil {
log.Printf("Write Suggestion failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// FetchTrial gets trial in specific namespace.
func (k *KatibUIHandler) FetchTrial(w http.ResponseWriter, r *http.Request) {
trialName := r.URL.Query()["trialName"][0]
namespace := r.URL.Query()["namespace"][0]
trial, err := k.katibClient.GetTrial(trialName, namespace)
if err != nil {
log.Printf("GetTrial failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response, err := json.Marshal(trial)
if err != nil {
log.Printf("Marshal Trial failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(response)
}
// FetchPodsLogs is sth
func (k *KatibUIHandler) FetchPodsLogs(w http.ResponseWriter, r *http.Request) {
log.Printf("Requesting logs")
trialName := r.URL.Query()["trialName"][0]
namespace := r.URL.Query()["namespace"][0]
log.Printf("Requesting logs")
logs, err := k.katibClient.GetTrialPodsLogs(trialName, namespace)
if err != nil {
log.Printf("GetLogs failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response, err := json.Marshal(logs)
if err != nil {
log.Printf("Marshal DeleteObservationLogRequest failed: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(response)
}