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
/
k8s_executor.go
105 lines (93 loc) · 4.26 KB
/
k8s_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
package impl
import (
"context"
"github.com/flyteorg/flyteadmin/pkg/errors"
"github.com/flyteorg/flyteadmin/pkg/executioncluster"
execClusterInterfaces "github.com/flyteorg/flyteadmin/pkg/executioncluster/interfaces"
runtimeInterfaces "github.com/flyteorg/flyteadmin/pkg/runtime/interfaces"
"github.com/flyteorg/flyteadmin/pkg/workflowengine/interfaces"
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flytestdlib/storage"
"google.golang.org/grpc/codes"
k8_api_err "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var deletePropagationBackground = v1.DeletePropagationBackground
const defaultIdentifier = "DefaultK8sExecutor"
// K8sWorkflowExecutor directly creates and delete Flyte workflow execution CRD objects using the configured execution
// cluster interface.
type K8sWorkflowExecutor struct {
config runtimeInterfaces.Configuration
executionCluster execClusterInterfaces.ClusterInterface
workflowBuilder interfaces.FlyteWorkflowBuilder
storageClient *storage.DataStore
}
func (e K8sWorkflowExecutor) ID() string {
return defaultIdentifier
}
func (e K8sWorkflowExecutor) Execute(ctx context.Context, data interfaces.ExecutionData) (interfaces.ExecutionResponse, error) {
flyteWf, err := e.workflowBuilder.Build(data.WorkflowClosure, data.ExecutionParameters.Inputs, data.ExecutionID, data.Namespace)
if err != nil {
logger.Infof(ctx, "failed to build the workflow [%+v] %v",
data.WorkflowClosure.Primary.Template.Id, err)
return interfaces.ExecutionResponse{}, err
}
err = PrepareFlyteWorkflow(data, flyteWf)
if err != nil {
return interfaces.ExecutionResponse{}, err
}
if e.config.ApplicationConfiguration().GetTopLevelConfig().UseOffloadedWorkflowClosure {
// if offloading workflow closure is enabled we set the WorkflowClosureReference and remove
// the closure generated static fields from the FlyteWorkflow CRD. They are read from the
// storage client and temporarily repopulated during execution to reduce the CRD size.
flyteWf.WorkflowClosureReference = data.WorkflowClosureReference
flyteWf.WorkflowSpec = nil
flyteWf.SubWorkflows = nil
flyteWf.Tasks = nil
}
executionTargetSpec := executioncluster.ExecutionTargetSpec{
Project: data.ExecutionID.Project,
Domain: data.ExecutionID.Domain,
Workflow: data.ReferenceWorkflowName,
LaunchPlan: data.ReferenceWorkflowName,
ExecutionID: data.ExecutionID.Name,
}
targetCluster, err := e.executionCluster.GetTarget(ctx, &executionTargetSpec)
if err != nil {
return interfaces.ExecutionResponse{}, errors.NewFlyteAdminErrorf(codes.Internal, "failed to create workflow in propeller %v", err)
}
_, err = targetCluster.FlyteClient.FlyteworkflowV1alpha1().FlyteWorkflows(data.Namespace).Create(ctx, flyteWf, v1.CreateOptions{})
if err != nil {
if !k8_api_err.IsAlreadyExists(err) {
logger.Debugf(context.TODO(), "Failed to create execution [%+v] in cluster: %s", data.ExecutionID, targetCluster.ID)
return interfaces.ExecutionResponse{}, errors.NewFlyteAdminErrorf(codes.Internal, "failed to create workflow in propeller %v", err)
}
}
return interfaces.ExecutionResponse{
Cluster: targetCluster.ID,
}, nil
}
func (e K8sWorkflowExecutor) Abort(ctx context.Context, data interfaces.AbortData) error {
target, err := e.executionCluster.GetTarget(ctx, &executioncluster.ExecutionTargetSpec{
TargetID: data.Cluster,
})
if err != nil {
return errors.NewFlyteAdminErrorf(codes.Internal, err.Error())
}
err = target.FlyteClient.FlyteworkflowV1alpha1().FlyteWorkflows(data.Namespace).Delete(ctx, data.ExecutionID.GetName(), v1.DeleteOptions{
PropagationPolicy: &deletePropagationBackground,
})
// An IsNotFound error indicates the resource is already deleted.
if err != nil && !k8_api_err.IsNotFound(err) {
return errors.NewFlyteAdminErrorf(codes.Internal, "failed to terminate execution: %v with err %v", data.ExecutionID, err)
}
return nil
}
func NewK8sWorkflowExecutor(config runtimeInterfaces.Configuration, executionCluster execClusterInterfaces.ClusterInterface, workflowBuilder interfaces.FlyteWorkflowBuilder, client *storage.DataStore) *K8sWorkflowExecutor {
return &K8sWorkflowExecutor{
config: config,
executionCluster: executionCluster,
workflowBuilder: workflowBuilder,
storageClient: client,
}
}