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
/
execution_validator.go
162 lines (142 loc) · 5.66 KB
/
execution_validator.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
package validation
import (
"context"
"regexp"
"strings"
"github.com/flyteorg/flyteadmin/pkg/repositories"
"github.com/flyteorg/flyteadmin/pkg/errors"
"github.com/flyteorg/flyteadmin/pkg/manager/impl/shared"
runtimeInterfaces "github.com/flyteorg/flyteadmin/pkg/runtime/interfaces"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flytepropeller/pkg/compiler/validators"
"google.golang.org/grpc/codes"
)
const allowedExecutionNameLength = 20
var executionIDRegex = regexp.MustCompile(`^[a-z][a-z\-0-9]*$`)
var acceptedReferenceLaunchTypes = map[core.ResourceType]interface{}{
core.ResourceType_LAUNCH_PLAN: nil,
core.ResourceType_TASK: nil,
}
func ValidateExecutionRequest(ctx context.Context, request admin.ExecutionCreateRequest,
db repositories.RepositoryInterface, config runtimeInterfaces.ApplicationConfiguration) error {
if err := ValidateEmptyStringField(request.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Domain, shared.Domain); err != nil {
return err
}
if request.Name != "" {
if err := CheckValidExecutionID(strings.ToLower(request.Name), shared.Name); err != nil {
return err
}
}
if len(request.Name) > allowedExecutionNameLength {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"name for ExecutionCreateRequest [%+v] exceeded allowed length %d", request, allowedExecutionNameLength)
}
if err := ValidateProjectAndDomain(ctx, db, config, request.Project, request.Domain); err != nil {
return err
}
if request.Spec == nil {
return shared.GetMissingArgumentError(shared.Spec)
}
// TODO(katrogan): Change the name of Spec.LaunchPlan to something more generic to permit reference Tasks.
// https://github.com/flyteorg/flyte/issues/262
if err := ValidateIdentifierFieldsSet(request.Spec.LaunchPlan); err != nil {
return err
}
if _, ok := acceptedReferenceLaunchTypes[request.Spec.LaunchPlan.ResourceType]; !ok {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"Invalid reference entity resource type [%v], only [%+v] allowed",
request.Spec.LaunchPlan.ResourceType, acceptedReferenceLaunchTypes)
}
if err := validateLiteralMap(request.Inputs, shared.Inputs); err != nil {
return err
}
// TODO: Remove redundant validation with the rest of the method.
// This final call to validating the request ensures the notification types are expected.
if err := request.Validate(); err != nil {
return err
}
return nil
}
func CheckAndFetchInputsForExecution(
userInputs *core.LiteralMap, fixedInputs *core.LiteralMap, expectedInputs *core.ParameterMap) (*core.LiteralMap, error) {
executionInputMap := map[string]*core.Literal{}
expectedInputMap := map[string]*core.Parameter{}
if expectedInputs != nil && len(expectedInputs.GetParameters()) > 0 {
expectedInputMap = expectedInputs.GetParameters()
}
if userInputs != nil && len(userInputs.GetLiterals()) > 0 {
for name, value := range userInputs.GetLiterals() {
if _, ok := expectedInputMap[name]; !ok {
return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument, "invalid input %s", name)
}
executionInputMap[name] = value
}
}
for name, expectedInput := range expectedInputMap {
if _, ok := executionInputMap[name]; !ok {
if expectedInput.GetRequired() {
return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument, "%s %s missing", shared.ExpectedInputs, name)
}
executionInputMap[name] = expectedInput.GetDefault()
} else {
inputType := validators.LiteralTypeForLiteral(executionInputMap[name])
if !validators.AreTypesCastable(inputType, expectedInput.GetVar().GetType()) {
return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument, "invalid %s input wrong type", name)
}
}
}
if fixedInputs != nil && len(fixedInputs.GetLiterals()) > 0 {
for name, fixedInput := range fixedInputs.GetLiterals() {
if _, ok := executionInputMap[name]; ok {
return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument, "%s %s cannot be overridden", shared.FixedInputs, name)
}
executionInputMap[name] = fixedInput
}
}
return &core.LiteralMap{
Literals: executionInputMap,
}, nil
}
func CheckValidExecutionID(executionID, fieldName string) error {
if len(executionID) > allowedExecutionNameLength {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"size of %s exceeded length %d : %s", fieldName, allowedExecutionNameLength, executionID)
}
matched := executionIDRegex.MatchString(executionID)
if !matched {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, "invalid %s format: %s", fieldName, executionID)
}
return nil
}
func ValidateCreateWorkflowEventRequest(request admin.WorkflowExecutionEventRequest, maxOutputSizeInBytes int64) error {
if request.Event == nil {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"Workflow event handler was called without event")
} else if request.Event.ExecutionId == nil {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"Workflow event handler request event doesn't have an execution id - %v", request.Event)
}
if err := ValidateOutputData(request.Event.GetOutputData(), maxOutputSizeInBytes); err != nil {
return err
}
return nil
}
func ValidateWorkflowExecutionIdentifier(identifier *core.WorkflowExecutionIdentifier) error {
if identifier == nil {
return shared.GetMissingArgumentError(shared.ID)
}
if err := ValidateEmptyStringField(identifier.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(identifier.Domain, shared.Domain); err != nil {
return err
}
if err := ValidateEmptyStringField(identifier.Name, shared.Name); err != nil {
return err
}
return nil
}