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
/
launch_plan_validator.go
160 lines (146 loc) · 6.18 KB
/
launch_plan_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
package validation
import (
"context"
repositoryInterfaces "github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
"github.com/flyteorg/flyteadmin/pkg/common"
"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"
)
func ValidateLaunchPlan(ctx context.Context,
request admin.LaunchPlanCreateRequest, db repositoryInterfaces.Repository,
config runtimeInterfaces.ApplicationConfiguration, workflowInterface *core.TypedInterface) error {
if err := ValidateIdentifier(request.Id, common.LaunchPlan); err != nil {
return err
}
if err := ValidateProjectAndDomain(ctx, db, config, request.Id.Project, request.Id.Domain); err != nil {
return err
}
if request.Spec == nil {
return shared.GetMissingArgumentError(shared.Spec)
}
if err := ValidateIdentifier(request.Spec.WorkflowId, common.Workflow); err != nil {
return err
}
if err := validateLabels(request.Spec.Labels); err != nil {
return err
}
if err := validateLiteralMap(request.Spec.FixedInputs, shared.FixedInputs); err != nil {
return err
}
if err := validateParameterMap(request.Spec.DefaultInputs, shared.DefaultInputs); err != nil {
return err
}
expectedInputs, err := checkAndFetchExpectedInputForLaunchPlan(workflowInterface.GetInputs(), request.Spec.FixedInputs, request.Spec.DefaultInputs)
if err != nil {
return err
}
if err := validateSchedule(request, expectedInputs); err != nil {
return err
}
// Augment default inputs with the unbound workflow inputs.
request.Spec.DefaultInputs = expectedInputs
// TODO: Remove redundant validation that occurs with launch plan and the validate method for the message.
// Ensure the notification types are validated.
if err := request.Validate(); err != nil {
return err
}
return nil
}
func validateSchedule(request admin.LaunchPlanCreateRequest, expectedInputs *core.ParameterMap) error {
schedule := request.GetSpec().GetEntityMetadata().GetSchedule()
if schedule.GetCronExpression() != "" || schedule.GetRate() != nil {
for key, value := range expectedInputs.Parameters {
if value.GetRequired() && key != schedule.GetKickoffTimeInputArg() {
return errors.NewFlyteAdminErrorf(
codes.InvalidArgument,
"Cannot create a launch plan with a schedule if there is an unbound required input. [%v] is required", key)
}
}
if schedule.GetKickoffTimeInputArg() != "" {
if param, ok := expectedInputs.Parameters[schedule.GetKickoffTimeInputArg()]; !ok {
return errors.NewFlyteAdminErrorf(
codes.InvalidArgument,
"Cannot create a schedule with a KickoffTimeInputArg that does not point to a free input. [%v] is not free or does not exist.", schedule.GetKickoffTimeInputArg())
} else if param.GetVar().GetType().GetSimple() != core.SimpleType_DATETIME {
return errors.NewFlyteAdminErrorf(
codes.InvalidArgument,
"KickoffTimeInputArg must reference a datetime input. [%v] is a [%v]", schedule.GetKickoffTimeInputArg(), param.GetVar().GetType())
}
}
}
return nil
}
func checkAndFetchExpectedInputForLaunchPlan(
workflowVariableMap *core.VariableMap, fixedInputs *core.LiteralMap, defaultInputs *core.ParameterMap) (*core.ParameterMap, error) {
expectedInputMap := map[string]*core.Parameter{}
var workflowExpectedInputMap map[string]*core.Variable
var defaultInputMap map[string]*core.Parameter
var fixedInputMap map[string]*core.Literal
if defaultInputs != nil && len(defaultInputs.GetParameters()) > 0 {
defaultInputMap = defaultInputs.GetParameters()
}
if fixedInputs != nil && len(fixedInputs.GetLiterals()) > 0 {
fixedInputMap = fixedInputs.GetLiterals()
}
// If there are no inputs that the workflow requires, there should be none at launch plan as well
if workflowVariableMap == nil || len(workflowVariableMap.Variables) == 0 {
if len(defaultInputMap) > 0 {
return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"invalid launch plan default inputs, expected none but found %d", len(defaultInputMap))
}
if len(fixedInputMap) > 0 {
return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"invalid launch plan fixed inputs, expected none but found %d", len(fixedInputMap))
}
return &core.ParameterMap{
Parameters: expectedInputMap,
}, nil
}
workflowExpectedInputMap = workflowVariableMap.Variables
for name, defaultInput := range defaultInputMap {
value, ok := workflowExpectedInputMap[name]
if !ok {
return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument, "unexpected default_input %s", name)
} else if !validators.AreTypesCastable(defaultInput.GetVar().GetType(), value.GetType()) {
return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"invalid default_input wrong type %s, expected %v, got %v instead",
name, defaultInput.GetVar().GetType().String(), value.GetType().String())
}
}
for name, fixedInput := range fixedInputMap {
value, ok := workflowExpectedInputMap[name]
if !ok {
return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument, "unexpected fixed_input %s", name)
}
inputType := validators.LiteralTypeForLiteral(fixedInput)
if !validators.AreTypesCastable(inputType, value.GetType()) {
return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"invalid fixed_input wrong type %s, expected %v, got %v instead", name, value.GetType(), inputType)
}
}
for name, workflowExpectedInput := range workflowExpectedInputMap {
if value, ok := defaultInputMap[name]; ok {
// If the launch plan has a default value - then use this value
expectedInputMap[name] = value
} else if _, ok = fixedInputMap[name]; !ok {
// If there is no mention of the input in LaunchPlan, then copy from the workflow
expectedInputMap[name] = &core.Parameter{
Var: &core.Variable{
Type: workflowExpectedInput.GetType(),
Description: workflowExpectedInput.GetDescription(),
},
Behavior: &core.Parameter_Required{
Required: true,
},
}
}
}
return &core.ParameterMap{
Parameters: expectedInputMap,
}, nil
}