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 63
/
Copy pathpropeller_executor.go
198 lines (173 loc) · 7.98 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
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.
var role string
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().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
executionTargetSpec := executioncluster.ExecutionTargetSpec{
ExecutionID: input.ExecutionID,
}
targetCluster, err := c.executionCluster.GetTarget(&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 propeller %v", input.WfClosure.Primary.Template.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(&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,
}
}