-
Notifications
You must be signed in to change notification settings - Fork 448
/
util.go
338 lines (295 loc) · 10.2 KB
/
util.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
/*
Copyright 2022 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"
"sort"
"strconv"
"strings"
"github.com/kubeflow/katib/pkg/controller.v1beta1/consts"
gographviz "github.com/awalterschulze/gographviz"
trialv1beta1 "github.com/kubeflow/katib/pkg/apis/controller/trials/v1beta1"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (k *KatibUIHandler) getExperiments(namespace []string) ([]ExperimentView, error) {
experiments := []ExperimentView{}
el, err := k.katibClient.GetExperimentList(namespace...)
if err != nil {
log.Printf("GetExperimentList failed: %v", err)
return nil, err
}
for _, experiment := range el.Items {
experimentLastCondition, err := experiment.GetLastConditionType()
if err != nil {
log.Printf("GetLastConditionType failed: %v", err)
return nil, err
}
tp := ExperimentTypeHP
if experiment.Spec.NasConfig != nil {
tp = ExperimentTypeNAS
}
newExperiment := ExperimentView{
experiment.Name,
experiment.Namespace,
tp,
string(experimentLastCondition),
experiment.Status,
}
experiments = append(experiments, newExperiment)
}
return experiments, nil
}
func havePipelineUID(trials []trialv1beta1.Trial) bool {
for _, t := range trials {
_, ok := t.GetAnnotations()[kfpRunIDAnnotation]
if ok {
return true
}
}
return false
}
func (k *KatibUIHandler) getTrialTemplatesViewList(r *http.Request) ([]TrialTemplatesDataView, error) {
trialTemplatesDataView := make([]TrialTemplatesDataView, 0)
// Get all available namespaces
namespaces, err := k.getAvailableNamespaces()
if err != nil {
log.Printf("GetAvailableNamespaces failed: %v", err)
return nil, err
}
// Get Trial Template ConfigMap for each namespace
for _, ns := range namespaces {
trialTemplatesConfigMapList, err := k.katibClient.GetTrialTemplates(ns)
if err != nil {
log.Printf("GetTrialTemplates failed: %v", err)
return nil, err
}
// Iterate over the trialTemplatesConfigMapList from all namespaces and filter out the
// configmaps that belong to namespaces other than kubeflow and the ones that the user has
// access privileges.
var newTrialTemplatesConfigMapList []apiv1.ConfigMap
for _, cmap := range trialTemplatesConfigMapList.Items {
if ns == consts.DefaultKatibNamespace {
// Add the configmaps from kubeflow namespace (no user can have access to kubeflow ns, so we hardcode it)
newTrialTemplatesConfigMapList = append(newTrialTemplatesConfigMapList, cmap)
} else {
// for all other namespaces check authorization rbac
configmapName := cmap.ObjectMeta.Name
user, err := IsAuthorized(consts.ActionTypeGet, ns, apiv1.ResourceConfigMaps.String(), "", configmapName, apiv1.SchemeGroupVersion, k.katibClient.GetClient(), r)
if err != nil {
log.Printf("The user: %s is not authorized to view configMap: %s in namespace: %s \n", user, configmapName, ns)
return nil, err
} else {
log.Printf("The user: %s is authorized to view configMap: %s in namespace: %s", user, configmapName, ns)
newTrialTemplatesConfigMapList = append(newTrialTemplatesConfigMapList, cmap)
}
}
}
if len(trialTemplatesConfigMapList.Items) != 0 {
newTrialTemplatesView := getTrialTemplatesView(newTrialTemplatesConfigMapList)
// ConfigMap with templates must exists in namespace
if len(newTrialTemplatesView.ConfigMaps) > 0 {
trialTemplatesDataView = append(trialTemplatesDataView, newTrialTemplatesView)
}
}
}
return trialTemplatesDataView, nil
}
func (k *KatibUIHandler) getAvailableNamespaces() ([]string, error) {
var namespaces []string
namespaceList, err := k.katibClient.GetNamespaceList()
if err != nil {
namespaces = append(namespaces, consts.DefaultKatibNamespace)
return namespaces, nil
}
for _, ns := range namespaceList.Items {
namespaces = append(namespaces, ns.ObjectMeta.Name)
}
return namespaces, nil
}
func getTrialTemplatesView(templatesConfigMapList []apiv1.ConfigMap) TrialTemplatesDataView {
trialTemplatesDataView := TrialTemplatesDataView{
ConfigMapNamespace: templatesConfigMapList[0].ObjectMeta.Namespace,
ConfigMaps: []ConfigMap{},
}
for _, configMap := range templatesConfigMapList {
newConfigMap := ConfigMap{
ConfigMapName: configMap.ObjectMeta.Name,
Templates: []Template{},
}
for key := range configMap.Data {
newTemplate := Template{
Path: key,
Yaml: configMap.Data[key],
}
newConfigMap.Templates = append(newConfigMap.Templates, newTemplate)
}
// Sort Trial Templates by Path
sort.SliceStable(newConfigMap.Templates, func(i, j int) bool {
return newConfigMap.Templates[i].Path <= newConfigMap.Templates[j].Path
})
// Templates with data must exists in ConfigMap
if len(newConfigMap.Templates) > 0 {
trialTemplatesDataView.ConfigMaps = append(trialTemplatesDataView.ConfigMaps, newConfigMap)
}
}
return trialTemplatesDataView
}
func (k *KatibUIHandler) updateTrialTemplates(
updatedConfigMapNamespace,
updatedConfigMapName,
configMapPath,
updatedConfigMapPath,
updatedTemplateYaml,
actionType string,
r *http.Request) ([]TrialTemplatesDataView, error) {
templates, err := k.katibClient.GetConfigMap(updatedConfigMapName, updatedConfigMapNamespace)
if err != nil && !(errors.IsNotFound(err) && actionType == ActionTypeAdd) {
log.Printf("GetConfigMap failed: %v", err)
return nil, err
}
if actionType == ActionTypeAdd {
if len(templates) == 0 {
templates = make(map[string]string)
templates[updatedConfigMapPath] = updatedTemplateYaml
} else {
templates[updatedConfigMapPath] = updatedTemplateYaml
}
} else if actionType == ActionTypeEdit {
delete(templates, configMapPath)
templates[updatedConfigMapPath] = updatedTemplateYaml
} else if actionType == ActionTypeDelete {
delete(templates, updatedConfigMapPath)
}
templatesConfigMap := &apiv1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: updatedConfigMapName,
Namespace: updatedConfigMapNamespace,
Labels: TrialTemplateLabel,
},
Data: templates,
}
// If templates is empty delete Trial template configMap
if len(templates) == 0 {
err = k.katibClient.DeleteRuntimeObject(templatesConfigMap)
if err != nil {
log.Printf("DeleteRuntimeObject failed: %v", err)
return nil, err
}
// If len(templates) == 1 and adding template, we must create new ConfigMap
} else if len(templates) == 1 && actionType == ActionTypeAdd {
err = k.katibClient.CreateRuntimeObject(templatesConfigMap)
if err != nil {
log.Printf("CreateRuntimeObject failed: %v", err)
return nil, err
}
// Otherwise updating configMap
} else {
err = k.katibClient.UpdateRuntimeObject(templatesConfigMap)
if err != nil {
log.Printf("UpdateRuntimeObject failed: %v", err)
return nil, err
}
}
newTemplates, err := k.getTrialTemplatesViewList(r)
if err != nil {
log.Printf("getTrialTemplatesViewList: %v", err)
return nil, err
}
return newTemplates, nil
}
func getNodeString(block *Block) string {
var nodeString string
switch block.Type {
case "convolution":
nodeString += block.Param.FilterSize + "x" + block.Param.FilterSize
nodeString += " conv\n"
nodeString += block.Param.FilterSize + " channels"
case "separable_convolution":
nodeString += block.Param.FilterSize + "x" + block.Param.FilterSize
nodeString += " sep_conv\n"
nodeString += block.Param.FilterSize + " channels"
case "depthwise_convolution":
nodeString += block.Param.FilterSize + "x" + block.Param.FilterSize
nodeString += " depth_conv\n"
case "reduction":
// TODO: Need to be fixed
nodeString += "3x3 max_pooling"
}
return strconv.Quote(nodeString)
}
func generateNNImage(architecture string, decoder string) string {
var architectureInt [][]int
if err := json.Unmarshal([]byte(architecture), &architectureInt); err != nil {
panic(err)
}
/*
Always has num_layers, input_size, output_size and embeding
Embeding is a map: int to Parameter
Parameter has id, type, Option
Beforehand substitute all ' to " and wrap the string in `
*/
replacedDecoder := strings.Replace(decoder, `'`, `"`, -1)
var decoderParsed Decoder
err := json.Unmarshal([]byte(replacedDecoder), &decoderParsed)
if err != nil {
panic(err)
}
graphAst, _ := gographviz.ParseString(`digraph G {}`)
graph := gographviz.NewGraph()
if err := gographviz.Analyse(graphAst, graph); err != nil {
panic(err)
}
if err = graph.AddNode("G", "0", map[string]string{"label": strconv.Quote("Input")}); err != nil {
panic(err)
}
var i int
for i = 0; i < len(architectureInt); i++ {
if err = graph.AddNode("G", strconv.Itoa(i+1), map[string]string{"label": getNodeString(decoderParsed.Embedding[architectureInt[i][0]])}); err != nil {
panic(err)
}
if err = graph.AddEdge(strconv.Itoa(i), strconv.Itoa(i+1), true, nil); err != nil {
panic(err)
}
for j := 1; j < i+1; j++ {
if architectureInt[i][j] == 1 {
if err = graph.AddEdge(strconv.Itoa(j-1), strconv.Itoa(i+1), true, nil); err != nil {
panic(err)
}
}
}
}
if err = graph.AddNode("G", strconv.Itoa(i+1), map[string]string{"label": strconv.Quote("GlobalAvgPool")}); err != nil {
panic(err)
}
if err = graph.AddEdge(strconv.Itoa(i), strconv.Itoa(i+1), true, nil); err != nil {
panic(err)
}
if err = graph.AddNode("G", strconv.Itoa(i+2), map[string]string{"label": strconv.Quote("FullConnect\nSoftmax")}); err != nil {
panic(err)
}
if err = graph.AddEdge(strconv.Itoa(i+1), strconv.Itoa(i+2), true, nil); err != nil {
panic(err)
}
if err = graph.AddNode("G", strconv.Itoa(i+3), map[string]string{"label": strconv.Quote("Output")}); err != nil {
panic(err)
}
if err = graph.AddEdge(strconv.Itoa(i+2), strconv.Itoa(i+3), true, nil); err != nil {
panic(err)
}
s := graph.String()
return s
}