Skip to content

Commit

Permalink
Update JSON schema for Atmos manifests validation. Update `!terraform…
Browse files Browse the repository at this point in the history
….output` YAML function (#1053)

* updates

* updates

* updates

* updates
  • Loading branch information
aknysh authored Feb 12, 2025
1 parent f10f64c commit 82bdaa6
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 7 deletions.
13 changes: 13 additions & 0 deletions examples/demo-context/schemas/atmos-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@
},
"providers": {
"$ref": "#/definitions/providers"
},
"hooks": {
"$ref": "#/definitions/hooks"
}
},
"required": [],
Expand Down Expand Up @@ -349,6 +352,10 @@
"description": "Custom configuration per component, not inherited by derived components",
"additionalProperties": true,
"title": "custom"
},
"locked": {
"type": "boolean",
"description": "Flag to lock the component and prevent modifications while allowing read operations"
}
},
"required": [],
Expand Down Expand Up @@ -731,6 +738,12 @@
"description": "Templates section",
"additionalProperties": true,
"title": "templates"
},
"hooks": {
"type": "object",
"description": "Hooks section",
"additionalProperties": true,
"title": "hooks"
}
}
}
13 changes: 13 additions & 0 deletions examples/demo-helmfile/schemas/atmos-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@
},
"providers": {
"$ref": "#/definitions/providers"
},
"hooks": {
"$ref": "#/definitions/hooks"
}
},
"required": [],
Expand Down Expand Up @@ -349,6 +352,10 @@
"description": "Custom configuration per component, not inherited by derived components",
"additionalProperties": true,
"title": "custom"
},
"locked": {
"type": "boolean",
"description": "Flag to lock the component and prevent modifications while allowing read operations"
}
},
"required": [],
Expand Down Expand Up @@ -731,6 +738,12 @@
"description": "Templates section",
"additionalProperties": true,
"title": "templates"
},
"hooks": {
"type": "object",
"description": "Hooks section",
"additionalProperties": true,
"title": "hooks"
}
}
}
13 changes: 13 additions & 0 deletions examples/demo-localstack/schemas/atmos-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@
},
"providers": {
"$ref": "#/definitions/providers"
},
"hooks": {
"$ref": "#/definitions/hooks"
}
},
"required": [],
Expand Down Expand Up @@ -349,6 +352,10 @@
"description": "Custom configuration per component, not inherited by derived components",
"additionalProperties": true,
"title": "custom"
},
"locked": {
"type": "boolean",
"description": "Flag to lock the component and prevent modifications while allowing read operations"
}
},
"required": [],
Expand Down Expand Up @@ -731,6 +738,12 @@
"description": "Templates section",
"additionalProperties": true,
"title": "templates"
},
"hooks": {
"type": "object",
"description": "Hooks section",
"additionalProperties": true,
"title": "hooks"
}
}
}
2 changes: 1 addition & 1 deletion examples/quick-start-advanced/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ARG GEODESIC_OS=debian
# https://atmos.tools/
# https://github.com/cloudposse/atmos
# https://github.com/cloudposse/atmos/releases
ARG ATMOS_VERSION=1.160.4
ARG ATMOS_VERSION=1.160.5

# Terraform: https://github.com/hashicorp/terraform/releases
ARG TF_VERSION=1.5.7
Expand Down
2 changes: 1 addition & 1 deletion go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@
},
"providers": {
"$ref": "#/definitions/providers"
},
"hooks": {
"$ref": "#/definitions/hooks"
}
},
"required": [],
Expand Down Expand Up @@ -735,6 +738,12 @@
"description": "Templates section",
"additionalProperties": true,
"title": "templates"
},
"hooks": {
"type": "object",
"description": "Hooks section",
"additionalProperties": true,
"title": "hooks"
}
}
}
60 changes: 58 additions & 2 deletions internal/exec/terraform_outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package exec
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
Expand All @@ -19,6 +20,44 @@ import (

var terraformOutputsCache = sync.Map{}

const (
cliArgsEnvVar = "TF_CLI_ARGS"
inputEnvVar = "TF_INPUT"
automationEnvVar = "TF_IN_AUTOMATION"
logEnvVar = "TF_LOG"
logCoreEnvVar = "TF_LOG_CORE"
logPathEnvVar = "TF_LOG_PATH"
logProviderEnvVar = "TF_LOG_PROVIDER"
reattachEnvVar = "TF_REATTACH_PROVIDERS"
appendUserAgentEnvVar = "TF_APPEND_USER_AGENT"
workspaceEnvVar = "TF_WORKSPACE"
disablePluginTLSEnvVar = "TF_DISABLE_PLUGIN_TLS"
skipProviderVerifyEnvVar = "TF_SKIP_PROVIDER_VERIFY"

varEnvVarPrefix = "TF_VAR_"
cliArgEnvVarPrefix = "TF_CLI_ARGS_"
)

var prohibitedEnvVars = []string{
cliArgsEnvVar,
inputEnvVar,
automationEnvVar,
logEnvVar,
logCoreEnvVar,
logPathEnvVar,
logProviderEnvVar,
reattachEnvVar,
appendUserAgentEnvVar,
workspaceEnvVar,
disablePluginTLSEnvVar,
skipProviderVerifyEnvVar,
}

var prohibitedEnvVarPrefixes = []string{
varEnvVarPrefix,
cliArgEnvVarPrefix,
}

func execTerraformOutput(
atmosConfig *schema.AtmosConfiguration,
component string,
Expand Down Expand Up @@ -131,8 +170,8 @@ func execTerraformOutput(
envMap, ok2 := envSection.(map[string]any)
if ok2 && len(envMap) > 0 {
l.Debug("Setting environment variables from the component's 'env' section", "env", envMap)
// Get all environment variables from the parent process
environMap := u.EnvironToMap()
// Get all environment variables (excluding the variables prohibited by terraform-exec/tfexec) from the parent process
environMap := environToMap()
// Add/override the environment variables from the component's 'env' section
for k, v := range envMap {
environMap[k] = fmt.Sprintf("%v", v)
Expand All @@ -142,6 +181,7 @@ func execTerraformOutput(
if err != nil {
return nil, err
}
l.Debug("Final environment variables", "environ", environMap)
}
}

Expand Down Expand Up @@ -339,3 +379,19 @@ func getStaticRemoteStateOutput(

return res
}

// environToMap converts all the environment variables (excluding the variables prohibited by terraform-exec/tfexec)
// in the environment into a map of strings
// TODO: review this (find another way to execute `terraform output` not using `terraform-exec/tfexec`)
func environToMap() map[string]string {
envMap := make(map[string]string)
for _, env := range os.Environ() {
pair := u.SplitStringAtFirstOccurrence(env, "=")
k := pair[0]
v := pair[1]
if !u.SliceContainsString(prohibitedEnvVars, k) && !u.SliceContainsStringStartsWith(prohibitedEnvVarPrefixes, k) {
envMap[k] = v
}
}
return envMap
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@
},
"providers": {
"$ref": "#/definitions/providers"
},
"hooks": {
"$ref": "#/definitions/hooks"
}
},
"required": [],
Expand Down Expand Up @@ -735,6 +738,12 @@
"description": "Templates section",
"additionalProperties": true,
"title": "templates"
},
"hooks": {
"type": "object",
"description": "Hooks section",
"additionalProperties": true,
"title": "hooks"
}
}
}
7 changes: 7 additions & 0 deletions tests/test-cases/atmos-functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ tests:
- "component-6"
- "-s"
- "nonprod"
env:
# The ENV variables will not be set in the child process (that executes the functions from `terraform-exec`)
# (because they are prohibited in the `terraform-exec` library)
TF_VAR_spacelift_stack: "test"
TF_CLI_ARGS_plan: "-refresh=false"
TF_IN_AUTOMATION: true
TF_APPEND_USER_AGENT: false
expect:
exit_code: 0
stdout:
Expand Down
2 changes: 1 addition & 1 deletion website/docs/integrations/atlantis.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ on:
branches: [ main ]
env:
ATMOS_VERSION: 1.160.4
ATMOS_VERSION: 1.160.5
ATMOS_CLI_CONFIG_PATH: ./
jobs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@
},
"providers": {
"$ref": "#/definitions/providers"
},
"hooks": {
"$ref": "#/definitions/hooks"
}
},
"required": [],
Expand Down Expand Up @@ -735,6 +738,12 @@
"description": "Templates section",
"additionalProperties": true,
"title": "templates"
},
"hooks": {
"type": "object",
"description": "Hooks section",
"additionalProperties": true,
"title": "hooks"
}
}
}

0 comments on commit 82bdaa6

Please sign in to comment.