Skip to content

Update atmos.yaml config for Terraform/OpenTofu to enable passing the generated varfile to tofu init#1163

Merged
aknysh merged 38 commits intomainfrom
update-terraform-init
Mar 25, 2025
Merged

Update atmos.yaml config for Terraform/OpenTofu to enable passing the generated varfile to tofu init#1163
aknysh merged 38 commits intomainfrom
update-terraform-init

Conversation

@aknysh
Copy link
Member

@aknysh aknysh commented Mar 22, 2025

what

  • Update atmos.yaml config for Terraform/OpenTofu to enable passing the generated varfile to tofu init
  • Fix Atmos TUI (launched using atmos command) to always process Go templates and Atmos YAML functions
  • Update atmos.Component template function
  • Add unit tests
  • Update docs

why

components:
  terraform:
    # Can also be set using 'ATMOS_COMPONENTS_TERRAFORM_COMMAND' ENV var, or '--terraform-command' command-line argument
    command: tofu
    init:
      # Can also be set using 'ATMOS_COMPONENTS_TERRAFORM_INIT_PASS_VARS' ENV var, or '--init-pass-vars' command-line argument
      pass_vars: false

With this configuration, executing atmos terraform init component-1 -s nonprod will pass the generated varfile to the init command:

Executing command:
tofu init -reconfigure -var-file nonprod-component-1.terraform.tfvars.json

references

Summary by CodeRabbit

  • New Features

    • Added a new configuration option for Terraform initialization to control the passing of variable files. Users can now enable or disable this behavior via an environment variable or a command-line flag.
  • Documentation

    • Updated the CLI and configuration documentation to clearly explain the new Terraform initialization settings, ensuring enhanced clarity and improved guidance for setup and usage.

@aknysh aknysh added the minor New features that do not break anything label Mar 22, 2025
@aknysh aknysh self-assigned this Mar 22, 2025
@aknysh aknysh requested a review from a team as a code owner March 22, 2025 01:10
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 22, 2025

📝 Walkthrough

Walkthrough

The changes introduce a new initialization configuration for Terraform components within Atmos. A new init section with a pass_vars option (defaulting to false) has been added to the primary configuration file and related example files. This option is supported via an environment variable (ATMOS_COMPONENTS_TERRAFORM_INIT_PASS_VARS) and a CLI flag (--init-pass-vars). In addition, internal function signatures and parameter handling have been updated to pass configuration details more efficiently, logging has been revised, tests have been expanded, and documentation has been updated to incorporate the new functionality.

Changes

File(s) Change Summary
atmos.yaml, tests/fixtures/scenarios/.../atmos.yaml,
website/docs/cli/configuration/*.mdx, website/docs/core-concepts/projects/configuration/*.mdx,
website/docs/quick-start/*.mdx, tests/snapshots/*.golden
Added a new init section under components.terraform with a pass_vars key set to false, configurable via env var and CLI flag.
go.mod Updated dependency: github.com/redis/go-redis/v9 bumped from v9.7.1 to v9.7.3.
internal/exec/atmos.go, internal/exec/terraform.go, internal/exec/utils.go,
pkg/config/const.go, pkg/config/default.go, pkg/config/utils.go,
pkg/schema/schema.go, and related files in internal/exec/template_*.*.go and internal/exec/terraform_generate_*.*.go
Revised function signatures and parameter passing (using pointers and the new configAndStacksInfo object); added conditional logic to append the --var-file argument when pass_vars is enabled; updated logging for consistency and clarity.
tests/*_test.go, pkg/config/utils_test.go Introduced new tests and updated existing ones to verify the CLI flag, environment variable, Terraform init with varfile functionality, and template processing; also fixed a typo and updated snapshot logs.
Other documentation and configuration examples in
tests/fixtures/scenarios/terraform-init/*,
tests/fixtures/scenarios/stack-templates-3/*, and
main_hooks_and_store_integration_test.go
Added new example configuration files and refined documentation formatting and hyperlinks to clearly reflect the new configuration options.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLIParser
    participant ConfigProcessor
    participant ExecuteTerraform
    participant Terraform
    User->>CLIParser: Provide CLI args (--init-pass-vars)
    CLIParser->>ConfigProcessor: Parse CLI flags & ENV variables
    ConfigProcessor->>ExecuteTerraform: Pass configuration (init.pass_vars flag)
    ExecuteTerraform->>Terraform: Run 'terraform init' with --var-file if pass_vars enabled
    Terraform-->>ExecuteTerraform: Return execution results
    ExecuteTerraform-->>User: Output results
Loading

Possibly related PRs


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 783717f and 842ef07.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (1)
  • go.mod (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • go.mod
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Summary

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary or @auto-summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @auto-title anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
internal/exec/utils.go (1)

849-860: Initialize InitPassVars flag for command-line processing

The implementation follows the established pattern for flag processing in the codebase. However, it uses dynamic error creation which violates the static analysis rule.

Consider using wrapped static errors instead of dynamic errors:

-				return info, fmt.Errorf("invalid flag: %s", arg)
+				return info, fmt.Errorf("%w: %s", ErrInvalidFlag, arg)

This would require defining a new error variable like:

var ErrInvalidFlag = errors.New("invalid flag")
🧰 Tools
🪛 GitHub Check: golangci-lint

[failure] 851-851:
do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf("invalid flag: %s", arg)"


[failure] 857-857:
do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf("invalid flag: %s", arg)"

pkg/config/utils.go (1)

272-280: Added environment variable processing for InitPassVars

The code properly handles the ATMOS_COMPONENTS_TERRAFORM_INIT_PASS_VARS environment variable, but uses deprecated logging function.

Replace the deprecated u.LogDebug with log.Debug:

-		u.LogDebug(fmt.Sprintf("Found ENV var ATMOS_COMPONENTS_TERRAFORM_INIT_PASS_VARS=%s", componentsInitPassVars))
+		log.Debug("Found ENV var", "ATMOS_COMPONENTS_TERRAFORM_INIT_PASS_VARS", componentsInitPassVars)
🧰 Tools
🪛 GitHub Check: golangci-lint

[failure] 274-274:
SA1019: u.LogDebug is deprecated: Use log.Debug instead. This function will be removed in a future release. LogDebug logs the provided debug message

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5b58700 and 77b9ac8.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (19)
  • atmos.yaml (1 hunks)
  • cmd/list_components.go (1 hunks)
  • cmd/list_stacks.go (1 hunks)
  • go.mod (1 hunks)
  • internal/exec/atmos.go (2 hunks)
  • internal/exec/terraform.go (1 hunks)
  • internal/exec/utils.go (2 hunks)
  • pkg/config/const.go (1 hunks)
  • pkg/config/default.go (2 hunks)
  • pkg/config/utils.go (3 hunks)
  • pkg/list/list_components_test.go (2 hunks)
  • pkg/list/list_stacks_test.go (2 hunks)
  • pkg/schema/schema.go (3 hunks)
  • tests/fixtures/scenarios/atmos-configuration/atmos.d/tools/terraform.yaml (1 hunks)
  • website/docs/cli/configuration/components.mdx (1 hunks)
  • website/docs/cli/configuration/configuration.mdx (2 hunks)
  • website/docs/core-concepts/projects/configuration/opentofu.mdx (1 hunks)
  • website/docs/core-concepts/projects/configuration/terraform.mdx (3 hunks)
  • website/docs/quick-start/advanced/configure-cli.mdx (1 hunks)
🧰 Additional context used
🧬 Code Definitions (4)
cmd/list_components.go (2)
internal/exec/describe_stacks.go (1) (1)
  • ExecuteDescribeStacks (148-687)
pkg/describe/describe_stacks.go (1) (1)
  • ExecuteDescribeStacks (9-19)
pkg/config/default.go (1)
pkg/schema/schema.go (1) (1)
  • TerraformInit (261-263)
internal/exec/utils.go (5)
internal/exec/describe_component.go (1) (1)
  • configAndStacksInfo (106-106)
internal/exec/aws_eks_update_kubeconfig.go (1) (1)
  • configAndStacksInfo (125-125)
pkg/component/component_processor.go (2) (2)
  • configAndStacksInfo (19-19)
  • configAndStacksInfo (55-55)
pkg/config/const.go (1) (1)
  • InitPassVars (31-31)
pkg/config/cache.go (1) (1)
  • cfg (54-54)
pkg/schema/schema.go (1)
pkg/config/const.go (2) (2)
  • InitRunReconfigure (30-30)
  • InitPassVars (31-31)
🪛 GitHub Check: golangci-lint
pkg/config/utils.go

[failure] 274-274:
SA1019: u.LogDebug is deprecated: Use log.Debug instead. This function will be removed in a future release. LogDebug logs the provided debug message

internal/exec/utils.go

[failure] 851-851:
do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf("invalid flag: %s", arg)"


[failure] 857-857:
do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf("invalid flag: %s", arg)"

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Summary
🔇 Additional comments (30)
go.mod (1)

53-53: Updated Dependency Version Analysis

The dependency for github.com/redis/go-redis/v9 has been bumped from version v9.7.1 to v9.7.3. This minor version increment likely includes bug fixes and improvements. Please verify that this upgrade is compatible with existing usage within the codebase and confirm that there are no breaking changes introduced by this update.

tests/fixtures/scenarios/atmos-configuration/atmos.d/tools/terraform.yaml (1)

24-26: Clean addition of the init.pass_vars configuration option

The new configuration option adds support for passing variables during the Terraform/OpenTofu initialization phase. This directly implements the PR objective of enabling the passing of a generated varfile to the tofu init command.

The default value of false is appropriate for backward compatibility. The comment clearly explains how users can override this setting through environment variables or command-line arguments.

website/docs/core-concepts/projects/configuration/opentofu.mdx (3)

32-53: Well-structured documentation with complete configuration example

The addition of a comprehensive YAML configuration example provides clear guidance on how to configure OpenTofu with Atmos. The format is consistent with the existing documentation style, and all configuration options are properly commented.


55-74: Thorough explanation of configuration options

The description list format effectively explains each configuration option, including the new init.pass_vars setting. The documentation clearly states the purpose of each option and links to relevant external documentation for the OpenTofu init command.


69-73: Clear documentation of the new feature

The explanation for init.pass_vars is concise and informative. The reference to OpenTofu's documentation provides valuable context about the feature's purpose for dynamic backend configuration.

pkg/config/const.go (1)

31-31: Appropriate constant definition for the new CLI flag

The new constant InitPassVars is correctly placed alongside other initialization-related flags. This follows the established pattern in the codebase for defining command-line arguments.

pkg/config/default.go (2)

3-14: Cleaned up import statements

The import statements have been consolidated to remove duplicate entries for the errors and viper packages. This is a good practice that improves code readability.


42-44: Proper default configuration for the new feature

The addition of the Init struct with PassVars set to false provides a sensible default that maintains backward compatibility. This initialization properly configures the new feature and aligns with the YAML configuration defaults.

cmd/list_components.go (1)

55-55: Added template and function processing.

The parameters in the ExecuteDescribeStacks call have been updated to enable both Go template processing and YAML function processing. This change aligns with the PR objective to fix the Atmos TUI for consistent template and function processing.

cmd/list_stacks.go (1)

49-49: Enabled template and function processing consistently.

Similar to the change in list_components.go, the boolean parameters in ExecuteDescribeStacks have been updated to enable Go template and YAML function processing. This ensures consistent behavior across all components that interact with stack configurations.

internal/exec/atmos.go (2)

44-44: Enhanced TUI with template and function processing.

The parameters in the ExecuteDescribeStacks call have been updated to enable Go template and YAML function processing, matching the same changes made in other components. This ensures a consistent approach to processing stack manifests.


150-151: Added explicit template and function processing for Terraform commands.

Setting both ProcessTemplates and ProcessFunctions to true ensures that Terraform commands process Go templates and YAML functions correctly in the Atmos TUI, fulfilling one of the key objectives of this PR.

internal/exec/terraform.go (1)

369-373: Added support for passing varfile to tofu init.

This change implements the primary objective of the PR by adding logic to pass a variable file to the init command when the PassVars option is enabled in atmos.yaml. OpenTofu supports this feature for dynamic backend configuration, which enhances flexibility in managing Terraform configurations.

The implementation correctly uses the existing variable flag and file variables, maintaining consistency with other command implementations in the codebase.

website/docs/cli/configuration/components.mdx (1)

75-77: Documentation added for new Terraform init configuration

The addition of the pass_vars configuration option under the init section is well-documented with clear comments explaining how to set it via environment variables or command-line arguments.

website/docs/quick-start/advanced/configure-cli.mdx (1)

58-60: Documentation consistent with other files

The init.pass_vars configuration is consistently documented here, matching the implementation in the main configuration file.

atmos.yaml (1)

44-46: New feature for passing vars to tofu init

This configuration addition implements the main PR objective - enabling the passing of generated varfiles to the tofu init command. The default is appropriately set to false to maintain backward compatibility.

pkg/list/list_components_test.go (2)

24-25: Updated test parameters to reflect new functionality

The parameters for ExecuteDescribeStacks have been updated to accommodate the new configuration option. This ensures the tests properly validate the new functionality.


44-45: Consistent parameter updates in multiple test functions

The parameter changes maintain consistency across multiple test functions, ensuring all tests properly handle the new configuration option.

internal/exec/utils.go (1)

232-232: Correctly added InitPassVars field to configAndStacksInfo structure

The change propagates the InitPassVars setting from argsAndFlagsInfo to configAndStacksInfo, maintaining consistency with similar boolean flag handling patterns.

pkg/list/list_stacks_test.go (2)

21-21: Updated ExecuteDescribeStacks parameters to support template and function processing

The parameter values have been updated from all false to include true, true, false which enables Go template processing and YAML function handling when describing stacks in tests.


37-37: Consistent update of ExecuteDescribeStacks parameters

The same parameter changes were applied here for consistency with line 21, ensuring both test functions use the same processing capabilities.

website/docs/core-concepts/projects/configuration/terraform.mdx (2)

10-10: Added link to Terraform website

Improved documentation by adding a proper hyperlink to the Terraform website, enhancing user experience.


42-44: Added documentation for new init.pass_vars configuration option

Clear documentation for the new capability to pass variables during initialization, including environment variable and command-line flag information.

pkg/config/utils.go (1)

514-521: Added command-line argument processing for InitPassVars

The implementation properly handles the InitPassVars command-line argument, following the same pattern used for other boolean flags.

pkg/schema/schema.go (4)

258-259: New field for OpenTofu's variable passing feature.

The addition of Init field to the Terraform struct enables configuration for passing variables during initialization, which is supported by OpenTofu. Good placement and consistent tagging with the rest of the struct.


261-263: Well-structured new configuration struct.

Clean implementation of the TerraformInit struct with a boolean field PassVars that controls whether to pass generated variable files to the tofu init command. The struct follows the code style and tag patterns established in the codebase.


344-344: Added field for CLI argument handling.

The InitPassVars field in ArgsAndFlagsInfo struct will store information related to command-line arguments for the init pass vars feature. This aligns with how other CLI arguments are handled in the codebase.


403-403: Added field for component configuration info.

The InitPassVars field in ConfigAndStacksInfo struct will track the initialization variables configuration for use in command execution. This follows the established pattern for handling component configuration.

website/docs/cli/configuration/configuration.mdx (2)

60-61: Documentation for new configuration option.

Good addition of the init section with pass_vars: false to the default CLI configuration. The default value of false maintains backward compatibility for existing users.


697-697: Clear documentation for environment variable.

Well-documented environment variable with a descriptive comment explaining its purpose and linking to the OpenTofu documentation. This helps users understand how to use this feature for dynamic backend configuration.

coderabbitai[bot]
coderabbitai bot previously approved these changes Mar 22, 2025
coderabbitai[bot]
coderabbitai bot previously approved these changes Mar 22, 2025
@codecov
Copy link

codecov bot commented Mar 22, 2025

Codecov Report

Attention: Patch coverage is 38.01653% with 75 lines in your changes missing coverage. Please review.

Project coverage is 26.88%. Comparing base (76aed74) to head (8c12e55).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pkg/config/utils.go 8.88% 39 Missing and 2 partials ⚠️
internal/exec/template_funcs_component.go 68.57% 6 Missing and 5 partials ⚠️
internal/exec/utils.go 64.28% 4 Missing and 1 partial ⚠️
internal/exec/describe_stacks.go 0.00% 4 Missing ⚠️
internal/exec/atmos.go 0.00% 3 Missing ⚠️
...nternal/exec/template_funcs_gomplate_datasource.go 0.00% 3 Missing ⚠️
internal/exec/terraform.go 50.00% 2 Missing and 1 partial ⚠️
internal/exec/terraform_generate_backends.go 0.00% 2 Missing ⚠️
internal/exec/terraform_generate_varfiles.go 0.00% 2 Missing ⚠️
internal/exec/template_funcs.go 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1163      +/-   ##
==========================================
+ Coverage   25.87%   26.88%   +1.01%     
==========================================
  Files         180      180              
  Lines       20627    20662      +35     
==========================================
+ Hits         5337     5556     +219     
+ Misses      14443    14177     -266     
- Partials      847      929      +82     
Flag Coverage Δ
unittests 26.88% <38.01%> (+1.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (1)
internal/exec/utils.go (1)

850-861: ⚠️ Potential issue

Do not define dynamic errors, use wrapped static errors instead.

The error handling in this block uses dynamic error messages which was previously flagged in code scanning.

Instead of:

return info, fmt.Errorf("invalid flag: %s", arg)

Consider using wrapped static errors:

-return info, fmt.Errorf("invalid flag: %s", arg)
+var ErrInvalidFlag = errors.New("invalid flag")
+return info, fmt.Errorf("%w: %s", ErrInvalidFlag, arg)

This pattern should be applied at both lines 852 and 858.

🧹 Nitpick comments (8)
internal/exec/utils.go (1)

850-861: Consider refactoring repetitive flag handling patterns.

The flag handling code follows a highly repetitive pattern throughout the function. Consider refactoring this into a helper function to reduce duplication and make future additions easier.

+// processFlag is a helper function to process command line flags
+func processFlag(flag string, args []string, index int) (string, int, error) {
+    if index+1 >= len(args) {
+        return "", index, fmt.Errorf("invalid flag: %s", flag)
+    }
+    return args[index+1], index + 1, nil
+}
+
+// processFlagWithEquals is a helper function to process command line flags with equals
+func processFlagWithEquals(arg string, flag string) (string, error) {
+    parts := strings.Split(arg, "=")
+    if len(parts) != 2 {
+        return "", fmt.Errorf("invalid flag: %s", arg)
+    }
+    return parts[1], nil
+}

Then simplify the flag handling:

-if arg == cfg.InitPassVars {
-    if len(inputArgsAndFlags) <= (i + 1) {
-        return info, fmt.Errorf("invalid flag: %s", arg)
-    }
-    info.InitPassVars = inputArgsAndFlags[i+1]
-} else if strings.HasPrefix(arg+"=", cfg.InitPassVars) {
-    initPassVarsParts := strings.Split(arg, "=")
-    if len(initPassVarsParts) != 2 {
-        return info, fmt.Errorf("invalid flag: %s", arg)
-    }
-    info.InitPassVars = initPassVarsParts[1]
-}
+if arg == cfg.InitPassVars {
+    val, _, err := processFlag(arg, inputArgsAndFlags, i)
+    if err != nil {
+        return info, err
+    }
+    info.InitPassVars = val
+} else if strings.HasPrefix(arg+"=", cfg.InitPassVars) {
+    val, err := processFlagWithEquals(arg, cfg.InitPassVars)
+    if err != nil {
+        return info, err
+    }
+    info.InitPassVars = val
+}
internal/exec/template_funcs_component.go (1)

82-92: Conditional logging based on component type.

The updated logging logic now only logs Terraform outputs when the component is actually of Terraform type, which is more precise and prevents misleading log entries.

Consider updating the deprecated logging functions as highlighted by static analysis:

-u.LogTrace(fmt.Sprintf("Executed template function 'atmos.Component(%s, %s)'", component, stack))
+log.Debug(fmt.Sprintf("Executed template function 'atmos.Component(%s, %s)'", component, stack))

-u.LogTrace("'outputs' section:")
+log.Debug("'outputs' section:")

-u.LogError(err2)
+log.Error(err2)

-u.LogTrace(y)
+log.Debug(y)
🧰 Tools
🪛 GitHub Check: golangci-lint

[failure] 82-82:
SA1019: u.LogTrace is deprecated: Use log.Debug instead. This function will be removed in a future release. LogTrace logs the provided trace message


[failure] 85-85:
SA1019: u.LogTrace is deprecated: Use log.Debug instead. This function will be removed in a future release. LogTrace logs the provided trace message


[failure] 88-88:
SA1019: u.LogError is deprecated: Use log.Error instead. This function will be removed in a future release. LogError logs errors to std.Error


[failure] 90-90:
SA1019: u.LogTrace is deprecated: Use log.Debug instead. This function will be removed in a future release. LogTrace logs the provided trace message

internal/exec/template_funcs.go (3)

19-26: Optimize parameter passing by using pointers for large structs

The static analysis tool correctly points out that both atmosConfig and configAndStacksInfo are large structs (5736 bytes and 1136 bytes respectively). For better performance, pass these by pointer rather than by value to avoid unnecessary copying.

func FuncMap(
-	atmosConfig schema.AtmosConfiguration,
-	configAndStacksInfo schema.ConfigAndStacksInfo,
+	atmosConfig *schema.AtmosConfiguration,
+	configAndStacksInfo *schema.ConfigAndStacksInfo,
	ctx context.Context,
	gomplateData *data.Data,
) template.FuncMap {
🧰 Tools
🪛 GitHub Check: golangci-lint

[failure] 20-20:
hugeParam: atmosConfig is heavy (5736 bytes); consider passing it by pointer


[failure] 21-21:
hugeParam: configAndStacksInfo is heavy (1136 bytes); consider passing it by pointer


32-37: Update the AtmosFuncs struct to use pointers for large structs

The AtmosFuncs struct is storing copies of the large structs. Update it to store pointers to these structs to align with the suggested function signature change.

type AtmosFuncs struct {
-	atmosConfig         schema.AtmosConfiguration
-	configAndStacksInfo schema.ConfigAndStacksInfo
+	atmosConfig         *schema.AtmosConfiguration
+	configAndStacksInfo *schema.ConfigAndStacksInfo
	ctx                 context.Context
	gomplateData        *data.Data
}

39-41: Update Component method to match the pointer-based struct fields

If you implement the suggested changes for using pointers, update this method to match. Also, verify that componentFunc is updated to accept pointers to these structs.

func (f AtmosFuncs) Component(component string, stack string) (any, error) {
-	return componentFunc(f.atmosConfig, f.configAndStacksInfo, component, stack)
+	return componentFunc(*f.atmosConfig, *f.configAndStacksInfo, component, stack)
}

Alternatively, if componentFunc is updated to accept pointers:

func (f AtmosFuncs) Component(component string, stack string) (any, error) {
-	return componentFunc(f.atmosConfig, f.configAndStacksInfo, component, stack)
+	return componentFunc(f.atmosConfig, f.configAndStacksInfo, component, stack)
}
internal/exec/template_utils.go (3)

34-34: Update the call to FuncMap to match the suggested signature change

If you implement the pointer-based parameter passing in FuncMap, update this call to pass pointers to the structs.

- funcs := lo.Assign(gomplate.CreateFuncs(ctx, &d), sprig.FuncMap(), FuncMap(schema.AtmosConfiguration{}, schema.ConfigAndStacksInfo{}, ctx, &d))
+ config := &schema.AtmosConfiguration{}
+ stacksInfo := &schema.ConfigAndStacksInfo{}
+ funcs := lo.Assign(gomplate.CreateFuncs(ctx, &d), sprig.FuncMap(), FuncMap(config, stacksInfo, ctx, &d))

64-72: Optimize parameter passing in ProcessTmplWithDatasources by using pointers

Similar to the previous recommendation, use pointers for large struct parameters in this function signature.

func ProcessTmplWithDatasources(
-	atmosConfig schema.AtmosConfiguration,
-	configAndStacksInfo schema.ConfigAndStacksInfo,
+	atmosConfig *schema.AtmosConfiguration,
+	configAndStacksInfo *schema.ConfigAndStacksInfo,
	settingsSection schema.Settings,
	tmplName string,
	tmplValue string,
	tmplData any,
	ignoreMissingTemplateValues bool,
) (string, error) {

Update the subsequent code to dereference the pointers where necessary, for example:

- if !atmosConfig.Templates.Settings.Enabled {
+ if !atmosConfig.Templates.Settings.Enabled {
🧰 Tools
🪛 GitHub Check: golangci-lint

[failure] 66-66:
hugeParam: configAndStacksInfo is heavy (1136 bytes); consider passing it by pointer


148-150: Update the call to FuncMap to match the suggested signature change

If you implement the pointer-based parameter passing in FuncMap, update this call accordingly.

- funcs = lo.Assign(funcs, FuncMap(atmosConfig, configAndStacksInfo, context.TODO(), &d))
+ funcs = lo.Assign(funcs, FuncMap(&atmosConfig, &configAndStacksInfo, context.TODO(), &d))
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b067d5a and 2f1c103.

📒 Files selected for processing (8)
  • internal/exec/describe_stacks.go (2 hunks)
  • internal/exec/template_funcs.go (1 hunks)
  • internal/exec/template_funcs_component.go (2 hunks)
  • internal/exec/template_utils.go (3 hunks)
  • internal/exec/terraform_generate_backends.go (1 hunks)
  • internal/exec/terraform_generate_varfiles.go (1 hunks)
  • internal/exec/utils.go (3 hunks)
  • pkg/config/const.go (2 hunks)
🧰 Additional context used
🧬 Code Definitions (7)
internal/exec/describe_stacks.go (4)
internal/exec/utils.go (1) (1)
  • configAndStacksInfo (197-197)
internal/exec/describe_component.go (1) (1)
  • configAndStacksInfo (106-106)
internal/exec/aws_eks_update_kubeconfig.go (1) (1)
  • configAndStacksInfo (125-125)
pkg/component/component_processor.go (2) (2)
  • configAndStacksInfo (19-19)
  • configAndStacksInfo (55-55)
internal/exec/terraform_generate_varfiles.go (4)
internal/exec/utils.go (1) (1)
  • configAndStacksInfo (197-197)
internal/exec/aws_eks_update_kubeconfig.go (1) (1)
  • configAndStacksInfo (125-125)
pkg/component/component_processor.go (2) (2)
  • configAndStacksInfo (19-19)
  • configAndStacksInfo (55-55)
internal/exec/describe_component.go (1) (1)
  • configAndStacksInfo (106-106)
internal/exec/terraform_generate_backends.go (4)
internal/exec/utils.go (1) (1)
  • configAndStacksInfo (197-197)
internal/exec/aws_eks_update_kubeconfig.go (1) (1)
  • configAndStacksInfo (125-125)
pkg/component/component_processor.go (2) (2)
  • configAndStacksInfo (19-19)
  • configAndStacksInfo (55-55)
internal/exec/describe_component.go (1) (1)
  • configAndStacksInfo (106-106)
internal/exec/template_funcs_component.go (5)
pkg/schema/schema.go (3) (3)
  • AtmosConfiguration (13-45)
  • ConfigAndStacksInfo (363-431)
  • Logs (296-299)
internal/exec/utils.go (1) (1)
  • configAndStacksInfo (197-197)
internal/exec/describe_component.go (1) (1)
  • configAndStacksInfo (106-106)
pkg/config/const.go (1) (1)
  • TerraformComponentType (42-42)
internal/exec/terraform_outputs.go (2) (2)
  • err (70-70)
  • execTerraformOutput (61-265)
internal/exec/template_funcs.go (6)
internal/exec/aws_eks_update_kubeconfig.go (2) (2)
  • atmosConfig (126-126)
  • configAndStacksInfo (125-125)
pkg/schema/schema.go (3) (3)
  • AtmosConfiguration (13-45)
  • ConfigAndStacksInfo (363-431)
  • Context (301-315)
internal/exec/utils.go (1) (1)
  • configAndStacksInfo (197-197)
internal/exec/describe_component.go (1) (1)
  • configAndStacksInfo (106-106)
pkg/component/component_processor.go (2) (2)
  • configAndStacksInfo (19-19)
  • configAndStacksInfo (55-55)
internal/exec/template_funcs_component.go (1) (1)
  • componentFunc (16-96)
internal/exec/template_utils.go (2)
internal/exec/template_funcs.go (1) (1)
  • FuncMap (19-30)
pkg/schema/schema.go (2) (2)
  • AtmosConfiguration (13-45)
  • ConfigAndStacksInfo (363-431)
internal/exec/utils.go (5)
internal/exec/describe_component.go (1) (1)
  • configAndStacksInfo (106-106)
internal/exec/aws_eks_update_kubeconfig.go (1) (1)
  • configAndStacksInfo (125-125)
pkg/component/component_processor.go (2) (2)
  • configAndStacksInfo (19-19)
  • configAndStacksInfo (55-55)
pkg/config/const.go (1) (1)
  • InitPassVars (31-31)
pkg/config/cache.go (1) (1)
  • cfg (54-54)
🪛 GitHub Check: golangci-lint
internal/exec/template_funcs_component.go

[failure] 16-16:
cognitive complexity 28 of func componentFunc is high (> 20)


[failure] 17-17:
hugeParam: atmosConfig is heavy (5736 bytes); consider passing it by pointer


[failure] 18-18:
hugeParam: configAndStacksInfo is heavy (1136 bytes); consider passing it by pointer


[warning] 53-53:
if configAndStacksInfo.ComponentType == cfg.TerraformComponentType has complex nested blocks (complexity: 5)


[failure] 82-82:
SA1019: u.LogTrace is deprecated: Use log.Debug instead. This function will be removed in a future release. LogTrace logs the provided trace message


[failure] 85-85:
SA1019: u.LogTrace is deprecated: Use log.Debug instead. This function will be removed in a future release. LogTrace logs the provided trace message


[failure] 88-88:
SA1019: u.LogError is deprecated: Use log.Error instead. This function will be removed in a future release. LogError logs errors to std.Error


[failure] 90-90:
SA1019: u.LogTrace is deprecated: Use log.Debug instead. This function will be removed in a future release. LogTrace logs the provided trace message

internal/exec/template_funcs.go

[failure] 20-20:
hugeParam: atmosConfig is heavy (5736 bytes); consider passing it by pointer


[failure] 21-21:
hugeParam: configAndStacksInfo is heavy (1136 bytes); consider passing it by pointer

internal/exec/template_utils.go

[failure] 66-66:
hugeParam: configAndStacksInfo is heavy (1136 bytes); consider passing it by pointer

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Summary
🔇 Additional comments (9)
pkg/config/const.go (2)

31-31: New constant added for passing variables during initialization.

The InitPassVars constant is properly defined to support the new feature that enables passing a generated varfile to the tofu init command.


42-43: Component type constants improve code clarity.

Moving from string literals to named constants for component types improves code readability and maintainability. This is a good practice that reduces the risk of typos when referencing these types elsewhere in the codebase.

internal/exec/utils.go (2)

232-232: ConfigAndStacksInfo struct updated with new InitPassVars field.

The addition of the InitPassVars field to the configAndStacksInfo struct ensures that the new configuration option is properly propagated through the system.


515-515: Parameter passing updated in ProcessTmplWithDatasources.

The line change appears to maintain the correct behavior while supporting the new initialization parameters for template processing.

internal/exec/describe_stacks.go (2)

379-379: Consistent implementation for template processing.

Adding configAndStacksInfo to the ProcessTmplWithDatasources call provides access to additional context that ensures the template has access to component and stack metadata during processing.


581-581: Consistent parameter pattern across function calls.

The addition of configAndStacksInfo here maintains consistency with the previous change at line 379. This ensures both template processing calls for terraform and helmfile components have access to the same context information.

internal/exec/terraform_generate_varfiles.go (1)

250-250: Making template processing consistent with other functions.

Adding configAndStacksInfo to ProcessTmplWithDatasources ensures that variable file generation has access to the same stack and component information as other template operations. This change aligns with the PR objective to enable passing the generated varfile to tofu init.

internal/exec/terraform_generate_backends.go (1)

241-241: Maintaining consistency in template processing.

Adding configAndStacksInfo to the function call provides access to additional context for backend configuration generation, which is essential for the PR objective of enabling varfile passing during initialization.

internal/exec/template_funcs_component.go (1)

51-76: Improved conditional handling of Terraform remote state.

This refactoring adds proper component type checking before processing Terraform remote state, ensuring that the operations are only performed for Terraform components. The structural change also improves code organization by:

  1. First checking for static remote state configuration
  2. Falling back to executing terraform output if needed
  3. Properly handling the outputs in both cases

This change directly supports the PR objective by refining how component information is processed during initialization.

🧰 Tools
🪛 GitHub Check: golangci-lint

[warning] 53-53:
if configAndStacksInfo.ComponentType == cfg.TerraformComponentType has complex nested blocks (complexity: 5)

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (4)
internal/exec/template_funcs_component_test.go (4)

26-38: Consider using relative paths based on workDir for cleanup operations.

The current cleanup uses hardcoded paths. It would be more maintainable to construct these paths relative to the workDir variable defined later in the test.

 defer func() {
     // Delete the generated files and folders after the test
-    err := os.RemoveAll(filepath.Join("components", "terraform", "mock", ".terraform"))
+    err := os.RemoveAll(filepath.Join(workDir, "components", "terraform", "mock", ".terraform"))
     assert.NoError(t, err)

-    err = os.RemoveAll(filepath.Join("components", "terraform", "mock", "terraform.tfstate.d"))
+    err = os.RemoveAll(filepath.Join(workDir, "components", "terraform", "mock", "terraform.tfstate.d"))
     assert.NoError(t, err)

57-60: Consider mocking ExecuteTerraform for more isolated testing.

Directly calling ExecuteTerraform creates a dependency that may cause the test to fail for reasons unrelated to the componentFunc functionality you're testing. Consider creating a mock version for testing purposes.


70-74: Add more comprehensive assertions.

The current assertions check only three specific values. Consider adding more assertions to validate the complete structure and all expected values of the component configuration to ensure thorough test coverage.

For example:

 assert.Contains(t, y, "foo: component-1-a")
 assert.Contains(t, y, "bar: component-1-b")
 assert.Contains(t, y, "baz: component-1-b--component-1-c")
+// Add assertions for other expected configuration values
+assert.Contains(t, y, "expected_key: expected_value")
+// Consider asserting the absence of values that shouldn't be there
+assert.NotContains(t, y, "unexpected_key:")

16-16: Add more descriptive test documentation.

Consider adding a brief comment before the test function explaining what specifically this test is verifying, especially how it relates to the new feature of passing varfiles to tofu init.

+// TestComponentFunc verifies that component functions correctly process and render component
+// configurations, particularly focusing on the ability to pass variables to tofu/terraform commands
+// as part of the PR enhancement to enable passing of generated varfiles to tofu init
 func TestComponentFunc(t *testing.T) {
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d2e708a and 783717f.

📒 Files selected for processing (1)
  • internal/exec/template_funcs_component_test.go (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
internal/exec/template_funcs_component_test.go (2)
Learnt from: samtholiya
PR: cloudposse/atmos#1077
File: pkg/downloader/custom_github_detector.go:0-0
Timestamp: 2025-03-20T16:30:34.659Z
Learning: The `github.com/charmbracelet/log` package should be imported with the alias `log` according to the project's import alias configuration.
Learnt from: samtholiya
PR: cloudposse/atmos#1077
File: pkg/downloader/custom_github_detector_test.go:0-0
Timestamp: 2025-03-20T16:30:44.601Z
Learning: The `github.com/charmbracelet/log` package should be imported with the alias `log`, not `clog`.
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Summary
🔇 Additional comments (2)
internal/exec/template_funcs_component_test.go (2)

1-14: LGTM on imports and package declaration.

The import statement using log "github.com/charmbracelet/log" follows the project's aliasing conventions correctly.


16-25: Well-structured test setup.

The test function includes proper logging configuration and working directory tracking. Good practice to capture the starting working directory to ensure tests are isolated and don't impact the environment.

coderabbitai[bot]
coderabbitai bot previously approved these changes Mar 24, 2025
@aknysh aknysh merged commit 6b301d3 into main Mar 25, 2025
49 checks passed
@aknysh aknysh deleted the update-terraform-init branch March 25, 2025 00:58
@github-actions
Copy link

These changes were released in v1.167.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor New features that do not break anything

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants