forked from secretflow/kuscia
/
adapter.go
482 lines (416 loc) · 14.3 KB
/
adapter.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// Copyright 2023 Ant Group Co., Ltd.
//
// 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 adapter
import (
"encoding/json"
"fmt"
"strconv"
"google.golang.org/protobuf/types/known/structpb"
corev1 "k8s.io/api/core/v1"
"github.com/secretflow/kuscia/pkg/common"
kusciaapisv1alpha1 "github.com/secretflow/kuscia/pkg/crd/apis/kuscia/v1alpha1"
kuscialistersv1alpha1 "github.com/secretflow/kuscia/pkg/crd/listers/kuscia/v1alpha1"
"github.com/secretflow/kuscia/proto/api/v1/interconn"
)
const version = "2.0.0"
// InterConnJobInfo defines interconn job info.
type InterConnJobInfo struct {
DAG *interconn.DAG `json:"dag"`
Config *interconn.Config `json:"config"`
JodID string `json:"job_id"`
FlowID string `json:"flow_id"`
}
// JobParamsResources defines resources of job params.
type JobParamsResources struct {
Disk float64 `json:"disk"`
Memory float64 `json:"memory"`
CPU float64 `json:"cpu"`
}
// GenerateInterConnJobInfoFrom generates interconn job info from kuscia job.
func GenerateInterConnJobInfoFrom(kusciaJob *kusciaapisv1alpha1.KusciaJob, appImageLister kuscialistersv1alpha1.AppImageLister) (*InterConnJobInfo, error) {
jobInfo := &InterConnJobInfo{
DAG: &interconn.DAG{},
Config: &interconn.Config{},
}
// componentName:value
taskParamsCommonAttr := make(map[string]interface{})
// index:componentName:value
taskParamsHostAttr := make(map[string]interface{})
// index:componentName:value
taskParamsGuestAttr := make(map[string]interface{})
// index:componentName:value
taskParamsArbiterAttr := make(map[string]interface{})
for _, task := range kusciaJob.Spec.Tasks {
taskInputConfig := &interconn.TaskInputConfig{}
err := json.Unmarshal([]byte(task.TaskInputConfig), taskInputConfig)
if err != nil {
return nil, fmt.Errorf("unmarshal task %v taskInputConfig failed, %v", task.Alias, err)
}
aimg, err := appImageLister.Get(task.AppImage)
if err != nil {
return nil, fmt.Errorf("failed to get appimage %v, %v", task.AppImage, err)
}
imageVersion := aimg.Spec.Image.Tag
if imageVersion == "" {
imageVersion = "latest"
}
cpt := &interconn.Component{
Code: task.AppImage,
Name: taskInputConfig.Name,
ModuleName: taskInputConfig.ModuleName,
Version: aimg.Spec.Image.Tag,
Input: taskInputConfig.Input,
Output: taskInputConfig.Output,
}
jobInfo.DAG.Components = append(jobInfo.DAG.Components, cpt)
if taskInputConfig.Initiator != nil && jobInfo.Config.Initiator == nil {
jobInfo.Config.Initiator = &interconn.ConfigInitiator{
Role: taskInputConfig.Initiator.Role,
NodeId: taskInputConfig.Initiator.NodeId,
}
}
if taskInputConfig.Role != nil && jobInfo.Config.Role == nil {
jobInfo.Config.Role = &interconn.ConfigRole{
Arbiter: taskInputConfig.Role.Arbiter,
Host: taskInputConfig.Role.Host,
Guest: taskInputConfig.Role.Guest,
}
}
if jobInfo.Config.JobParams == nil {
jobParams, err := buildJobParams(taskInputConfig.Role)
if err != nil {
return nil, fmt.Errorf("build job params failed, %v", err)
}
jobInfo.Config.JobParams = jobParams
}
parseTaskParamsFromTaskInputConfig(taskInputConfig, taskParamsCommonAttr, taskParamsHostAttr, taskParamsGuestAttr, taskParamsArbiterAttr)
}
if err := buildInterConnJobConfig(jobInfo, taskParamsCommonAttr, taskParamsHostAttr, taskParamsGuestAttr, taskParamsArbiterAttr); err != nil {
return nil, err
}
jobInfo.DAG.Version = version
jobInfo.FlowID = kusciaJob.Spec.FlowID
jobInfo.JodID = kusciaJob.Name
return jobInfo, nil
}
// GenerateKusciaJobFrom generates kuscia job from interconn job info.
func GenerateKusciaJobFrom(jobInfo *InterConnJobInfo) (*kusciaapisv1alpha1.KusciaJob, error) {
if jobInfo == nil || jobInfo.Config == nil || jobInfo.DAG == nil {
return nil, fmt.Errorf("dag and config of interconn job info can't be empty")
}
kusciaJob := &kusciaapisv1alpha1.KusciaJob{}
kusciaJob.Name = jobInfo.JodID
kusciaJob.Labels = map[string]string{
common.LabelInterConnProtocolType: string(kusciaapisv1alpha1.InterConnBFIA),
common.LabelJobStage: string(kusciaapisv1alpha1.JobCreateStage),
}
kusciaJob.Spec.FlowID = jobInfo.FlowID
taskMaxParallelism := 2
kusciaJob.Spec.MaxParallelism = &taskMaxParallelism
kusciaJob.Spec.ScheduleMode = kusciaapisv1alpha1.KusciaJobScheduleModeStrict
if jobInfo.Config.Initiator == nil || jobInfo.Config.Initiator.NodeId == "" {
return nil, fmt.Errorf("interconn job config initiator can't be empty")
}
kusciaJob.Spec.Initiator = jobInfo.Config.Initiator.NodeId
deps := buildKusciaJobDependenciesFrom(jobInfo)
tolerable := false
var jobTasks []kusciaapisv1alpha1.KusciaTaskTemplate
for idx := range jobInfo.DAG.Components {
cpt := jobInfo.DAG.Components[idx]
if err := validateComponentCode(cpt.Code); err != nil {
return nil, fmt.Errorf("validate appImage failed, %v", err)
}
taskInputConfig, parties, err := buildTaskInputConfigAndPartiesFrom(cpt, jobInfo.Config)
if err != nil {
return nil, fmt.Errorf("generate taskInputConfig failed, %v", err)
}
jobTasks = append(jobTasks, kusciaapisv1alpha1.KusciaTaskTemplate{
Alias: cpt.Name,
Dependencies: deps[cpt.Name],
Tolerable: &tolerable,
AppImage: cpt.Code,
TaskInputConfig: taskInputConfig,
Parties: parties,
})
}
kusciaJob.Spec.Tasks = jobTasks
return kusciaJob, nil
}
// buildInterConnJobParamsResources builds resources for job params.
func buildInterConnJobParamsResources(appImageLister kuscialistersv1alpha1.AppImageLister, role, appName string) (*JobParamsResources, error) {
appImage, err := appImageLister.Get(appName)
if err != nil {
return nil, fmt.Errorf("failed to get appimage %v, %v", appImage, err)
}
calculateResource := func(request corev1.ResourceList) (float64, float64, float64) {
if request == nil {
return -1, -1, -1
}
var (
disk float64
memory float64
cpu float64
)
if request.Storage() != nil {
disk += request.Storage().AsApproximateFloat64()
}
if request.Memory() != nil {
memory += request.Memory().AsApproximateFloat64()
}
if request.Memory() != nil {
cpu += request.Cpu().AsApproximateFloat64()
}
return disk, memory, cpu
}
var (
resourceDisk float64 = -1
resourceMemory float64 = -1
resourceCPU float64 = -1
)
foundTpl := false
for _, deployTpl := range appImage.Spec.DeployTemplates {
if deployTpl.Role == role {
foundTpl = true
for _, container := range deployTpl.Spec.Containers {
resourceDisk, resourceMemory, resourceCPU = calculateResource(container.Resources.Requests)
}
break
}
}
if !foundTpl && len(appImage.Spec.DeployTemplates) > 0 {
for _, container := range appImage.Spec.DeployTemplates[0].Spec.Containers {
resourceDisk, resourceMemory, resourceCPU = calculateResource(container.Resources.Requests)
}
}
return &JobParamsResources{
Disk: resourceDisk,
Memory: resourceMemory,
CPU: resourceCPU,
}, nil
}
// buildJobParams builds job params.
func buildJobParams(role *interconn.ConfigRole) (*interconn.ConfigParams, error) {
jobParams := &interconn.ConfigParams{}
roleMap := make(map[string]interface{})
emptyStruct, _ := structpb.NewStruct(roleMap)
for idx := range role.Arbiter {
a, err := structpb.NewStruct(roleMap)
if err != nil {
return nil, err
}
a.Fields[strconv.Itoa(idx)] = structpb.NewStructValue(emptyStruct)
jobParams.Arbiter = a
}
for idx := range role.Host {
h, err := structpb.NewStruct(roleMap)
if err != nil {
return nil, err
}
h.Fields[strconv.Itoa(idx)] = structpb.NewStructValue(emptyStruct)
jobParams.Host = h
}
for idx := range role.Guest {
g, err := structpb.NewStruct(roleMap)
if err != nil {
return nil, err
}
g.Fields[strconv.Itoa(idx)] = structpb.NewStructValue(emptyStruct)
jobParams.Guest = g
}
return jobParams, nil
}
// parseTaskParamsFromTaskInputConfig parses task params from taskInputConfig.
func parseTaskParamsFromTaskInputConfig(taskInputConfig *interconn.TaskInputConfig, commonAttr, hostAttr, guestAttr, arbiterAttr map[string]interface{}) {
if taskInputConfig.TaskParams.Common != nil {
commonAttr[taskInputConfig.Name] = taskInputConfig.TaskParams.Common.AsMap()
}
setAttr := func(name string, input, output map[string]interface{}) {
for index, inputAttr := range input {
outputAttr, exist := output[index]
if !exist {
output[index] = map[string]interface{}{
name: inputAttr,
}
} else {
attrMap := outputAttr.(map[string]interface{})
attrMap[name] = inputAttr
output[index] = attrMap
}
}
}
if taskInputConfig.TaskParams.Host != nil {
setAttr(taskInputConfig.Name, taskInputConfig.TaskParams.Host.AsMap(), hostAttr)
}
if taskInputConfig.TaskParams.Guest != nil {
setAttr(taskInputConfig.Name, taskInputConfig.TaskParams.Guest.AsMap(), guestAttr)
}
if taskInputConfig.TaskParams.Arbiter != nil {
setAttr(taskInputConfig.Name, taskInputConfig.TaskParams.Arbiter.AsMap(), arbiterAttr)
}
}
// buildInterConnJobConfig builds interconn job config.
func buildInterConnJobConfig(jobSpec *InterConnJobInfo, commonAttr, hostAttr, guestAttr, arbiterAttr map[string]interface{}) error {
taskParams := &interconn.ConfigParams{}
jobSpec.Config.Version = version
if commonAttr != nil {
common, err := structpb.NewStruct(commonAttr)
if err != nil {
return fmt.Errorf("new struct for task_params common atrribute failed, %v", err)
}
taskParams.Common = common
}
if hostAttr != nil {
host, err := structpb.NewStruct(hostAttr)
if err != nil {
return fmt.Errorf("new struct for task_params host atrribute failed, %v", err)
}
taskParams.Host = host
}
if guestAttr != nil {
guest, err := structpb.NewStruct(guestAttr)
if err != nil {
return fmt.Errorf("new struct for task_params guest atrribute failed, %v", err)
}
taskParams.Guest = guest
}
if arbiterAttr != nil {
arbiter, err := structpb.NewStruct(arbiterAttr)
if err != nil {
return fmt.Errorf("new struct for task_params arbiter atrribute failed, %v", err)
}
taskParams.Arbiter = arbiter
}
jobSpec.Config.TaskParams = taskParams
return nil
}
// buildKusciaJobDependenciesFrom builds kuscia job dependencies from interconn job info.
func buildKusciaJobDependenciesFrom(jobInfo *InterConnJobInfo) map[string][]string {
deps := make(map[string][]string)
for _, t := range jobInfo.DAG.Components {
if len(t.Input) == 0 {
continue
}
for _, i := range t.Input {
loop:
for _, tt := range jobInfo.DAG.Components {
if t.Name == tt.Name && t.ModuleName == tt.ModuleName {
continue
}
for _, o := range tt.Output {
if i.Type == o.Type && i.Key == fmt.Sprintf("%s.%s", tt.Name, o.Key) {
deps[t.Name] = append(deps[t.Name], tt.Name)
break loop
}
}
}
}
}
return deps
}
// validateComponentCode validates component code.
func validateComponentCode(code string) error {
if code == "" {
return fmt.Errorf("component code can't be empty")
}
return nil
}
// buildTaskInputConfigAndPartiesFrom builds taskInputConfig and parties from interconn job component and config.
func buildTaskInputConfigAndPartiesFrom(cpt *interconn.Component, config *interconn.Config) (string, []kusciaapisv1alpha1.Party, error) {
taskInputConfig := &interconn.TaskInputConfig{
Name: cpt.Name,
ModuleName: cpt.ModuleName,
Input: cpt.Input,
Output: cpt.Output,
Role: config.Role,
Initiator: config.Initiator,
TaskParams: &interconn.ConfigParams{},
}
var parties []kusciaapisv1alpha1.Party
arbiter, partiesRet, err := parseTaskRoleParams(config.TaskParams.Arbiter, config.Role.Arbiter, cpt.Name, "arbiter")
if err != nil {
return "", nil, err
}
taskInputConfig.TaskParams.Arbiter = arbiter
parties = append(parties, partiesRet...)
host, partiesRet, err := parseTaskRoleParams(config.TaskParams.Host, config.Role.Host, cpt.Name, "host")
if err != nil {
return "", nil, err
}
taskInputConfig.TaskParams.Host = host
parties = append(parties, partiesRet...)
guest, partiesRet, err := parseTaskRoleParams(config.TaskParams.Guest, config.Role.Guest, cpt.Name, "guest")
if err != nil {
return "", nil, err
}
taskInputConfig.TaskParams.Guest = guest
parties = append(parties, partiesRet...)
commonTaskParams := make(map[string]interface{})
if config.TaskParams.Common != nil {
for cptName, content := range config.TaskParams.Common.Fields {
if cptName == cpt.Name {
for k, v := range content.GetStructValue().AsMap() {
commonTaskParams[k] = v
}
break
}
}
if len(commonTaskParams) > 0 {
s, err := structpb.NewStruct(commonTaskParams)
if err != nil {
return "", nil, err
}
taskInputConfig.TaskParams.Common = s
}
}
retS, err := json.Marshal(taskInputConfig)
if err != nil {
return "", nil, err
}
return string(retS), parties, nil
}
// parseTaskRoleParams parses task role params.
func parseTaskRoleParams(roleParams *structpb.Struct, roles []string, componentName, role string) (*structpb.Struct, []kusciaapisv1alpha1.Party, error) {
if roleParams == nil {
return nil, nil, nil
}
var parties []kusciaapisv1alpha1.Party
paramsMap := make(map[string]interface{})
for roleIndex, f := range roleParams.Fields {
for cptName, content := range f.GetStructValue().AsMap() {
if cptName == componentName {
paramsMap[roleIndex] = content
index, err := strconv.Atoi(roleIndex)
if err != nil {
return nil, nil, fmt.Errorf("role index should be number string format, %v", err)
}
if index+1 > len(roles) {
return nil, nil, fmt.Errorf("role index %v should be less than role %v length", index+1, roles)
}
parties = append(parties, kusciaapisv1alpha1.Party{
DomainID: roles[index],
Role: role,
})
break
}
}
}
if len(paramsMap) == 0 {
return nil, nil, nil
}
s, err := structpb.NewStruct(paramsMap)
if err != nil {
return nil, nil, err
}
return s, parties, nil
}