Skip to content

Commit

Permalink
fix(workflows): defer provider resolution
Browse files Browse the repository at this point in the history
This fixes a regression introduced in `6382d3d` (emit stack graph & task
log events), where we began emitting `stackGraph` events before running
the workflow's steps.

This caused problems for workflows that rely on script steps to prepare
authentication for providers (e.g. k8s credentials).

We now simply emit the `stackGraph` event from the relevant step
commands.
  • Loading branch information
thsig committed Sep 23, 2021
1 parent e928f92 commit 039cca4
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 80 deletions.
4 changes: 0 additions & 4 deletions core/src/commands/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ export interface PrepareParams<T extends Parameters = {}, U extends Parameters =

export interface CommandParams<T extends Parameters = {}, U extends Parameters = {}> extends PrepareParams<T, U> {
garden: Garden
/**
* Only use when running a workflow step command (in which case `true` should be passed).
*/
isWorkflowStepCommand?: boolean
}

interface PrepareOutput {
Expand Down
3 changes: 1 addition & 2 deletions core/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export class BuildCommand extends Command<Args, Opts> {

async action({
garden,
isWorkflowStepCommand,
log,
footerLog,
args,
Expand All @@ -104,7 +103,7 @@ export class BuildCommand extends Command<Args, Opts> {

await garden.clearBuilds()

const graph = await garden.getConfigGraph({ log, emit: !isWorkflowStepCommand })
const graph = await garden.getConfigGraph({ log, emit: true })
const modules = graph.getModules({ names: args.modules })
const moduleNames = modules.map((m) => m.name)

Expand Down
4 changes: 2 additions & 2 deletions core/src/commands/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ export class CallCommand extends Command<Args> {
printHeader(headerLog, "Call", "telephone_receiver")
}

async action({ garden, isWorkflowStepCommand, log, args }: CommandParams<Args>): Promise<CommandResult<CallResult>> {
async action({ garden, log, args }: CommandParams<Args>): Promise<CommandResult<CallResult>> {
let [serviceName, path] = splitFirst(args.serviceAndPath, "/")

// TODO: better error when service doesn't exist
const graph = await garden.getConfigGraph({ log, emit: !isWorkflowStepCommand })
const graph = await garden.getConfigGraph({ log, emit: true })
const service = graph.getService(serviceName)
// No need for full context, since we're just checking if the service is running.
const runtimeContext = emptyRuntimeContext
Expand Down
8 changes: 4 additions & 4 deletions core/src/commands/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ export class DeleteEnvironmentCommand extends Command {
printHeader(headerLog, `Deleting environment`, "skull_and_crossbones")
}

async action({ garden, isWorkflowStepCommand, log }: CommandParams): Promise<CommandResult<DeleteEnvironmentResult>> {
async action({ garden, log }: CommandParams): Promise<CommandResult<DeleteEnvironmentResult>> {
const actions = await garden.getActionRouter()

const graph = await garden.getConfigGraph({ log, emit: !isWorkflowStepCommand })
const graph = await garden.getConfigGraph({ log, emit: true })
const serviceStatuses = await actions.deleteServices(graph, log)

log.info("")
Expand Down Expand Up @@ -159,8 +159,8 @@ export class DeleteServiceCommand extends Command {
printHeader(headerLog, "Delete service", "skull_and_crossbones")
}

async action({ garden, isWorkflowStepCommand, log, args }: CommandParams<DeleteServiceArgs>): Promise<CommandResult> {
const graph = await garden.getConfigGraph({ log, emit: !isWorkflowStepCommand })
async action({ garden, log, args }: CommandParams<DeleteServiceArgs>): Promise<CommandResult> {
const graph = await garden.getConfigGraph({ log, emit: true })
const services = graph.getServices({ names: args.services })

if (services.length === 0) {
Expand Down
3 changes: 1 addition & 2 deletions core/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ export class DeployCommand extends Command<Args, Opts> {

async action({
garden,
isWorkflowStepCommand,
log,
footerLog,
args,
Expand All @@ -149,7 +148,7 @@ export class DeployCommand extends Command<Args, Opts> {
this.server.setGarden(garden)
}

const initGraph = await garden.getConfigGraph({ log, emit: !isWorkflowStepCommand })
const initGraph = await garden.getConfigGraph({ log, emit: true })
let services = initGraph.getServices({ names: args.services, includeDisabled: true })

const disabled = services.filter((s) => s.disabled).map((s) => s.name)
Expand Down
3 changes: 1 addition & 2 deletions core/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ export class DevCommand extends Command<DevCommandArgs, DevCommandOpts> {

async action({
garden,
isWorkflowStepCommand,
log,
footerLog,
args,
Expand All @@ -128,7 +127,7 @@ export class DevCommand extends Command<DevCommandArgs, DevCommandOpts> {
this.garden = garden
this.server?.setGarden(garden)

const graph = await garden.getConfigGraph({ log, emit: !isWorkflowStepCommand })
const graph = await garden.getConfigGraph({ log, emit: true })
const modules = graph.getModules()

const skipTests = opts["skip-tests"]
Expand Down
9 changes: 2 additions & 7 deletions core/src/commands/get/get-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,9 @@ export class GetStatusCommand extends Command {
printHeader(headerLog, "Get status", "pager")
}

async action({
garden,
isWorkflowStepCommand,
log,
opts,
}: CommandParams): Promise<CommandResult<StatusCommandResult>> {
async action({ garden, log, opts }: CommandParams): Promise<CommandResult<StatusCommandResult>> {
const actions = await garden.getActionRouter()
const graph = await garden.getConfigGraph({ log, emit: !isWorkflowStepCommand })
const graph = await garden.getConfigGraph({ log, emit: true })

const envStatus = await garden.getEnvironmentStatus(log)
const serviceStatuses = await actions.getServiceStatuses({ log, graph })
Expand Down
9 changes: 2 additions & 7 deletions core/src/commands/get/get-task-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,10 @@ export class GetTaskResultCommand extends Command<Args> {
printHeader(headerLog, `Task result for task ${chalk.cyan(taskName)}`, "rocket")
}

async action({
garden,
isWorkflowStepCommand,
log,
args,
}: CommandParams<Args>): Promise<CommandResult<GetTaskResultCommandResult>> {
async action({ garden, log, args }: CommandParams<Args>): Promise<CommandResult<GetTaskResultCommandResult>> {
const taskName = args.name

const graph: ConfigGraph = await garden.getConfigGraph({ log, emit: !isWorkflowStepCommand })
const graph: ConfigGraph = await garden.getConfigGraph({ log, emit: true })
const task = graph.getTask(taskName)

const actions = await garden.getActionRouter()
Expand Down
9 changes: 2 additions & 7 deletions core/src/commands/get/get-test-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,11 @@ export class GetTestResultCommand extends Command<Args> {
)
}

async action({
garden,
isWorkflowStepCommand,
log,
args,
}: CommandParams<Args>): Promise<CommandResult<GetTestResultCommandResult>> {
async action({ garden, log, args }: CommandParams<Args>): Promise<CommandResult<GetTestResultCommandResult>> {
const testName = args.name
const moduleName = args.module

const graph = await garden.getConfigGraph({ log, emit: !isWorkflowStepCommand })
const graph = await garden.getConfigGraph({ log, emit: true })
const actions = await garden.getActionRouter()

const module = graph.getModule(moduleName)
Expand Down
3 changes: 1 addition & 2 deletions core/src/commands/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,12 @@ export class PublishCommand extends Command<Args, Opts> {

async action({
garden,
isWorkflowStepCommand,
log,
footerLog,
args,
opts,
}: CommandParams<Args, Opts>): Promise<CommandResult<PublishCommandResult>> {
const graph = await garden.getConfigGraph({ log, emit: !isWorkflowStepCommand })
const graph = await garden.getConfigGraph({ log, emit: true })
const modules = graph.getModules({ names: args.modules })

const results = await publishModules({
Expand Down
10 changes: 2 additions & 8 deletions core/src/commands/run/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,8 @@ export class RunTaskCommand extends Command<Args, Opts> {
printHeader(headerLog, msg, "runner")
}

async action({
garden,
isWorkflowStepCommand,
log,
args,
opts,
}: CommandParams<Args, Opts>): Promise<CommandResult<RunTaskOutput>> {
const graph = await garden.getConfigGraph({ log, emit: !isWorkflowStepCommand })
async action({ garden, log, args, opts }: CommandParams<Args, Opts>): Promise<CommandResult<RunTaskOutput>> {
const graph = await garden.getConfigGraph({ log, emit: true })
const task = graph.getTask(args.task, true)

if (task.disabled && !opts.force) {
Expand Down
10 changes: 2 additions & 8 deletions core/src/commands/run/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,11 @@ export class RunTestCommand extends Command<Args, Opts> {
printHeader(headerLog, `Running test ${chalk.cyan(args.test)}`, "runner")
}

async action({
garden,
isWorkflowStepCommand,
log,
args,
opts,
}: CommandParams<Args, Opts>): Promise<CommandResult<RunTestOutput>> {
async action({ garden, log, args, opts }: CommandParams<Args, Opts>): Promise<CommandResult<RunTestOutput>> {
const moduleName = args.module
const testName = args.test

const graph = await garden.getConfigGraph({ log, emit: !isWorkflowStepCommand })
const graph = await garden.getConfigGraph({ log, emit: true })
const module = graph.getModule(moduleName, true)

const testConfig = findByName(module.testConfigs, testName)
Expand Down
23 changes: 0 additions & 23 deletions core/src/commands/run/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export class RunWorkflowCommand extends Command<Args, {}> {
const steps = workflow.steps
const allStepNames = steps.map((s, i) => getStepName(i, s.name))
const startedAt = new Date().valueOf()
await maybeEmitStackGraphEvent(garden, log, workflow)

const result: WorkflowRunOutput = {
steps: {},
Expand Down Expand Up @@ -331,7 +330,6 @@ export async function runStepCommand({
headerLog,
args,
opts: merge(inheritedOpts, opts),
isWorkflowStepCommand: true, // <-----
})
return result
}
Expand Down Expand Up @@ -455,27 +453,6 @@ async function registerAndSetUid(garden: Garden, log: LogEntry, config: Workflow
}
}

/**
* If one or more steps of the workflow is a command with `streamEvents = true`, we prepare a `ConfigGraph` instance
* and stream a stack graph event.
*
* This is only done once at the beginning of the workflow to avoid streaming the graph for every subcommand having
* `streamEvents = true` (since we're also passing `isStepCommand = true` to the step command action in
* `runStepCommand`).
*/
async function maybeEmitStackGraphEvent(garden: Garden, log: LogEntry, config: WorkflowConfig) {
const shouldEmitStackGraphEvent = config.steps.find((s) => {
if (!s.command) {
return false
}
const { command } = pickCommand(getAllCommands(), s.command)
return command && command.streamEvents
})
if (shouldEmitStackGraphEvent) {
await garden.getConfigGraph({ log, emit: true })
}
}

async function writeWorkflowFile(garden: Garden, file: WorkflowFileSpec) {
let data: string

Expand Down
3 changes: 1 addition & 2 deletions core/src/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ export class TestCommand extends Command<Args, Opts> {

async action({
garden,
isWorkflowStepCommand,
log,
footerLog,
args,
Expand All @@ -131,7 +130,7 @@ export class TestCommand extends Command<Args, Opts> {
this.server.setGarden(garden)
}

const graph = await garden.getConfigGraph({ log, emit: !isWorkflowStepCommand })
const graph = await garden.getConfigGraph({ log, emit: true })
const skipDependants = opts["skip-dependants"]
let modules: GardenModule[]

Expand Down

0 comments on commit 039cca4

Please sign in to comment.