Skip to content

Commit

Permalink
fix(pulumi): propagate resolved action context to pulumi plugin comma…
Browse files Browse the repository at this point in the history
…nds (#5324)

* fix(pulumi): propagate resolved action context to pulumi plugin commands

* test(pulumi):  use action references in varfiles to test the fix

* chore: remove misleading comment

Providers are already resolved and cached.
  • Loading branch information
vvagaytsev committed Nov 1, 2023
1 parent d460f3f commit 750e88d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
47 changes: 33 additions & 14 deletions plugins/pulumi/src/commands.ts
Expand Up @@ -40,7 +40,8 @@ import { TemplatableConfigContext } from "@garden-io/core/build/src/config/templ
import { ActionTaskProcessParams, ValidResultType } from "@garden-io/core/build/src/tasks/base"
import { deletePulumiDeploy } from "./handlers"
import { ActionLog, createActionLog, Log } from "@garden-io/core/build/src/logger/log-entry"
import { ActionConfigContext } from "@garden-io/core/build/src/config/template-contexts/actions"
import { ActionSpecContext } from "@garden-io/core/build/src/config/template-contexts/actions"
import { ProviderMap } from "@garden-io/core/build/src/config/provider"

type PulumiBaseParams = Omit<PulumiParams, "action">

Expand Down Expand Up @@ -181,15 +182,23 @@ const pulumiCommandSpecs: PulumiCommandSpec[] = [
},
]

const makePluginContextForDeploy = async (params: PulumiParams & { garden: Garden; graph: ConfigGraph }) => {
const { garden, provider, ctx, action } = params
const templateContext = new ActionConfigContext({
const makePluginContextForDeploy = async (
params: PulumiParams & {
garden: Garden
graph: ConfigGraph
resolvedProviders: ProviderMap
}
) => {
const { garden, graph, provider, resolvedProviders, ctx, action } = params
const templateContext = new ActionSpecContext({
garden,
config: action.getConfig(),
thisContextParams: {
name: action.name,
mode: action.mode(),
},
resolvedProviders,
action,
partialRuntimeResolution: false,
modules: graph.getModules(),
resolvedDependencies: action.getResolvedDependencies(),
executedDependencies: action.getExecutedDependencies(),
inputs: action.getInternal().inputs || {},
variables: action.getVariables(),
})
const ctxForDeploy = await garden.getPluginContext({ provider, templateContext, events: ctx.events })
Expand All @@ -206,6 +215,7 @@ interface PulumiPluginCommandTaskParams {
skipRuntimeDependencies: boolean
runFn: PulumiRunFn
pulumiParams: PulumiBaseParams
resolvedProviders: ProviderMap
}

export type PulumiCommandResult = ValidResultType
Expand All @@ -217,6 +227,7 @@ class PulumiPluginCommandTask extends PluginActionTask<PulumiDeploy, PulumiComma
commandDescription: string
override skipRuntimeDependencies: boolean
runFn: PulumiRunFn
resolvedProviders: ProviderMap

constructor({
garden,
Expand All @@ -228,6 +239,7 @@ class PulumiPluginCommandTask extends PluginActionTask<PulumiDeploy, PulumiComma
skipRuntimeDependencies = false,
runFn,
pulumiParams,
resolvedProviders,
}: PulumiPluginCommandTaskParams) {
super({
garden,
Expand All @@ -243,6 +255,7 @@ class PulumiPluginCommandTask extends PluginActionTask<PulumiDeploy, PulumiComma
this.pulumiParams = pulumiParams
const provider = <PulumiProvider>pulumiParams.ctx.provider
this.concurrencyLimit = provider.config.pluginTaskConcurrencyLimit
this.resolvedProviders = resolvedProviders
}

getDescription() {
Expand Down Expand Up @@ -278,6 +291,7 @@ class PulumiPluginCommandTask extends PluginActionTask<PulumiDeploy, PulumiComma
skipRuntimeDependencies: this.skipRuntimeDependencies,
runFn: this.runFn,
pulumiParams: this.pulumiParams,
resolvedProviders: this.resolvedProviders,
})
})

Expand All @@ -291,7 +305,11 @@ class PulumiPluginCommandTask extends PluginActionTask<PulumiDeploy, PulumiComma
async process({ dependencyResults }: ActionTaskProcessParams<PulumiDeploy, PulumiCommandResult>) {
this.log.info(chalk.gray(`Running ${chalk.white(this.commandDescription)}`))

const params = { ...this.pulumiParams, action: this.getResolvedAction(this.action, dependencyResults) }
const params = {
...this.pulumiParams,
action: this.getResolvedAction(this.action, dependencyResults),
resolvedProviders: this.resolvedProviders,
}

try {
await selectStack(params)
Expand Down Expand Up @@ -352,24 +370,25 @@ function makePulumiCommand({ name, commandDescription, beforeFn, runFn, afterFn

beforeFn && (await beforeFn({ ctx, log }))

const provider = ctx.provider as PulumiProvider

const pulumiProvider = ctx.provider as PulumiProvider
const actions = graph.getDeploys({ names }).filter((a) => a.type === "pulumi")
const resolvedProviders = await garden.resolveProviders(log)

const tasks = await Promise.all(
actions.map(async (action) => {
const templateContext = new TemplatableConfigContext(garden, action.getConfig())
const actionLog = createActionLog({ log, actionName: action.name, actionKind: action.kind })

const pulumiParams: PulumiBaseParams = {
ctx: await garden.getPluginContext({ provider, templateContext, events: ctx.events }),
provider,
ctx: await garden.getPluginContext({ provider: pulumiProvider, templateContext, events: ctx.events }),
provider: pulumiProvider,
log: actionLog,
}

return new PulumiPluginCommandTask({
garden,
graph,
resolvedProviders,
log: actionLog,
action,
commandName: name,
Expand Down
@@ -1 +1,2 @@
pulumi-k8s-test:appName: api-varfile1-override
pulumi-k8s-test:appName: api-varfile1-override
pulumi-k8s-test:nsName: ${actions["k8s-namespace"].name}-varfile2-override
@@ -1 +1,2 @@
pulumi-k8s-test:appName: api-varfile2-override
pulumi-k8s-test:appName: api-varfile2-override
pulumi-k8s-test:nsName: ${actions["k8s-namespace"].name}-varfile2-override

0 comments on commit 750e88d

Please sign in to comment.