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 62
/
propeller_executor.go
295 lines (260 loc) · 12.2 KB
/
propeller_executor.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
package impl
import (
"context"
interfaces2 "github.com/lyft/flyteadmin/pkg/executioncluster/interfaces"
"github.com/lyft/flyteadmin/pkg/common"
"github.com/lyft/flyteadmin/pkg/executioncluster"
runtimeInterfaces "github.com/lyft/flyteadmin/pkg/runtime/interfaces"
"github.com/lyft/flyteadmin/pkg/workflowengine/interfaces"
"github.com/lyft/flytestdlib/promutils"
"github.com/prometheus/client_golang/prometheus"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/lyft/flytestdlib/logger"
"github.com/lyft/flyteadmin/pkg/errors"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/core"
"github.com/lyft/flytepropeller/pkg/apis/flyteworkflow/v1alpha1"
"github.com/lyft/flytepropeller/pkg/compiler/transformers/k8s"
"google.golang.org/grpc/codes"
k8_api_err "k8s.io/apimachinery/pkg/api/errors"
)
var deletePropagationBackground = v1.DeletePropagationBackground
type propellerMetrics struct {
Scope promutils.Scope
WorkflowBuildSuccess prometheus.Counter
WorkflowBuildFailure prometheus.Counter
InvalidExecutionID prometheus.Counter
ExecutionCreationSuccess prometheus.Counter
ExecutionCreationFailure prometheus.Counter
TerminateExecutionFailure prometheus.Counter
}
type FlytePropeller struct {
executionCluster interfaces2.ClusterInterface
builder interfaces.FlyteWorkflowInterface
roleNameKey string
metrics propellerMetrics
config runtimeInterfaces.NamespaceMappingConfiguration
}
type FlyteWorkflowBuilder struct{}
func (b *FlyteWorkflowBuilder) BuildFlyteWorkflow(
wfClosure *core.CompiledWorkflowClosure, inputs *core.LiteralMap, executionID *core.WorkflowExecutionIdentifier,
namespace string) (*v1alpha1.FlyteWorkflow, error) {
return k8s.BuildFlyteWorkflow(wfClosure, inputs, executionID, namespace)
}
func addMapValues(overrides map[string]string, flyteWfValues map[string]string) map[string]string {
if flyteWfValues == nil {
flyteWfValues = map[string]string{}
}
if overrides == nil {
return flyteWfValues
}
for label, value := range overrides {
flyteWfValues[label] = value
}
return flyteWfValues
}
func (c *FlytePropeller) addPermissions(launchPlan admin.LaunchPlan, flyteWf *v1alpha1.FlyteWorkflow) {
// Set role permissions based on launch plan Auth values.
// The branched-ness of this check is due to the presence numerous deprecated fields
var role string
if launchPlan.Spec.GetAuthRole() != nil && len(launchPlan.GetSpec().GetAuthRole().GetAssumableIamRole()) > 0 {
role = launchPlan.GetSpec().GetAuthRole().GetAssumableIamRole()
} else if launchPlan.GetSpec().GetAuth() != nil && len(launchPlan.GetSpec().GetAuth().GetAssumableIamRole()) > 0 {
role = launchPlan.GetSpec().GetAuth().GetAssumableIamRole()
} else if len(launchPlan.GetSpec().GetRole()) > 0 {
// Although deprecated, older launch plans may reference the role field instead of the Auth AssumableIamRole.
role = launchPlan.GetSpec().GetRole()
} else if launchPlan.GetSpec().GetAuthRole() != nil && len(launchPlan.GetSpec().GetAuthRole().GetKubernetesServiceAccount()) > 0 {
flyteWf.ServiceAccountName = launchPlan.GetSpec().GetAuthRole().GetKubernetesServiceAccount()
} else if launchPlan.GetSpec().GetAuth() != nil && len(launchPlan.GetSpec().GetAuth().GetKubernetesServiceAccount()) > 0 {
flyteWf.ServiceAccountName = launchPlan.GetSpec().GetAuth().GetKubernetesServiceAccount()
}
if len(role) > 0 {
if flyteWf.Annotations == nil {
flyteWf.Annotations = map[string]string{}
}
flyteWf.Annotations[c.roleNameKey] = role
}
}
func (c *FlytePropeller) ExecuteWorkflow(ctx context.Context, input interfaces.ExecuteWorkflowInput) (*interfaces.ExecutionInfo, error) {
if input.ExecutionID == nil {
c.metrics.InvalidExecutionID.Inc()
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "invalid execution id")
}
namespace := common.GetNamespaceName(c.config.GetNamespaceMappingConfig(), input.ExecutionID.GetProject(), input.ExecutionID.GetDomain())
flyteWf, err := c.builder.BuildFlyteWorkflow(&input.WfClosure, input.Inputs, input.ExecutionID, namespace)
if err != nil {
c.metrics.WorkflowBuildFailure.Inc()
logger.Infof(ctx, "failed to build the workflow [%+v] %v",
input.WfClosure.Primary.Template.Id, err)
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "failed to build the workflow [%+v] %v",
input.WfClosure.Primary.Template.Id, err)
}
c.metrics.WorkflowBuildSuccess.Inc()
// add the executionId so Propeller can send events back that are associated with the ID
flyteWf.ExecutionID = v1alpha1.WorkflowExecutionIdentifier{
WorkflowExecutionIdentifier: input.ExecutionID,
}
// add the acceptedAt timestamp so propeller can emit latency metrics.
acceptAtWrapper := v1.NewTime(input.AcceptedAt)
flyteWf.AcceptedAt = &acceptAtWrapper
c.addPermissions(input.Reference, flyteWf)
labels := addMapValues(input.Labels, flyteWf.Labels)
flyteWf.Labels = labels
annotations := addMapValues(input.Annotations, flyteWf.Annotations)
flyteWf.Annotations = annotations
if input.Reference.Spec.RawOutputDataConfig != nil {
flyteWf.RawOutputDataConfig = v1alpha1.RawOutputDataConfig{
RawOutputDataConfig: input.Reference.Spec.RawOutputDataConfig,
}
}
/*
TODO(katrogan): uncomment once propeller has updated the flyte workflow CRD.
queueingBudgetSeconds := int64(input.QueueingBudget.Seconds())
flyteWf.QueuingBudgetSeconds = &queueingBudgetSeconds
*/
executionTargetSpec := executioncluster.ExecutionTargetSpec{
Project: input.ExecutionID.Project,
Domain: input.ExecutionID.Domain,
Workflow: input.Reference.Spec.WorkflowId.Name,
LaunchPlan: input.Reference.Id.Name,
ExecutionID: input.ExecutionID.Name,
}
targetCluster, err := c.executionCluster.GetTarget(ctx, &executionTargetSpec)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "failed to create workflow in propeller %v", err)
}
_, err = targetCluster.FlyteClient.FlyteworkflowV1alpha1().FlyteWorkflows(namespace).Create(flyteWf)
if err != nil {
if !k8_api_err.IsAlreadyExists(err) {
logger.Debugf(ctx, "failed to create workflow [%+v] in cluster %s %v",
input.WfClosure.Primary.Template.Id, targetCluster.ID, err)
c.metrics.ExecutionCreationFailure.Inc()
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "failed to create workflow in propeller %v", err)
}
}
logger.Debugf(ctx, "Successfully created workflow execution [%+v]", input.WfClosure.Primary.Template.Id)
c.metrics.ExecutionCreationSuccess.Inc()
return &interfaces.ExecutionInfo{
Cluster: targetCluster.ID,
}, nil
}
func (c *FlytePropeller) ExecuteTask(ctx context.Context, input interfaces.ExecuteTaskInput) (*interfaces.ExecutionInfo, error) {
if input.ExecutionID == nil {
c.metrics.InvalidExecutionID.Inc()
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "invalid execution id")
}
namespace := common.GetNamespaceName(c.config.GetNamespaceMappingConfig(), input.ExecutionID.GetProject(), input.ExecutionID.GetDomain())
flyteWf, err := c.builder.BuildFlyteWorkflow(&input.WfClosure, input.Inputs, input.ExecutionID, namespace)
if err != nil {
c.metrics.WorkflowBuildFailure.Inc()
logger.Infof(ctx, "failed to build the workflow [%+v] %v",
input.WfClosure.Primary.Template.Id, err)
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "failed to build the workflow [%+v] %v",
input.WfClosure.Primary.Template.Id, err)
}
c.metrics.WorkflowBuildSuccess.Inc()
// add the executionId so Propeller can send events back that are associated with the ID
flyteWf.ExecutionID = v1alpha1.WorkflowExecutionIdentifier{
WorkflowExecutionIdentifier: input.ExecutionID,
}
// add the acceptedAt timestamp so propeller can emit latency metrics.
acceptAtWrapper := v1.NewTime(input.AcceptedAt)
flyteWf.AcceptedAt = &acceptAtWrapper
// Add execution roles from auth if any.
if input.Auth != nil && len(input.Auth.GetAssumableIamRole()) > 0 {
role := input.Auth.GetAssumableIamRole()
if flyteWf.Annotations == nil {
flyteWf.Annotations = map[string]string{}
}
flyteWf.Annotations[c.roleNameKey] = role
} else if input.Auth != nil && len(input.Auth.GetKubernetesServiceAccount()) > 0 {
flyteWf.ServiceAccountName = input.Auth.GetKubernetesServiceAccount()
}
labels := addMapValues(input.Labels, flyteWf.Labels)
flyteWf.Labels = labels
annotations := addMapValues(input.Annotations, flyteWf.Annotations)
flyteWf.Annotations = annotations
/*
TODO(katrogan): uncomment once propeller has updated the flyte workflow CRD.
queueingBudgetSeconds := int64(input.QueueingBudget.Seconds())
flyteWf.QueuingBudgetSeconds = &queueingBudgetSeconds
*/
executionTargetSpec := executioncluster.ExecutionTargetSpec{
Project: input.ExecutionID.Project,
Domain: input.ExecutionID.Domain,
Workflow: input.ReferenceName,
LaunchPlan: input.ReferenceName,
ExecutionID: input.ExecutionID.Name,
}
targetCluster, err := c.executionCluster.GetTarget(ctx, &executionTargetSpec)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "failed to create workflow in propeller %v", err)
}
_, err = targetCluster.FlyteClient.FlyteworkflowV1alpha1().FlyteWorkflows(namespace).Create(flyteWf)
if err != nil {
if !k8_api_err.IsAlreadyExists(err) {
logger.Debugf(ctx, "failed to create workflow [%+v] in cluster %s %v",
input.WfClosure.Primary.Template.Id, targetCluster.ID, err)
c.metrics.ExecutionCreationFailure.Inc()
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "failed to create workflow in propeller %v", err)
}
}
logger.Debugf(ctx, "Successfully created workflow execution [%+v]", input.WfClosure.Primary.Template.Id)
c.metrics.ExecutionCreationSuccess.Inc()
return &interfaces.ExecutionInfo{
Cluster: targetCluster.ID,
}, nil
}
func (c *FlytePropeller) TerminateWorkflowExecution(
ctx context.Context, input interfaces.TerminateWorkflowInput) error {
if input.ExecutionID == nil {
c.metrics.InvalidExecutionID.Inc()
return errors.NewFlyteAdminErrorf(codes.Internal, "invalid execution id")
}
namespace := common.GetNamespaceName(c.config.GetNamespaceMappingConfig(), input.ExecutionID.GetProject(), input.ExecutionID.GetDomain())
target, err := c.executionCluster.GetTarget(ctx, &executioncluster.ExecutionTargetSpec{
TargetID: input.Cluster,
})
if err != nil {
return errors.NewFlyteAdminErrorf(codes.Internal, err.Error())
}
err = target.FlyteClient.FlyteworkflowV1alpha1().FlyteWorkflows(namespace).Delete(input.ExecutionID.GetName(), &v1.DeleteOptions{
PropagationPolicy: &deletePropagationBackground,
})
// An IsNotFound error indicates the resource is already deleted.
if err != nil && !k8_api_err.IsNotFound(err) {
c.metrics.TerminateExecutionFailure.Inc()
logger.Errorf(ctx, "failed to terminate execution %v", input.ExecutionID)
return errors.NewFlyteAdminErrorf(codes.Internal, "failed to terminate execution: %v with err %v", input.ExecutionID, err)
}
logger.Debugf(ctx, "terminated execution: %v in cluster %s", input.ExecutionID, input.Cluster)
return nil
}
func newPropellerMetrics(scope promutils.Scope) propellerMetrics {
return propellerMetrics{
Scope: scope,
WorkflowBuildSuccess: scope.MustNewCounter("build_success",
"count of workflows built by propeller without error"),
WorkflowBuildFailure: scope.MustNewCounter("build_failure",
"count of workflows built by propeller with errors"),
InvalidExecutionID: scope.MustNewCounter("invalid_execution_id",
"count of invalid execution identifiers used when creating a workflow execution"),
ExecutionCreationSuccess: scope.MustNewCounter("execution_creation_success",
"count of successfully created workflow executions"),
ExecutionCreationFailure: scope.MustNewCounter("execution_creation_failure",
"count of failed workflow executions creations"),
TerminateExecutionFailure: scope.MustNewCounter("execution_termination_failure",
"count of failed workflow executions terminations"),
}
}
func NewFlytePropeller(roleNameKey string, executionCluster interfaces2.ClusterInterface,
scope promutils.Scope, configuration runtimeInterfaces.NamespaceMappingConfiguration) interfaces.Executor {
return &FlytePropeller{
executionCluster: executionCluster,
builder: &FlyteWorkflowBuilder{},
roleNameKey: roleNameKey,
metrics: newPropellerMetrics(scope),
config: configuration,
}
}