-
Notifications
You must be signed in to change notification settings - Fork 267
/
resolve-provider.ts
459 lines (383 loc) · 15.2 KB
/
resolve-provider.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* Copyright (C) 2018-2024 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 type { CommonTaskParams, ResolveProcessDependenciesParams, TaskProcessParams } from "./base.js"
import { BaseTask } from "./base.js"
import type { GenericProviderConfig, Provider, ProviderMap } from "../config/provider.js"
import { providerFromConfig, getProviderTemplateReferences } from "../config/provider.js"
import { resolveTemplateStrings } from "../template-string/template-string.js"
import { ConfigurationError, PluginError } from "../exceptions.js"
import { keyBy, omit, flatten, uniq } from "lodash-es"
import { ProviderConfigContext } from "../config/template-contexts/provider.js"
import type { ModuleConfig } from "../config/module.js"
import type { GardenPluginSpec } from "../plugin/plugin.js"
import { joi } from "../config/common.js"
import { validateWithPath, validateSchema } from "../config/validation.js"
import type { EnvironmentStatus } from "../plugin/handlers/Provider/getEnvironmentStatus.js"
import { defaultEnvironmentStatus } from "../plugin/handlers/Provider/getEnvironmentStatus.js"
import { getPluginBases, getPluginBaseNames } from "../plugins.js"
import { Profile } from "../util/profiling.js"
import { join, dirname } from "path"
import { deserialize, serialize } from "v8"
import { environmentStatusSchema } from "../config/status.js"
import { hashString, isNotNull } from "../util/util.js"
import { CACHE_DIR_NAME, gardenEnv } from "../constants.js"
import { stableStringify } from "../util/string.js"
import { OtelTraced } from "../util/open-telemetry/decorators.js"
import { LogLevel } from "../logger/logger.js"
import type { Log } from "../logger/log-entry.js"
import { styles } from "../logger/styles.js"
import type { ObjectPath } from "../config/base.js"
import fsExtra from "fs-extra"
const { readFile, writeFile, ensureDir } = fsExtra
/**
* Returns a provider log context with the provider name set.
*
* Also sets the log level to verbose for some built in providers that aren't really
* resolved per se. A bit hacky but this is just a cosmetic change.
*/
function getProviderLog(providerName: string, log: Log) {
const debugLogProviders = ["templated", "container"]
const fixLevel = debugLogProviders.includes(providerName) ? LogLevel.debug : undefined
return log.createLog({ name: providerName, fixLevel })
}
interface Params extends CommonTaskParams {
plugin: GardenPluginSpec
allPlugins: GardenPluginSpec[]
config: GenericProviderConfig
forceRefresh: boolean
forceInit: boolean
}
interface CachedStatus extends EnvironmentStatus {
configHash: string
resolvedAt: Date
}
const cachedStatusSchema = environmentStatusSchema().keys({
configHash: joi.string().required(),
resolvedAt: joi.date().required(),
})
const defaultCacheTtl = 3600 // 1 hour
/**
* Resolves the configuration for the specified provider.
*/
@Profile()
export class ResolveProviderTask extends BaseTask<Provider> {
type = "resolve-provider"
override concurrencyLimit = 20
private config: GenericProviderConfig
private plugin: GardenPluginSpec
private forceRefresh: boolean
private forceInit: boolean
private allPlugins: GardenPluginSpec[]
constructor(params: Params) {
super(params)
this.config = params.config
this.plugin = params.plugin
this.allPlugins = params.allPlugins
this.forceRefresh = params.forceRefresh
this.forceInit = params.forceInit
}
getName() {
return this.config.name
}
getDescription() {
return `resolve provider ${this.getName()}`
}
getInputVersion() {
return this.garden.version
}
resolveStatusDependencies() {
return []
}
resolveProcessDependencies({ status }: ResolveProcessDependenciesParams<Provider>) {
if (status?.state === "ready" && !this.force) {
return []
}
const pluginDeps = this.plugin.dependencies
const explicitDeps = (this.config.dependencies || []).map((name) => ({ name }))
const implicitDeps = getProviderTemplateReferences(this.config).map((name) => ({ name }))
const allDeps = uniq([...pluginDeps, ...explicitDeps, ...implicitDeps])
const rawProviderConfigs = this.garden.getRawProviderConfigs()
const plugins = keyBy(this.allPlugins, "name")
const matchDependencies = (depName: string) => {
// Match against a provider if its name matches directly, or it inherits from a base named `depName`
return rawProviderConfigs.filter(
(c) => c.name === depName || getPluginBaseNames(c.name, plugins).includes(depName)
)
}
// Make sure explicit dependencies are configured
pluginDeps.map((dep) => {
const matched = matchDependencies(dep.name)
if (matched.length === 0 && !dep.optional) {
throw new ConfigurationError({
message:
`Provider '${this.config.name}' depends on provider '${dep.name}', which is not configured. ` +
`You need to add '${dep.name}' to your project configuration for the '${this.config.name}' to work.`,
})
}
})
return flatten(
allDeps.map((dep) => {
return matchDependencies(dep.name).map((config) => {
const plugin = plugins[config.name]
return new ResolveProviderTask({
garden: this.garden,
plugin,
allPlugins: this.allPlugins,
config,
log: this.log,
force: this.force,
forceRefresh: this.forceRefresh,
forceInit: this.forceInit,
})
})
})
)
}
async getStatus() {
return null
}
@OtelTraced({
name(_params) {
return this.config.name + ".resolveProvider"
},
getAttributes(_spec) {
return {
name: this.config.name,
}
},
})
async process({ dependencyResults }: TaskProcessParams) {
const providerResults = dependencyResults.getResultsByType(this).filter(isNotNull)
const resolvedProviders: ProviderMap = keyBy(providerResults.map((r) => r.result).filter(isNotNull), "name")
// Return immediately if the provider has been previously resolved
const alreadyResolvedProviders = this.garden["resolvedProviders"][this.config.name]
if (alreadyResolvedProviders) {
return alreadyResolvedProviders
}
const context = new ProviderConfigContext(this.garden, resolvedProviders, this.garden.variables)
this.log.silly(() => `Resolving template strings for provider ${this.config.name}`)
const projectConfig = this.garden.getProjectConfig()
const yamlDoc = projectConfig.internal.yamlDoc
let yamlDocBasePath: ObjectPath = []
if (yamlDoc) {
projectConfig.providers.forEach((p, i) => {
if (p.name === this.config.name) {
yamlDocBasePath = ["providers", i]
return false
}
return true
})
}
const source = { yamlDoc, basePath: yamlDocBasePath }
let resolvedConfig = resolveTemplateStrings({ value: this.config, context, source })
const providerName = resolvedConfig.name
const providerLog = getProviderLog(providerName, this.log)
providerLog.info("Configuring provider...")
this.log.silly(() => `Validating ${providerName} config`)
const validateConfig = (config: GenericProviderConfig) => {
return <GenericProviderConfig>validateWithPath({
config: omit(config, "path"),
schema: this.plugin.configSchema || joi.object(),
path: this.garden.projectRoot,
projectRoot: this.garden.projectRoot,
configType: "provider configuration",
ErrorClass: ConfigurationError,
source,
})
}
resolvedConfig = validateConfig(resolvedConfig)
resolvedConfig.path = this.garden.projectRoot
let moduleConfigs: ModuleConfig[] = []
this.log.silly(() => `Calling configureProvider on ${providerName}`)
const actions = await this.garden.getActionRouter()
// Validating the output config against the base plugins. This is important to make sure base handlers are
// compatible with the config.
const plugins = this.allPlugins
const pluginsByName = keyBy(plugins, "name")
const plugin = pluginsByName[providerName]
const configureOutput = await actions.provider.configureProvider({
ctx: await this.garden.getPluginContext({
provider: providerFromConfig({
plugin,
config: resolvedConfig,
dependencies: {},
moduleConfigs: [],
status: { ready: false, outputs: {} },
}),
templateContext: undefined,
events: undefined,
}),
environmentName: this.garden.environmentName,
namespace: this.garden.namespace,
pluginName: providerName,
log: providerLog,
config: resolvedConfig,
configStore: this.garden.localConfigStore,
projectName: this.garden.projectName,
projectRoot: this.garden.projectRoot,
dependencies: resolvedProviders,
})
this.log.silly(() => `Validating ${providerName} config returned from configureProvider handler`)
resolvedConfig = validateConfig(configureOutput.config)
resolvedConfig.path = this.garden.projectRoot
if (configureOutput.moduleConfigs) {
moduleConfigs = configureOutput.moduleConfigs
}
// Validating the output config against the base plugins. This is important to make sure base handlers are
// compatible with the config.
const bases = getPluginBases(this.plugin, pluginsByName)
for (const base of bases) {
if (!base.configSchema) {
continue
}
this.log.silly(() => `Validating '${providerName}' config against '${base.name}' schema`)
resolvedConfig = <GenericProviderConfig>validateWithPath({
config: omit(resolvedConfig, "path"),
schema: base.configSchema.unknown(true),
path: this.garden.projectRoot,
projectRoot: this.garden.projectRoot,
configType: `provider configuration (base schema from '${base.name}' plugin)`,
ErrorClass: ConfigurationError,
source: { yamlDoc, basePath: yamlDocBasePath },
})
}
this.log.silly(() => `Ensuring ${providerName} provider is ready`)
providerLog.success("Provider configured")
const tmpProvider = providerFromConfig({
plugin: this.plugin,
config: resolvedConfig,
dependencies: resolvedProviders,
moduleConfigs,
status: defaultEnvironmentStatus,
})
const status = await this.ensurePrepared(tmpProvider)
return providerFromConfig({
plugin: this.plugin,
config: resolvedConfig,
dependencies: resolvedProviders,
moduleConfigs,
status,
})
}
private getCachePath() {
return getProviderStatusCachePath({
gardenDirPath: this.garden.gardenDirPath,
pluginName: this.plugin.name,
})
}
private hashConfig(config: GenericProviderConfig) {
return hashString(stableStringify(config))
}
private async getCachedStatus(config: GenericProviderConfig): Promise<EnvironmentStatus | null> {
const cachePath = this.getCachePath()
this.log.silly(() => `Checking provider status cache for ${this.plugin.name} at ${cachePath}`)
let cachedStatus: CachedStatus | null = null
if (!this.forceRefresh) {
try {
const cachedData = deserialize(await readFile(cachePath))
cachedStatus = validateSchema(cachedData, cachedStatusSchema)
} catch (err) {
// Can't find or read a cached status
this.log.silly(() => `Unable to find or read provider status from ${cachePath}: ${err}`)
}
}
if (!cachedStatus) {
return null
}
const configHash = this.hashConfig(config)
if (cachedStatus.configHash !== configHash) {
this.log.silly(() => `Cached provider status at ${cachePath} does not match the current config`)
return null
}
const ttl = gardenEnv.GARDEN_CACHE_TTL || defaultCacheTtl
const cacheAge = (new Date().getTime() - cachedStatus?.resolvedAt.getTime()) / 1000
if (cacheAge > ttl) {
this.log.silly(() => `Cached provider status at ${cachePath} is out of date`)
return null
}
return omit(cachedStatus, ["configHash", "resolvedAt"])
}
private async setCachedStatus(config: GenericProviderConfig, status: EnvironmentStatus) {
const cachePath = this.getCachePath()
this.log.silly(() => `Caching provider status for ${this.plugin.name} at ${cachePath}`)
const cachedStatus: CachedStatus = {
...status,
cached: true,
resolvedAt: new Date(),
configHash: this.hashConfig(config),
}
await ensureDir(dirname(cachePath))
await writeFile(cachePath, serialize(cachedStatus))
}
private async ensurePrepared(tmpProvider: Provider) {
const pluginName = tmpProvider.name
const providerLog = getProviderLog(pluginName, this.log)
const actions = await this.garden.getActionRouter()
const ctx = await this.garden.getPluginContext({
provider: tmpProvider,
templateContext: undefined,
events: undefined,
})
// Forward log events to the CLI.
// Probably should be solved similarly to
// https://github.com/garden-io/garden/blob/135ea041306f1d9093ae9d6547b1f862ca809f57/core/src/router/build.ts#L44C12-L44C12
// to have things consistent
// but currently the `getEnvironmentStatus` and `prepareEnvironment` handlers don't go through the router
ctx.events.on("log", ({ msg, origin, level }) => {
// stream logs to CLI
ctx.log[level]({ msg, origin })
})
// Check for cached provider status
const cachedStatus = await this.getCachedStatus(tmpProvider.config)
if (cachedStatus) {
providerLog.success(`Provider status cached`)
return cachedStatus
}
// TODO: avoid calling the handler manually (currently doing it to override the plugin context)
const handler = await actions.provider["getPluginHandler"]({
handlerType: "getEnvironmentStatus",
pluginName,
defaultHandler: async () => defaultEnvironmentStatus,
})
let status = await handler!({ ctx, log: providerLog })
if (this.forceInit || !status.ready) {
const statusMsg = status.ready
? `${styles.highlight("Ready")}, will ${styles.highlight("force re-initialize")}`
: `${styles.highlight("Not ready")}, will initialize`
providerLog.info(statusMsg)
// TODO: avoid calling the handler manually
const prepareHandler = await actions.provider["getPluginHandler"]({
handlerType: "prepareEnvironment",
pluginName,
defaultHandler: async () => ({ status }),
})
const result = await prepareHandler!({ ctx, log: providerLog, force: this.forceInit, status })
status = result.status
}
if (!status.ready) {
providerLog.error("Failed initializing provider")
throw new PluginError({
message: `Provider ${pluginName} reports status as not ready and could not prepare the configured environment.`,
})
}
providerLog.success("Provider ready")
if (!status.disableCache) {
await this.setCachedStatus(tmpProvider.config, status)
}
return status
}
}
export function getProviderStatusCachePath({
gardenDirPath,
pluginName,
}: {
gardenDirPath: string
pluginName: string
}) {
return join(gardenDirPath, CACHE_DIR_NAME, "provider-statuses", `${pluginName}.json`)
}