This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
workflow.go
232 lines (194 loc) · 7.17 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
package v1alpha1
import (
"bytes"
"encoding/json"
"k8s.io/apimachinery/pkg/types"
"github.com/golang/protobuf/jsonpb"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/core"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const StartNodeID = "start-node"
const EndNodeID = "end-node"
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// FlyteWorkflow: represents one Execution Workflow object
type FlyteWorkflow struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
*WorkflowSpec `json:"spec"`
Inputs *Inputs `json:"inputs,omitempty"`
ExecutionID ExecutionID `json:"executionId"`
Tasks map[TaskID]*TaskSpec `json:"tasks"`
SubWorkflows map[WorkflowID]*WorkflowSpec `json:"subWorkflows,omitempty"`
// StartTime before the system will actively try to mark it failed and kill associated containers.
// Value must be a positive integer.
// +optional
ActiveDeadlineSeconds *int64 `json:"activeDeadlineSeconds,omitempty"`
// Specifies the time when the workflow has been accepted into the system.
AcceptedAt *metav1.Time `json:"acceptedAt,omitEmpty"`
// ServiceAccountName is the name of the ServiceAccount to use to run this pod.
// More info: https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/
// +optional
ServiceAccountName string `json:"serviceAccountName,omitempty" protobuf:"bytes,8,opt,name=serviceAccountName"`
// Status is the only mutable section in the workflow. It holds all the execution information
Status WorkflowStatus `json:"status,omitempty"`
}
var FlyteWorkflowGVK = SchemeGroupVersion.WithKind(FlyteWorkflowKind)
func (in *FlyteWorkflow) NewControllerRef() metav1.OwnerReference {
// TODO Open Issue - https://github.com/kubernetes/client-go/issues/308
// For some reason the CRD does not have the GVK correctly populated. So we will fake it.
if len(in.GroupVersionKind().Group) == 0 || len(in.GroupVersionKind().Kind) == 0 || len(in.GroupVersionKind().Version) == 0 {
return *metav1.NewControllerRef(in, FlyteWorkflowGVK)
}
return *metav1.NewControllerRef(in, in.GroupVersionKind())
}
func (in *FlyteWorkflow) GetTask(id TaskID) (ExecutableTask, error) {
t, ok := in.Tasks[id]
if !ok {
return nil, errors.Errorf("Unable to find task with Id [%v]", id)
}
return t, nil
}
func (in *FlyteWorkflow) GetExecutionStatus() ExecutableWorkflowStatus {
return &in.Status
}
func (in *FlyteWorkflow) GetK8sWorkflowID() types.NamespacedName {
return types.NamespacedName{
Name: in.GetName(),
Namespace: in.GetNamespace(),
}
}
func (in *FlyteWorkflow) GetExecutionID() ExecutionID {
return in.ExecutionID
}
func (in *FlyteWorkflow) FindSubWorkflow(subID WorkflowID) ExecutableSubWorkflow {
s, ok := in.SubWorkflows[subID]
if !ok {
return nil
}
return s
}
func (in *FlyteWorkflow) GetNodeExecutionStatus(id NodeID) ExecutableNodeStatus {
return in.Status.GetNodeExecutionStatus(id)
}
func (in *FlyteWorkflow) GetServiceAccountName() string {
return in.ServiceAccountName
}
type Inputs struct {
*core.LiteralMap
}
func (in *Inputs) UnmarshalJSON(b []byte) error {
in.LiteralMap = &core.LiteralMap{}
return jsonpb.Unmarshal(bytes.NewReader(b), in.LiteralMap)
}
func (in *Inputs) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
if err := marshaler.Marshal(&buf, in.LiteralMap); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Inputs) DeepCopyInto(out *Inputs) {
*out = *in
// We do not manipulate the object, so its ok
// Once we figure out the autogenerate story we can replace this
}
type Connections struct {
DownstreamEdges map[NodeID][]NodeID
UpstreamEdges map[NodeID][]NodeID
}
func (in *Connections) UnmarshalJSON(b []byte) error {
in.DownstreamEdges = map[NodeID][]NodeID{}
err := json.Unmarshal(b, &in.DownstreamEdges)
if err != nil {
return err
}
in.UpstreamEdges = map[NodeID][]NodeID{}
for from, nodes := range in.DownstreamEdges {
for _, to := range nodes {
if _, ok := in.UpstreamEdges[to]; !ok {
in.UpstreamEdges[to] = []NodeID{}
}
in.UpstreamEdges[to] = append(in.UpstreamEdges[to], from)
}
}
return nil
}
func (in *Connections) MarshalJSON() ([]byte, error) {
return json.Marshal(in.DownstreamEdges)
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Connections) DeepCopyInto(out *Connections) {
*out = *in
// We do not manipulate the object, so its ok
// Once we figure out the autogenerate story we can replace this
}
// WorkflowSpec is the spec for the actual Flyte Workflow (DAG)
type WorkflowSpec struct {
ID WorkflowID `json:"id"`
Nodes map[NodeID]*NodeSpec `json:"nodes"`
// Defines the set of connections (both data dependencies and execution dependencies) that the graph is
// formed of. The execution engine will respect and follow these connections as it determines which nodes
// can and should be executed.
Connections Connections `json:"connections"`
// Defines a single node to execute in case the system determined the Workflow has failed.
OnFailure *NodeSpec `json:"onFailure,omitempty"`
// Defines the declaration of the outputs types and names this workflow is expected to generate.
Outputs *OutputVarMap `json:"outputs,omitempty"`
// Defines the data links used to construct the final outputs of the workflow. Bindings will typically
// refer to specific outputs of a subset of the nodes executed in the Workflow. When executing the end-node,
// the execution engine will traverse these bindings and assemble the final set of outputs of the workflow.
OutputBindings []*Binding `json:"outputBindings,omitempty"`
}
func (in *WorkflowSpec) StartNode() ExecutableNode {
n, ok := in.Nodes[StartNodeID]
if !ok {
return nil
}
return n
}
func (in *WorkflowSpec) GetID() WorkflowID {
return in.ID
}
func (in *WorkflowSpec) FromNode(name NodeID) ([]NodeID, error) {
if _, ok := in.Nodes[name]; !ok {
return nil, errors.Errorf("Bad Node [%v], is not defined in the Workflow [%v]", name, in.ID)
}
downstreamNodes := in.Connections.DownstreamEdges[name]
return downstreamNodes, nil
}
func (in *WorkflowSpec) GetOutputs() *OutputVarMap {
return in.Outputs
}
func (in *WorkflowSpec) GetNode(nodeID NodeID) (ExecutableNode, bool) {
n, ok := in.Nodes[nodeID]
return n, ok
}
func (in *WorkflowSpec) GetConnections() *Connections {
return &in.Connections
}
func (in *WorkflowSpec) GetOutputBindings() []*Binding {
return in.OutputBindings
}
func (in *WorkflowSpec) GetOnFailureNode() ExecutableNode {
if in.OnFailure == nil {
return nil
}
return in.OnFailure
}
func (in *WorkflowSpec) GetNodes() []NodeID {
nodeIds := make([]NodeID, 0, len(in.Nodes))
for id := range in.Nodes {
nodeIds = append(nodeIds, id)
}
return nodeIds
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// FlyteWorkflowList is a list of FlyteWorkflow resources
type FlyteWorkflowList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`
Items []FlyteWorkflow `json:"items"`
}