Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement(terraform): enable streaming logs to cloud #6829

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/guides/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ Do not use this command. It has no effect.

## Garden Plugins

<h3 id="hadolintPlugin">The `Hadolint` plugin</h3>
<h3 id="deprecatedPlugins">The `conftest`, `conftest-container`, `conftest-kubernetes`, `hadolint` and `octant` plugins</h3>

Do not use this plugin.
These plugins are still enabled by default in Garden 0.13, but will be removed in Garden 0.14. Do not use these plugins explicitly in Garden 0.14.
13 changes: 13 additions & 0 deletions docs/reference/providers/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ providers:

# Use the specified Terraform workspace.
workspace:

# Set to `true` to make logs from Terraform Deploy actions visible in Garden Cloud/Enterprise. Defaults to `false`
streamLogsToCloud: false
```
## Configuration Keys

Expand Down Expand Up @@ -181,3 +184,13 @@ Use the specified Terraform workspace.
| -------- | -------- |
| `string` | No |

### `providers[].streamLogsToCloud`

[providers](#providers) > streamLogsToCloud

Set to `true` to make logs from Terraform Deploy actions visible in Garden Cloud/Enterprise. Defaults to `false`

| Type | Default | Required |
| --------- | ------- | -------- |
| `boolean` | `false` | No |

2 changes: 1 addition & 1 deletion plugins/terraform/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const deployTerraform: DeployActionHandler<"deploy", TerraformDeploy> = a
const root = getModuleStackRoot(action, spec)

if (spec.autoApply) {
await applyStack({ log, ctx, provider, root, variables: spec.variables, workspace })
await applyStack({ log, ctx, provider, root, variables: spec.variables, workspace, actionName: action.name })
} else {
const templateKey = `\${runtime.services.${action.name}.outputs.*}`
log.warn(
Expand Down
21 changes: 17 additions & 4 deletions plugins/terraform/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,24 @@ export async function getStackStatus(params: TerraformParamsWithVariables): Prom
}
}

export async function applyStack(params: TerraformParamsWithVariables) {
const { ctx, log, provider, root, variables } = params
export async function applyStack(params: TerraformParamsWithVariables & { actionName?: string }) {
const { ctx, log, provider, root, variables, actionName } = params

await ensureWorkspace(params)

const args = ["apply", "-auto-approve", "-input=false", ...(await prepareVariables(root, variables))]
const proc = await terraform(ctx, provider).spawn({ log, args, cwd: root })

const statusLine = log.createLog({}).info("→ Applying Terraform stack...")
const logEventContext = {
origin: "terraform",
level: "verbose" as const,
}
const statusLine = log
.createLog({
name: actionName || "terraform",
origin: "terraform",
})
.info("Applying Terraform stack...")
const logStream = split2()

let stderr = ""
Expand All @@ -207,7 +216,11 @@ export async function applyStack(params: TerraformParamsWithVariables) {
}

logStream.on("data", (line: Buffer) => {
statusLine.info(styles.primary("→ " + line.toString()))
const msg = line.toString()
statusLine.info(styles.primary(msg))
if (provider.config.streamLogsToCloud) {
ctx.events.emit("log", { timestamp: new Date().toISOString(), msg, ...logEventContext })
}
})

await new Promise<void>((_resolve, reject) => {
Expand Down
7 changes: 7 additions & 0 deletions plugins/terraform/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { joi } from "@garden-io/core/build/src/config/common.js"
export type TerraformProviderConfig = BaseProviderConfig &
TerraformBaseSpec & {
initRoot?: string
streamLogsToCloud: boolean
}

export type TerraformProvider = Provider<TerraformProviderConfig>
Expand Down Expand Up @@ -53,5 +54,11 @@ export const terraformProviderConfigSchema = providerConfigBaseSchema()
The version of Terraform to use. Set to \`null\` to use whichever version of \`terraform\` that is on your PATH.
`),
workspace: joi.string().description("Use the specified Terraform workspace."),
streamLogsToCloud: joi
.boolean()
.default(false)
.description(
`Set to \`true\` to make logs from Terraform Deploy actions visible in Garden Cloud/Enterprise. Defaults to \`false\``
),
})
.unknown(false)