-
Notifications
You must be signed in to change notification settings - Fork 538
/
workflow.go
322 lines (269 loc) · 9.8 KB
/
workflow.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
// This package converts the output of the compiler into a K8s resource for propeller to execute.
package k8s
import (
"fmt"
"hash/fnv"
"strings"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/flyteorg/flyte/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flyte/flytepropeller/pkg/apis/flyteworkflow/v1alpha1"
"github.com/flyteorg/flyte/flytepropeller/pkg/compiler/common"
"github.com/flyteorg/flyte/flytepropeller/pkg/compiler/errors"
"github.com/flyteorg/flyte/flytepropeller/pkg/utils"
)
const (
// Labels are set on the FlyteWorkflow CRD to aid downstream processing
// The FlyteWorkflow domain according to registration ownership
DomainLabel = "domain"
// A concatenation of project, domain, workflow name, and a unique ID
ExecutionIDLabel = "execution-id"
// The FlyteWorkflow project according to registration ownership
ProjectLabel = "project"
// Shard keys are used during FlytePropeller sharding, this value is set to a hash of the FlyteWorkflow ExecutionID.
// The pseudo-random unique ID component means this value is deterministic for the same ExecutionID, but will vary
// across executions of the same workflow.
ShardKeyLabel = "shard-key"
// The fully qualified FlyteWorkflow name
WorkflowNameLabel = "workflow-name"
)
func requiresInputs(w *core.WorkflowTemplate) bool {
if w == nil || w.GetInterface() == nil || w.GetInterface().GetInputs() == nil ||
w.GetInterface().GetInputs().Variables == nil {
return false
}
return len(w.GetInterface().GetInputs().Variables) > 0
}
// Note: Update WorkflowNameFromID for any change made to WorkflowIDAsString
func WorkflowIDAsString(id *core.Identifier) string {
b := strings.Builder{}
_, err := b.WriteString(id.Project)
if err != nil {
return ""
}
_, err = b.WriteRune(':')
if err != nil {
return ""
}
_, err = b.WriteString(id.Domain)
if err != nil {
return ""
}
_, err = b.WriteRune(':')
if err != nil {
return ""
}
_, err = b.WriteString(id.Name)
if err != nil {
return ""
}
return b.String()
}
func WorkflowNameFromID(id string) string {
tokens := strings.Split(id, ":")
if len(tokens) != 3 {
return ""
}
return tokens[2]
}
func buildFlyteWorkflowSpec(wf *core.CompiledWorkflow, tasks []*core.CompiledTask, errs errors.CompileErrors) (
spec *v1alpha1.WorkflowSpec, err error) {
wf.Template.Interface = StripInterfaceTypeMetadata(wf.Template.Interface)
var failureN *v1alpha1.NodeSpec
if n := wf.Template.GetFailureNode(); n != nil {
nodes, ok := buildNodeSpec(n, tasks, errs.NewScope())
if !ok {
return nil, errs
}
failureN = nodes[0]
}
nodes, _ := buildNodes(wf.Template.GetNodes(), tasks, errs.NewScope())
if errs.HasErrors() {
return nil, errs
}
outputBindings := make([]*v1alpha1.Binding, 0, len(wf.Template.Outputs))
for _, b := range wf.Template.Outputs {
outputBindings = append(outputBindings, &v1alpha1.Binding{
Binding: b,
})
}
var outputs *v1alpha1.OutputVarMap
if wf.Template.GetInterface() != nil {
outputs = &v1alpha1.OutputVarMap{VariableMap: wf.Template.GetInterface().Outputs}
} else {
outputs = &v1alpha1.OutputVarMap{VariableMap: &core.VariableMap{}}
}
failurePolicy := v1alpha1.WorkflowOnFailurePolicy(core.WorkflowMetadata_FAIL_IMMEDIATELY)
if wf.Template != nil && wf.Template.Metadata != nil {
failurePolicy = v1alpha1.WorkflowOnFailurePolicy(wf.Template.Metadata.OnFailure)
}
connections := buildConnections(wf)
return &v1alpha1.WorkflowSpec{
ID: WorkflowIDAsString(wf.Template.Id),
OnFailure: failureN,
Nodes: nodes,
Outputs: outputs,
OutputBindings: outputBindings,
OnFailurePolicy: failurePolicy,
Connections: connections,
DeprecatedConnections: v1alpha1.DeprecatedConnections{
DownstreamEdges: connections.Downstream,
UpstreamEdges: connections.Upstream,
},
}, nil
}
func withSeparatorIfNotEmpty(value string) string {
if len(value) > 0 {
return fmt.Sprintf("%v-", value)
}
return ""
}
func generateName(wfID *core.Identifier, execID *core.WorkflowExecutionIdentifier) (
name string, generateName string, label string, project string, domain string, err error) {
if execID != nil {
return execID.Name, "", execID.Name, execID.Project, execID.Domain, nil
} else if wfID != nil {
wid := fmt.Sprintf("%v%v%v", withSeparatorIfNotEmpty(wfID.Project), withSeparatorIfNotEmpty(wfID.Domain), wfID.Name)
// TODO: this is a hack until we figure out how to restrict generated names. K8s has a limitation of 63 chars
wid = wid[:minInt(32, len(wid))]
return "", fmt.Sprintf("%v-", wid), wid, wfID.Project, wfID.Domain, nil
} else {
return "", "", "", "", "", fmt.Errorf("expected param not set. wfID or execID must be non-nil values")
}
}
// BuildFlyteWorkflow builds v1alpha1.FlyteWorkflow resource. Returned error, if not nil, is of type errors.CompilerErrors.
func BuildFlyteWorkflow(wfClosure *core.CompiledWorkflowClosure, inputs *core.LiteralMap,
executionID *core.WorkflowExecutionIdentifier, namespace string) (*v1alpha1.FlyteWorkflow, error) {
errs := errors.NewCompileErrors()
if wfClosure == nil {
errs.Collect(errors.NewValueRequiredErr("root", "wfClosure"))
return nil, errs
}
wf := wfClosure.Primary.Template
tasks := wfClosure.Tasks
// Fill in inputs in the start node.
if inputs != nil {
if ok := validateInputs(common.StartNodeID, wf.GetInterface(), *inputs, errs.NewScope()); !ok {
return nil, errs
}
} else if requiresInputs(wf) {
errs.Collect(errors.NewValueRequiredErr("root", "inputs"))
return nil, errs
}
for _, t := range tasks {
t.Template.Interface = StripInterfaceTypeMetadata(t.Template.Interface)
}
primarySpec, err := buildFlyteWorkflowSpec(wfClosure.Primary, tasks, errs.NewScope())
if err != nil {
errs.Collect(errors.NewWorkflowBuildError(err))
return nil, errs
}
subwfs := make(map[v1alpha1.WorkflowID]*v1alpha1.WorkflowSpec, len(wfClosure.SubWorkflows))
for _, subWf := range wfClosure.SubWorkflows {
spec, err := buildFlyteWorkflowSpec(subWf, tasks, errs.NewScope())
if err != nil {
errs.Collect(errors.NewWorkflowBuildError(err))
} else {
subwfs[subWf.Template.Id.String()] = spec
}
}
if errs.HasErrors() {
return nil, errs
}
interruptible := false
if wf.GetMetadataDefaults() != nil {
interruptible = wf.GetMetadataDefaults().GetInterruptible()
}
obj := &v1alpha1.FlyteWorkflow{
TypeMeta: v1.TypeMeta{
Kind: v1alpha1.FlyteWorkflowKind,
APIVersion: v1alpha1.SchemeGroupVersion.String(),
},
ObjectMeta: v1.ObjectMeta{
Namespace: namespace,
Labels: map[string]string{},
},
Inputs: &v1alpha1.Inputs{LiteralMap: inputs},
WorkflowSpec: primarySpec,
SubWorkflows: subwfs,
Tasks: buildTasks(tasks, errs.NewScope()),
NodeDefaults: v1alpha1.NodeDefaults{Interruptible: interruptible},
}
name, generatedName, label, project, domain, err := generateName(wf.GetId(), executionID)
if err != nil {
errs.Collect(errors.NewWorkflowBuildError(err))
}
obj.ObjectMeta.Name = name
obj.ObjectMeta.GenerateName = generatedName
obj.ObjectMeta.Labels[ExecutionIDLabel] = label
obj.ObjectMeta.Labels[ProjectLabel] = project
obj.ObjectMeta.Labels[DomainLabel] = domain
obj.ObjectMeta.Labels[WorkflowNameLabel] = utils.SanitizeLabelValue(WorkflowNameFromID(primarySpec.ID))
h := fnv.New32a()
h.Write([]byte(label))
hash := h.Sum32() % v1alpha1.ShardKeyspaceSize
obj.ObjectMeta.Labels[ShardKeyLabel] = fmt.Sprint(hash)
if obj.Nodes == nil || obj.Connections.Downstream == nil {
// If we come here, we'd better have an error generated earlier. Otherwise, add one to make sure build fails.
if !errs.HasErrors() {
errs.Collect(errors.NewWorkflowBuildError(fmt.Errorf("failed to build workflow for unknown reason." +
" Make sure to pass this workflow through the compiler first")))
}
} else if startingNodes, err := obj.FromNode(v1alpha1.StartNodeID); err == nil && len(startingNodes) == 0 {
errs.Collect(errors.NewWorkflowHasNoEntryNodeErr(wf.GetId().String()))
} else if err != nil {
errs.Collect(errors.NewWorkflowBuildError(err))
}
if errs.HasErrors() {
return nil, errs
}
return obj, nil
}
func toMapOfLists(connections map[string]*core.ConnectionSet_IdList) map[string][]string {
res := make(map[string][]string, len(connections))
for key, val := range connections {
res[key] = val.Ids
}
return res
}
func buildConnections(w *core.CompiledWorkflow) v1alpha1.Connections {
res := v1alpha1.Connections{}
res.Downstream = toMapOfLists(w.GetConnections().GetDownstream())
res.Upstream = toMapOfLists(w.GetConnections().GetUpstream())
return res
}
type WfClosureCrdFields struct {
*v1alpha1.WorkflowSpec `json:"spec"`
SubWorkflows map[v1alpha1.WorkflowID]*v1alpha1.WorkflowSpec `json:"subWorkflows,omitempty"`
Tasks map[v1alpha1.TaskID]*v1alpha1.TaskSpec `json:"tasks"`
}
func BuildWfClosureCrdFields(wfClosure *core.CompiledWorkflowClosure) (*WfClosureCrdFields, error) {
errs := errors.NewCompileErrors()
if wfClosure == nil {
errs.Collect(errors.NewValueRequiredErr("root", "wfClosure"))
return nil, errs
}
primarySpec, err := buildFlyteWorkflowSpec(wfClosure.Primary, wfClosure.Tasks, errs.NewScope())
if err != nil {
errs.Collect(errors.NewWorkflowBuildError(err))
return nil, errs
}
for _, t := range wfClosure.Tasks {
t.Template.Interface = StripInterfaceTypeMetadata(t.Template.Interface)
}
tasks := buildTasks(wfClosure.Tasks, errs.NewScope())
subwfs := make(map[v1alpha1.WorkflowID]*v1alpha1.WorkflowSpec, len(wfClosure.SubWorkflows))
for _, subWf := range wfClosure.SubWorkflows {
spec, err := buildFlyteWorkflowSpec(subWf, wfClosure.Tasks, errs.NewScope())
if err != nil {
errs.Collect(errors.NewWorkflowBuildError(err))
} else {
subwfs[subWf.Template.Id.String()] = spec
}
}
wfClosureCrdFields := &WfClosureCrdFields{
WorkflowSpec: primarySpec,
SubWorkflows: subwfs,
Tasks: tasks,
}
return wfClosureCrdFields, nil
}