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
/
shared.go
300 lines (272 loc) · 11 KB
/
shared.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
// Shared method implementations.
package util
import (
"context"
"time"
"github.com/flyteorg/flyteadmin/pkg/common"
"github.com/flyteorg/flyteadmin/pkg/errors"
"github.com/flyteorg/flyteadmin/pkg/manager/impl/shared"
"github.com/flyteorg/flyteadmin/pkg/manager/impl/validation"
"github.com/flyteorg/flyteadmin/pkg/manager/interfaces"
repoInterfaces "github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
"github.com/flyteorg/flyteadmin/pkg/repositories/models"
"github.com/flyteorg/flyteadmin/pkg/repositories/transformers"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flytestdlib/storage"
"google.golang.org/grpc/codes"
)
func GetExecutionName(request admin.ExecutionCreateRequest) string {
if request.Name != "" {
return request.Name
}
return common.GetExecutionName(time.Now().UnixNano())
}
func GetTask(ctx context.Context, repo repoInterfaces.Repository, identifier core.Identifier) (
*admin.Task, error) {
taskModel, err := GetTaskModel(ctx, repo, &identifier)
if err != nil {
return nil, err
}
task, err := transformers.FromTaskModel(*taskModel)
if err != nil {
logger.Errorf(ctx,
"Failed to transform task model for identifier [%+v] with err: %v", identifier, err)
return nil, err
}
return &task, nil
}
func GetWorkflowModel(
ctx context.Context, repo repoInterfaces.Repository, identifier core.Identifier) (models.Workflow, error) {
workflowModel, err := (repo).WorkflowRepo().Get(ctx, repoInterfaces.Identifier{
Project: identifier.Project,
Domain: identifier.Domain,
Name: identifier.Name,
Version: identifier.Version,
})
if err != nil {
return models.Workflow{}, err
}
return workflowModel, nil
}
func FetchAndGetWorkflowClosure(ctx context.Context,
store *storage.DataStore,
remoteLocationIdentifier string) (*admin.WorkflowClosure, error) {
closure := &admin.WorkflowClosure{}
err := store.ReadProtobuf(ctx, storage.DataReference(remoteLocationIdentifier), closure)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.Internal,
"Unable to read WorkflowClosure from location %s : %v", remoteLocationIdentifier, err)
}
return closure, nil
}
func GetWorkflow(
ctx context.Context,
repo repoInterfaces.Repository,
store *storage.DataStore,
identifier core.Identifier) (*admin.Workflow, error) {
workflowModel, err := GetWorkflowModel(ctx, repo, identifier)
if err != nil {
return nil, err
}
workflow, err := transformers.FromWorkflowModel(workflowModel)
if err != nil {
return nil, err
}
closure, err := FetchAndGetWorkflowClosure(ctx, store, workflowModel.RemoteClosureIdentifier)
if err != nil {
return nil, err
}
closure.CreatedAt = workflow.Closure.CreatedAt
workflow.Closure = closure
return &workflow, nil
}
func GetLaunchPlanModel(
ctx context.Context, repo repoInterfaces.Repository, identifier core.Identifier) (models.LaunchPlan, error) {
launchPlanModel, err := (repo).LaunchPlanRepo().Get(ctx, repoInterfaces.Identifier{
Project: identifier.Project,
Domain: identifier.Domain,
Name: identifier.Name,
Version: identifier.Version,
})
if err != nil {
return models.LaunchPlan{}, err
}
return launchPlanModel, nil
}
func GetLaunchPlan(
ctx context.Context, repo repoInterfaces.Repository, identifier core.Identifier) (*admin.LaunchPlan, error) {
launchPlanModel, err := GetLaunchPlanModel(ctx, repo, identifier)
if err != nil {
return nil, err
}
return transformers.FromLaunchPlanModel(launchPlanModel)
}
func GetNamedEntityModel(
ctx context.Context, repo repoInterfaces.Repository, resourceType core.ResourceType, identifier admin.NamedEntityIdentifier) (models.NamedEntity, error) {
metadataModel, err := (repo).NamedEntityRepo().Get(ctx, repoInterfaces.GetNamedEntityInput{
ResourceType: resourceType,
Project: identifier.Project,
Domain: identifier.Domain,
Name: identifier.Name,
})
if err != nil {
return models.NamedEntity{}, err
}
return metadataModel, nil
}
func GetNamedEntity(
ctx context.Context, repo repoInterfaces.Repository, resourceType core.ResourceType, identifier admin.NamedEntityIdentifier) (*admin.NamedEntity, error) {
metadataModel, err := GetNamedEntityModel(ctx, repo, resourceType, identifier)
if err != nil {
return nil, err
}
metadata := transformers.FromNamedEntityModel(metadataModel)
return &metadata, nil
}
// Returns the set of filters necessary to query launch plan models to find the active version of a launch plan
func GetActiveLaunchPlanVersionFilters(project, domain, name string) ([]common.InlineFilter, error) {
projectFilter, err := common.NewSingleValueFilter(common.LaunchPlan, common.Equal, shared.Project, project)
if err != nil {
return nil, err
}
domainFilter, err := common.NewSingleValueFilter(common.LaunchPlan, common.Equal, shared.Domain, domain)
if err != nil {
return nil, err
}
nameFilter, err := common.NewSingleValueFilter(common.LaunchPlan, common.Equal, shared.Name, name)
if err != nil {
return nil, err
}
activeFilter, err := common.NewSingleValueFilter(common.LaunchPlan, common.Equal, shared.State, int32(admin.LaunchPlanState_ACTIVE))
if err != nil {
return nil, err
}
return []common.InlineFilter{projectFilter, domainFilter, nameFilter, activeFilter}, nil
}
// Returns the set of filters necessary to query launch plan models to find the active version of a launch plan
func ListActiveLaunchPlanVersionsFilters(project, domain string) ([]common.InlineFilter, error) {
projectFilter, err := common.NewSingleValueFilter(common.LaunchPlan, common.Equal, shared.Project, project)
if err != nil {
return nil, err
}
domainFilter, err := common.NewSingleValueFilter(common.LaunchPlan, common.Equal, shared.Domain, domain)
if err != nil {
return nil, err
}
activeFilter, err := common.NewSingleValueFilter(common.LaunchPlan, common.Equal, shared.State, int32(admin.LaunchPlanState_ACTIVE))
if err != nil {
return nil, err
}
return []common.InlineFilter{projectFilter, domainFilter, activeFilter}, nil
}
func GetExecutionModel(
ctx context.Context, repo repoInterfaces.Repository, identifier core.WorkflowExecutionIdentifier) (
*models.Execution, error) {
executionModel, err := repo.ExecutionRepo().Get(ctx, repoInterfaces.Identifier{
Project: identifier.Project,
Domain: identifier.Domain,
Name: identifier.Name,
})
if err != nil {
return nil, err
}
return &executionModel, nil
}
func GetNodeExecutionModel(ctx context.Context, repo repoInterfaces.Repository, nodeExecutionIdentifier *core.NodeExecutionIdentifier) (
*models.NodeExecution, error) {
nodeExecutionModel, err := repo.NodeExecutionRepo().Get(ctx, repoInterfaces.NodeExecutionResource{
NodeExecutionIdentifier: *nodeExecutionIdentifier,
})
if err != nil {
return nil, err
}
return &nodeExecutionModel, nil
}
func GetTaskModel(ctx context.Context, repo repoInterfaces.Repository, taskIdentifier *core.Identifier) (
*models.Task, error) {
taskModel, err := repo.TaskRepo().Get(ctx, repoInterfaces.Identifier{
Project: taskIdentifier.Project,
Domain: taskIdentifier.Domain,
Name: taskIdentifier.Name,
Version: taskIdentifier.Version,
})
if err != nil {
return nil, err
}
return &taskModel, nil
}
func GetTaskExecutionModel(
ctx context.Context, repo repoInterfaces.Repository, taskExecutionID *core.TaskExecutionIdentifier) (*models.TaskExecution, error) {
if err := validation.ValidateTaskExecutionIdentifier(taskExecutionID); err != nil {
logger.Debugf(ctx, "can't get task execution with invalid identifier [%v]: %v", taskExecutionID, err)
return nil, err
}
taskExecutionModel, err := repo.TaskExecutionRepo().Get(ctx, repoInterfaces.GetTaskExecutionInput{
TaskExecutionID: *taskExecutionID,
})
if err != nil {
logger.Debugf(ctx, "Failed to get task execution with id [%+v] with err %v", taskExecutionID, err)
return nil, err
}
return &taskExecutionModel, nil
}
// GetMatchableResource gets matchable resource for resourceType and project - domain - workflow combination.
// Returns nil with nothing is found or return an error
func GetMatchableResource(ctx context.Context, resourceManager interfaces.ResourceInterface, resourceType admin.MatchableResource,
project, domain, workflowName string) (*interfaces.ResourceResponse, error) {
matchableResource, err := resourceManager.GetResource(ctx, interfaces.ResourceRequest{
Project: project,
Domain: domain,
Workflow: workflowName,
ResourceType: resourceType,
})
if err != nil {
if flyteAdminError, ok := err.(errors.FlyteAdminError); !ok || flyteAdminError.Code() != codes.NotFound {
logger.Errorf(ctx, "Failed to get %v overrides in %s project %s domain %s workflow with error: %v", resourceType,
project, domain, workflowName, err)
return nil, err
}
}
return matchableResource, nil
}
// MergeIntoExecConfig into workflowExecConfig (higher priority) from spec (lower priority) and return the
// new object with the merged changes.
// After settings project is done, can move this function back to execution manager. Currently shared with resource.
func MergeIntoExecConfig(workflowExecConfig admin.WorkflowExecutionConfig, spec shared.WorkflowExecutionConfigInterface) admin.WorkflowExecutionConfig {
if workflowExecConfig.GetMaxParallelism() == 0 && spec.GetMaxParallelism() > 0 {
workflowExecConfig.MaxParallelism = spec.GetMaxParallelism()
}
// Do a deep check on the spec in case the security context is set but to an empty object (which may be
// the case when coming from the UI)
if workflowExecConfig.GetSecurityContext() == nil && spec.GetSecurityContext() != nil {
if spec.GetSecurityContext().GetRunAs() != nil &&
(len(spec.GetSecurityContext().GetRunAs().GetK8SServiceAccount()) > 0 ||
len(spec.GetSecurityContext().GetRunAs().GetIamRole()) > 0) {
workflowExecConfig.SecurityContext = spec.GetSecurityContext()
}
}
// Launchplan spec has label, annotation and rawOutputDataConfig initialized with empty values.
// Hence we do a deep check in the following conditions before assignment
if (workflowExecConfig.GetRawOutputDataConfig() == nil ||
len(workflowExecConfig.GetRawOutputDataConfig().GetOutputLocationPrefix()) == 0) &&
(spec.GetRawOutputDataConfig() != nil && len(spec.GetRawOutputDataConfig().OutputLocationPrefix) > 0) {
workflowExecConfig.RawOutputDataConfig = spec.GetRawOutputDataConfig()
}
if (workflowExecConfig.GetLabels() == nil || len(workflowExecConfig.GetLabels().Values) == 0) &&
(spec.GetLabels() != nil && len(spec.GetLabels().Values) > 0) {
workflowExecConfig.Labels = spec.GetLabels()
}
if (workflowExecConfig.GetAnnotations() == nil || len(workflowExecConfig.GetAnnotations().Values) == 0) &&
(spec.GetAnnotations() != nil && len(spec.GetAnnotations().Values) > 0) {
workflowExecConfig.Annotations = spec.GetAnnotations()
}
if workflowExecConfig.GetInterruptible() == nil && spec.GetInterruptible() != nil {
workflowExecConfig.Interruptible = spec.GetInterruptible()
}
if !workflowExecConfig.GetOverwriteCache() && spec.GetOverwriteCache() {
workflowExecConfig.OverwriteCache = spec.GetOverwriteCache()
}
return workflowExecConfig
}