-
Notifications
You must be signed in to change notification settings - Fork 40
/
ready_conditions.go
207 lines (184 loc) · 7.96 KB
/
ready_conditions.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
package service_config
import (
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/assert"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_starlark_framework"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_starlark_framework/builtin_argument"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_starlark_framework/kurtosis_type_constructor"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/recipe"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/startosis_errors"
"go.starlark.net/starlark"
"reflect"
"time"
)
const (
ReadyConditionsTypeName = "ReadyConditions"
RecipeAttr = "recipe"
FieldAttr = "field"
AssertionAttr = "assertion"
TargetAttr = "target_value"
IntervalAttr = "interval"
TimeoutAttr = "timeout"
defaultInterval = 1 * time.Second
defaultTimeout = 15 * time.Minute //TODO we could move these two to the service helpers method
)
func NewReadyConditionsType() *kurtosis_type_constructor.KurtosisTypeConstructor {
return &kurtosis_type_constructor.KurtosisTypeConstructor{
KurtosisBaseBuiltin: &kurtosis_starlark_framework.KurtosisBaseBuiltin{
Name: ReadyConditionsTypeName,
Arguments: []*builtin_argument.BuiltinArgument{
{
Name: RecipeAttr,
IsOptional: false,
ZeroValueProvider: builtin_argument.ZeroValueProvider[starlark.Value],
Validator: validateRecipe,
},
{
Name: FieldAttr,
IsOptional: false,
ZeroValueProvider: builtin_argument.ZeroValueProvider[starlark.String],
Validator: func(value starlark.Value) *startosis_errors.InterpretationError {
return builtin_argument.NonEmptyString(value, FieldAttr)
},
},
{
Name: AssertionAttr,
IsOptional: false,
ZeroValueProvider: builtin_argument.ZeroValueProvider[starlark.String],
Validator: assert.ValidateAssertionToken,
},
{
Name: TargetAttr,
IsOptional: false,
ZeroValueProvider: builtin_argument.ZeroValueProvider[starlark.Comparable],
Validator: func(value starlark.Value) *startosis_errors.InterpretationError {
return builtin_argument.NonEmptyString(value, FieldAttr)
},
},
{
Name: IntervalAttr,
IsOptional: true,
ZeroValueProvider: builtin_argument.ZeroValueProvider[starlark.String],
Validator: func(value starlark.Value) *startosis_errors.InterpretationError {
return builtin_argument.Duration(value, IntervalAttr)
},
},
{
Name: TimeoutAttr,
IsOptional: true,
ZeroValueProvider: builtin_argument.ZeroValueProvider[starlark.String],
Validator: func(value starlark.Value) *startosis_errors.InterpretationError {
return builtin_argument.Duration(value, TimeoutAttr)
},
},
},
},
Instantiate: instantiateReadyConditions,
}
}
func instantiateReadyConditions(arguments *builtin_argument.ArgumentValuesSet) (kurtosis_type_constructor.KurtosisValueType, *startosis_errors.InterpretationError) {
kurtosisValueType, err := kurtosis_type_constructor.CreateKurtosisStarlarkTypeDefault(ReadyConditionsTypeName, arguments)
if err != nil {
return nil, err
}
return &ReadyConditions{
KurtosisValueTypeDefault: kurtosisValueType,
}, nil
}
// ReadyConditions is a starlark.Value that holds all the information needed for ensuring service readiness
type ReadyConditions struct {
*kurtosis_type_constructor.KurtosisValueTypeDefault
}
func (readyConditions *ReadyConditions) GetRecipe() (recipe.Recipe, *startosis_errors.InterpretationError) {
var genericRecipe recipe.Recipe
httpRecipe, found, interpretationErr := kurtosis_type_constructor.ExtractAttrValue[*recipe.HttpRequestRecipe](readyConditions.KurtosisValueTypeDefault, RecipeAttr)
genericRecipe = httpRecipe
if !found {
return nil, startosis_errors.NewInterpretationError("Required attribute '%s' could not be found on type '%s'",
RecipeAttr, ReadyConditionsTypeName)
}
//TODO we should rework the recipe types to inherit a single common type, this will avoid the double parsing here.
if interpretationErr != nil {
execRecipe, _, interpretationErr := kurtosis_type_constructor.ExtractAttrValue[*recipe.ExecRecipe](readyConditions.KurtosisValueTypeDefault, RecipeAttr)
if interpretationErr != nil {
return nil, interpretationErr
}
genericRecipe = execRecipe
}
return genericRecipe, nil
}
func (readyConditions *ReadyConditions) GetField() (string, *startosis_errors.InterpretationError) {
field, found, interpretationErr := kurtosis_type_constructor.ExtractAttrValue[starlark.String](readyConditions.KurtosisValueTypeDefault, FieldAttr)
if interpretationErr != nil {
return "", interpretationErr
}
if !found {
return "", startosis_errors.NewInterpretationError("Required attribute '%s' could not be found on type '%s'",
FieldAttr, ReadyConditionsTypeName)
}
fieldStr := field.GoString()
return fieldStr, nil
}
func (readyConditions *ReadyConditions) GetAssertion() (string, *startosis_errors.InterpretationError) {
assertion, found, interpretationErr := kurtosis_type_constructor.ExtractAttrValue[starlark.String](readyConditions.KurtosisValueTypeDefault, AssertionAttr)
if interpretationErr != nil {
return "", interpretationErr
}
if !found {
return "", startosis_errors.NewInterpretationError("Required attribute '%s' could not be found on type '%s'",
AssertionAttr, ReadyConditionsTypeName)
}
assertionStr := assertion.GoString()
return assertionStr, nil
}
func (readyConditions *ReadyConditions) GetTarget() (starlark.Comparable, *startosis_errors.InterpretationError) {
target, found, interpretationErr := kurtosis_type_constructor.ExtractAttrValue[starlark.Comparable](readyConditions.KurtosisValueTypeDefault, TargetAttr)
if interpretationErr != nil {
return nil, interpretationErr
}
if !found {
return nil, startosis_errors.NewInterpretationError("Required attribute '%s' could not be found on type '%s'",
TargetAttr, ReadyConditionsTypeName)
}
return target, nil
}
func (readyConditions *ReadyConditions) GetInterval() (time.Duration, *startosis_errors.InterpretationError) {
interval := defaultInterval
intervalStr, found, interpretationErr := kurtosis_type_constructor.ExtractAttrValue[starlark.String](readyConditions.KurtosisValueTypeDefault, IntervalAttr)
if interpretationErr != nil {
return interval, interpretationErr
}
if found {
parsedInterval, parseErr := time.ParseDuration(intervalStr.GoString())
if parseErr != nil {
return interval, startosis_errors.WrapWithInterpretationError(parseErr, "An error occurred when parsing interval '%v'", intervalStr.GoString())
}
interval = parsedInterval
}
return interval, nil
}
func (readyConditions *ReadyConditions) GetTimeout() (time.Duration, *startosis_errors.InterpretationError) {
timeout := defaultTimeout
timeoutStr, found, interpretationErr := kurtosis_type_constructor.ExtractAttrValue[starlark.String](readyConditions.KurtosisValueTypeDefault, TimeoutAttr)
if interpretationErr != nil {
return timeout, interpretationErr
}
if found {
parsedTimeout, parseErr := time.ParseDuration(timeoutStr.GoString())
if parseErr != nil {
return timeout, startosis_errors.WrapWithInterpretationError(parseErr, "An error occurred when parsing timeout '%v'", timeoutStr.GoString())
}
timeout = parsedTimeout
}
return timeout, nil
}
func validateRecipe(value starlark.Value) *startosis_errors.InterpretationError {
_, ok := value.(*recipe.HttpRequestRecipe)
if !ok {
//TODO we should rework the recipe types to inherit a single common type, this will avoid the double parsing here.
_, ok := value.(*recipe.ExecRecipe)
if !ok {
return startosis_errors.NewInterpretationError("The '%s' attribute is not a Recipe (was '%s').", RecipeAttr, reflect.TypeOf(value))
}
}
return nil
}