-
Notifications
You must be signed in to change notification settings - Fork 267
/
resolve-action.ts
292 lines (255 loc) · 8.92 KB
/
resolve-action.ts
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
/*
* Copyright (C) 2018-2023 Garden Technologies, Inc. <info@garden.io>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { BaseActionTask, ActionTaskProcessParams, ActionTaskStatusParams, BaseTask, ValidResultType } from "./base"
import { Profile } from "../util/profiling"
import type { Action, ActionState, BaseActionConfig, ExecutedAction, Resolved, ResolvedAction } from "../actions/types"
import { ActionSpecContext } from "../config/template-contexts/actions"
import { resolveTemplateStrings } from "../template-string/template-string"
import { InternalError } from "../exceptions"
import { validateWithPath } from "../config/validation"
import { DeepPrimitiveMap } from "../config/common"
import { merge } from "lodash"
import { mergeVariables } from "../graph/common"
import { actionToResolved } from "../actions/helpers"
import { ResolvedConfigGraph } from "../graph/config-graph"
import { OtelTraced } from "../util/open-telemetry/decorators"
export interface ResolveActionResults<T extends Action> extends ValidResultType {
state: ActionState
outputs: {
resolvedAction: Resolved<T>
}
detail: null
}
@Profile()
export class ResolveActionTask<T extends Action> extends BaseActionTask<T, ResolveActionResults<T>> {
type = "resolve-action"
getDescription() {
return `resolve ${this.action.longDescription()}`
}
override getName() {
return this.action.key()
}
getStatus({}: ActionTaskStatusParams<T>) {
return null
}
override resolveStatusDependencies() {
return []
}
override resolveProcessDependencies(): BaseTask[] {
// TODO-0.13.1
// If we get a resolved task upfront, e.g. from module conversion, we could avoid resolving any dependencies.
// if (this.action.getConfig().internal?.resolved) {
// return []
// }
return this.action.getDependencyReferences().flatMap((d): BaseTask[] => {
const action = this.graph.getActionByRef(d, { includeDisabled: true })
if (d.needsExecutedOutputs) {
// Need runtime outputs from dependency
return [this.getExecuteTask(action)]
} else if (d.needsStaticOutputs || d.explicit) {
// Needs a static output from dependency
return [this.getResolveTask(action)]
} else {
return []
}
})
}
@OtelTraced({
name(_params) {
return this.action.key() + ".resolveAction"
},
getAttributes(_params) {
return {
key: this.action.key(),
kind: this.action.kind,
}
},
})
async process({
dependencyResults,
}: ActionTaskProcessParams<T, ResolveActionResults<T>>): Promise<ResolveActionResults<T>> {
const action = this.action
const config = action.getConfig() as BaseActionConfig
// Collect dependencies
const resolvedDependencies: ResolvedAction[] = []
const executedDependencies: ExecutedAction[] = []
// TODO: get this to a type-safer place
for (const task of dependencyResults.getTasks()) {
if (task instanceof ResolveActionTask) {
const r = dependencyResults.getResult(task)
if (!r) {
continue
}
resolvedDependencies.push(r.outputs.resolvedAction)
} else if (task.isExecuteTask()) {
const r = dependencyResults.getResult(task)
if (!r?.result) {
continue
}
executedDependencies.push(r.result.executedAction)
}
}
// Resolve template inputs
const inputsContext = new ActionSpecContext({
garden: this.garden,
resolvedProviders: await this.garden.resolveProviders(this.log),
action,
modules: this.graph.getModules(),
partialRuntimeResolution: false,
resolvedDependencies,
executedDependencies,
variables: {},
inputs: {},
})
const template = config.internal.templateName ? this.garden.configTemplates[config.internal.templateName] : null
const inputs = resolveTemplateStrings({
value: config.internal.inputs || {},
context: inputsContext,
contextOpts: { allowPartial: false },
source: { yamlDoc: template?.internal.yamlDoc, basePath: ["inputs"] },
})
// Resolve variables
let groupVariables: DeepPrimitiveMap = {}
const groupName = action.groupName()
if (groupName) {
const group = this.graph.getGroup(groupName)
groupVariables = resolveTemplateStrings({
value: await mergeVariables({ basePath: group.path, variables: group.variables, varfiles: group.varfiles }),
context: inputsContext,
// TODO: map variables to their source
source: undefined,
})
}
const actionVariables = resolveTemplateStrings({
value: await mergeVariables({
basePath: action.sourcePath(),
variables: config.variables,
varfiles: config.varfiles,
}),
context: new ActionSpecContext({
garden: this.garden,
resolvedProviders: await this.garden.resolveProviders(this.log),
action,
modules: this.graph.getModules(),
partialRuntimeResolution: false,
resolvedDependencies,
executedDependencies,
variables: groupVariables,
inputs,
}),
// TODO: map variables to their source
source: undefined,
})
const variables = groupVariables
merge(variables, actionVariables)
// Override with CLI-set variables
merge(variables, this.garden.variableOverrides)
// Resolve spec
let spec = resolveTemplateStrings({
value: action.getConfig().spec || {},
context: new ActionSpecContext({
garden: this.garden,
resolvedProviders: await this.garden.resolveProviders(this.log),
action,
modules: this.graph.getModules(),
partialRuntimeResolution: false,
resolvedDependencies,
executedDependencies,
variables,
inputs,
}),
source: { yamlDoc: action.getInternal().yamlDoc, basePath: ["spec"] },
})
// Validate spec
spec = await this.validateSpec(spec)
// Resolve action without outputs
const resolvedGraph = new ResolvedConfigGraph({
actions: [...resolvedDependencies, ...executedDependencies],
moduleGraph: this.graph.moduleGraph,
groups: this.graph.getGroups(),
})
const resolvedAction = actionToResolved(action, {
resolvedGraph,
dependencyResults,
executedDependencies,
resolvedDependencies,
variables,
inputs,
spec,
staticOutputs: {},
}) as Resolved<T>
// Get outputs and assign to the resolved action
const router = await this.garden.getActionRouter()
const { outputs: staticOutputs } = await router.getActionOutputs({
action: resolvedAction,
graph: this.graph,
log: this.log,
})
// Validate the outputs
const actionRouter = router.getRouterForActionKind(resolvedAction.kind)
await actionRouter.validateActionOutputs(resolvedAction, "static", staticOutputs)
await actionRouter.callHandler({
handlerType: "validate",
params: { action: resolvedAction, graph: resolvedGraph, log: this.log, events: undefined },
defaultHandler: async (_) => ({}),
})
// TODO: avoid this private assignment
resolvedAction["_staticOutputs"] = staticOutputs
return {
state: "ready",
outputs: {
resolvedAction,
},
detail: null,
}
}
@OtelTraced({
name: "validateAction",
getAttributes(_spec) {
return {
key: this.action.key(),
kind: this.action.kind,
}
},
})
private async validateSpec<S>(spec: S) {
const actionTypes = await this.garden.getActionTypes()
const { kind, type } = this.action
const actionType = actionTypes[kind][type]?.spec
const description = this.action.longDescription()
if (!actionType) {
// This should be caught way earlier in normal usage, so it's an internal error
throw new InternalError({
message: `Could not find type definition for ${description} (kind: ${kind}, type: ${type}).`,
})
}
const path = this.action.sourcePath()
const internal = this.action.getInternal()
spec = validateWithPath({
config: spec,
schema: actionType.schema,
path,
projectRoot: this.garden.projectRoot,
configType: `spec for ${description}`,
source: { yamlDoc: internal.yamlDoc, basePath: ["spec"] },
})
const actionTypeBases = await this.garden.getActionTypeBases(kind, type)
for (const base of actionTypeBases) {
this.log.silly(`Validating ${description} spec against '${base.name}' schema`)
spec = validateWithPath({
config: spec,
schema: base.schema,
path,
projectRoot: this.garden.projectRoot,
configType: `spec for ${description} (base schema from '${base.name}' plugin)`,
source: { yamlDoc: internal.yamlDoc, basePath: ["spec"] },
})
}
return spec
}
}