Skip to content

Commit

Permalink
Merge branch 'main' into DEV-2804
Browse files Browse the repository at this point in the history
  • Loading branch information
osterman authored Feb 18, 2025
2 parents 7934498 + a948928 commit 53a10b7
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 6 deletions.
3 changes: 2 additions & 1 deletion internal/exec/helmfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ func ExecuteHelmfile(info schema.ConfigAndStacksInfo) error {
if atmosConfig.Components.Helmfile.UseEKS {
envVars = append(envVars, envVarsEKS...)
}

envVars = append(envVars, fmt.Sprintf("ATMOS_CLI_CONFIG_PATH=%s", atmosConfig.CliConfigPath))
envVars = append(envVars, fmt.Sprintf("ATMOS_BASE_PATH=%s", atmosConfig.BasePath))
u.LogTrace("Using ENV vars:")
for _, v := range envVars {
u.LogTrace(v)
Expand Down
3 changes: 2 additions & 1 deletion internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
u.LogWarning(fmt.Sprintf("detected '%s' set in the environment; this may interfere with Atmos's control of Terraform.", varName))
}
}

info.ComponentEnvList = append(info.ComponentEnvList, fmt.Sprintf("ATMOS_CLI_CONFIG_PATH=%s", atmosConfig.CliConfigPath))
info.ComponentEnvList = append(info.ComponentEnvList, fmt.Sprintf("ATMOS_BASE_PATH=%s", atmosConfig.BasePath))
// Set `TF_IN_AUTOMATION` ENV var to `true` to suppress verbose instructions after terraform commands
// https://developer.hashicorp.com/terraform/cli/config/environment-variables#tf_in_automation
info.ComponentEnvList = append(info.ComponentEnvList, "TF_IN_AUTOMATION=true")
Expand Down
18 changes: 16 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func InitCliConfig(configAndStacksInfo schema.ConfigAndStacksInfo, processStacks

// Process config in system folder
configFilePath1 := ""

atmosConfigFilePath := ""
// https://pureinfotech.com/list-environment-variables-windows-10/
// https://docs.microsoft.com/en-us/windows/deployment/usmt/usmt-recognized-environment-variables
// https://softwareengineering.stackexchange.com/questions/299869/where-is-the-appropriate-place-to-put-application-configuration-files-for-each-p
Expand All @@ -161,6 +161,7 @@ func InitCliConfig(configAndStacksInfo schema.ConfigAndStacksInfo, processStacks
}
if found {
configFound = true
atmosConfigFilePath = configFile1
}
}

Expand All @@ -176,6 +177,7 @@ func InitCliConfig(configAndStacksInfo schema.ConfigAndStacksInfo, processStacks
}
if found {
configFound = true
atmosConfigFilePath = configFile2
}

// Process config in the current dir
Expand All @@ -190,6 +192,7 @@ func InitCliConfig(configAndStacksInfo schema.ConfigAndStacksInfo, processStacks
}
if found {
configFound = true
atmosConfigFilePath = configFile3
}

// Process config from the path in ENV var `ATMOS_CLI_CONFIG_PATH`
Expand All @@ -203,6 +206,7 @@ func InitCliConfig(configAndStacksInfo schema.ConfigAndStacksInfo, processStacks
}
if found {
configFound = true
atmosConfigFilePath = configFile4
}
}

Expand All @@ -217,6 +221,7 @@ func InitCliConfig(configAndStacksInfo schema.ConfigAndStacksInfo, processStacks
}
if found {
configFound = true
atmosConfigFilePath = configFile5
}
}
}
Expand Down Expand Up @@ -256,7 +261,16 @@ func InitCliConfig(configAndStacksInfo schema.ConfigAndStacksInfo, processStacks
if err != nil {
return atmosConfig, err
}

// Set the CLI config path in the atmosConfig struct
if filepath.IsAbs(atmosConfigFilePath) {
atmosConfig.CliConfigPath = atmosConfigFilePath
} else {
absPath, err := filepath.Abs(atmosConfigFilePath)
if err != nil {
return atmosConfig, err
}
atmosConfig.CliConfigPath = absPath
}
// Process ENV vars
err = processEnvVars(&atmosConfig)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ type AtmosConfiguration struct {
// Stores is never read from yaml, it is populated in processStoreConfig and it's used to pass to the populated store
// registry through to the yaml parsing functions when !store is run and to pass the registry to the hooks
// functions to be able to call stores from within hooks.
Stores store.StoreRegistry `yaml:"stores_registry,omitempty" json:"stores_registry,omitempty" mapstructure:"stores_registry"`
Stores store.StoreRegistry `yaml:"stores_registry,omitempty" json:"stores_registry,omitempty" mapstructure:"stores_registry"`
CliConfigPath string `yaml:"cli_config_path" json:"cli_config_path,omitempty" mapstructure:"cli_config_path"`
}

type Validate struct {
Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/components/terraform/env-example/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
required_providers {
environment = {
source = "EppO/environment"
version = "~> 1.3.0" # Check for latest version
}
}
}

# Get all environment variables matching patterns
data "environment_variables" "required" {
filter = "^ATMOS_.*|^EXAMPLE$" # Regex pattern
}
25 changes: 25 additions & 0 deletions tests/fixtures/components/terraform/env-example/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
output "atmos_cli_config_path" {
value = data.environment_variables.required.items["ATMOS_CLI_CONFIG_PATH"]
description = "The path to the Atmos CLI configuration file"
}

output "atmos_base_path" {
value = data.environment_variables.required.items["ATMOS_BASE_PATH"]
description = "The base path used by Atmos"
}

output "example" {
value = data.environment_variables.required.items["EXAMPLE"]
description = "Example environment variable"
}

# Output all matched variables
output "all_atmos_vars" {
value = data.environment_variables.required.items
description = "All matched environment variables"
}

variable "stage" {
type = string
description = "Deployment stage/environment (e.g., dev, prod)"
}
21 changes: 21 additions & 0 deletions tests/fixtures/scenarios/env/atmos.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
base_path: "./"

components:
terraform:
base_path: "../../components/terraform"
apply_auto_approve: true
deploy_run_init: true
init_run_reconfigure: true
auto_generate_backend_file: false

stacks:
base_path: "stacks"
included_paths:
- "deploy/**/*"
excluded_paths:
- "**/_defaults.yaml"
name_pattern: "{stage}"

logs:
file: "/dev/stderr"
level: Info
10 changes: 10 additions & 0 deletions tests/fixtures/scenarios/env/stacks/catalog/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# yaml-language-server: $schema=https://atmos.tools/schemas/atmos/atmos-manifest/1.0/atmos-manifest.json
env:
EXAMPLE: "test"

components:
terraform:
env-example:
metadata:
component: env-example
vars: {}
12 changes: 12 additions & 0 deletions tests/fixtures/scenarios/env/stacks/deploy/dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# yaml-language-server: $schema=https://atmos.tools/schemas/atmos/atmos-manifest/1.0/atmos-manifest.json

vars:
stage: dev

import:
- catalog/example

components:
terraform:
env-example:
vars: {}
12 changes: 12 additions & 0 deletions tests/fixtures/scenarios/env/stacks/deploy/prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# yaml-language-server: $schema=https://atmos.tools/schemas/atmos/atmos-manifest/1.0/atmos-manifest.json

vars:
stage: prod

import:
- catalog/example

components:
terraform:
env-example:
vars: {}
12 changes: 12 additions & 0 deletions tests/fixtures/scenarios/env/stacks/deploy/staging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# yaml-language-server: $schema=https://atmos.tools/schemas/atmos/atmos-manifest/1.0/atmos-manifest.json

vars:
stage: staging

import:
- catalog/example

components:
terraform:
env-example:
vars: {}

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

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

22 changes: 22 additions & 0 deletions tests/test-cases/env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# yaml-language-server: $schema=schema.json
tests:
- name: atmos_terraform_apply_env
snapshot: false
enabled: true
description: "Ensure atmos export atmos_base_path and atmos_cli_config_path"
workdir: "fixtures/scenarios/env"
command: "atmos"
args:
- "terraform"
- "apply"
- "env-example"
- "-s"
- "dev"
expect:
diff: []
stdout:
- 'atmos_base_path = "./"'
- 'atmos_cli_config_path = ".*tests.*fixtures.*scenarios.*env.*atmos"'
stderr:
- "^$"
exit_code: 0

0 comments on commit 53a10b7

Please sign in to comment.