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

Process Go templates and Atmos YAML functions only when executing atmos terraform commands that require it #1023

Merged
merged 5 commits into from
Feb 5, 2025

Conversation

aknysh
Copy link
Member

@aknysh aknysh commented Feb 5, 2025

what

  • Process Go templates and Atmos YAML functions only when executing atmos terraform commands that require it
  • Refactor terraform-related code, move the utility functions into terraform_utils.go

why

  • Only the following atmos terraform commands require processing of Go templates and Atmos YAML functions in Atmos manifests:

    • plan
    • apply
    • deploy
    • destroy
    • generate
  • All other atmos terraform command don't need the outputs of the templates and functions (e.g. atmos terraform init or atmos terraform workspace)

  • Speed up the commands execution

  • Some YAML functions like !terraform.output require assuming roles, and not executing it simplifies CI/CD workflows

Summary by CodeRabbit

  • New Features

    • Enhanced Terraform command functionality now offers automated configuration processing for smoother operations.
    • Atmos integrations have been updated—Docker setups and workflows now use Atmos version 1.160.0.
  • Bug Fixes

    • Improved error messaging during command execution provides clearer guidance on required inputs.
  • Chores

    • Upgraded various dependencies for enhanced stability, performance, and overall system robustness.

@aknysh aknysh added the minor New features that do not break anything label Feb 5, 2025
@aknysh aknysh requested review from osterman and milldr February 5, 2025 03:49
@aknysh aknysh self-assigned this Feb 5, 2025
@aknysh aknysh requested a review from a team as a code owner February 5, 2025 03:49
Copy link

mergify bot commented Feb 5, 2025

Important

Cloud Posse Engineering Team Review Required

This pull request modifies files that require Cloud Posse's review. Please be patient, and a core maintainer will review your changes.

To expedite this process, reach out to us on Slack in the #pr-reviews channel.

@mergify mergify bot added the needs-cloudposse Needs Cloud Posse assistance label Feb 5, 2025
Copy link
Contributor

coderabbitai bot commented Feb 5, 2025

📝 Walkthrough

Walkthrough

This pull request introduces changes to various command and internal files. A new Contains function is added in one command utility file while its previous implementation is removed from another. In addition, the terraform command flow has been refactored with enhanced error logging, updated import order, and Atmos version bumps. Internal execution logic for Terraform has been reworked by removing some legacy helper functions and adding new ones for stack validation, backend configuration, and provider overrides. Several dependency versions in the module have also been updated.

Changes

File(s) Change Summary
cmd/cmd_utils.go
cmd/terraform.go
Introduces a new Contains function in cmd/cmd_utils.go for reusable string checks; removes the duplicate Contains function from cmd/terraform.go, restructures the init function, and adds error handling for actualCmd.Usage().
cmd/terraform_commands.go Adjusts import statements without modifying functionality or public declarations.
examples/quick-start-advanced/Dockerfile
website/docs/integrations/atlantis.mdx
Updates the Atmos version from 1.158.0 to 1.160.0 in both the Dockerfile and GitHub Actions workflow configuration.
go.mod Increments versions for multiple dependencies including AWS SDK packages, Charmbracelet libraries, and Go modules like golang.org/x/oauth2, golang.org/x/term, and golang.org/x/sys.
internal/exec/terraform.go
internal/exec/terraform_utils.go
Removes legacy functions for stack processing and backend configuration from terraform.go and introduces new helper functions (shouldProcessStacks, generateBackendConfig, generateProviderOverrides, needProcessTemplatesAndYamlFunctions) in terraform_utils.go to streamline Terraform command execution.

Sequence Diagram(s)

sequenceDiagram
    participant User as User
    participant TR as terraformRun
    participant AC as actualCmd.Usage
    participant Log as Logger

    User->>TR: Invoke Terraform command
    TR->>AC: Call actualCmd.Usage()
    AC-->>TR: Return usage result or error
    alt Error Occurs
        TR->>Log: Log the error
    else No Error
        TR->>User: Proceed with command execution
    end
Loading
sequenceDiagram
    participant Exec as ExecuteTerraform
    participant Utils as Terraform Utils
    participant FS as Filesystem/Logger

    Exec->>Utils: needProcessTemplatesAndYamlFunctions(command)
    alt Processing required
        Utils-->>Exec: true
        Exec->>Utils: shouldProcessStacks(info)
        Utils-->>Exec: Return processing flags
        Exec->>Utils: generateBackendConfig(atmosConfig, info, workingDir)
        Exec->>Utils: generateProviderOverrides(atmosConfig, info, workingDir)
        Utils->>FS: Write config files (unless dry run)
    else Not required
        Utils-->>Exec: false
    end
Loading

Possibly related PRs

Suggested reviewers

  • osterman
  • milldr

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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. (Beta)
  • @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 (1)
cmd/cmd_utils.go (1)

609-617: Consider using slices.Contains for better maintainability.

The implementation is correct, but since Go 1.21+, you can use the standard library's slices.Contains function for a more idiomatic solution.

-// Contains checks if a slice of strings contains an exact match for the target string.
-func Contains(slice []string, target string) bool {
-	for _, item := range slice {
-		if item == target {
-			return true
-		}
-	}
-	return false
-}
+import "slices"
+
+// Contains checks if a slice of strings contains an exact match for the target string.
+func Contains(slice []string, target string) bool {
+	return slices.Contains(slice, target)
+}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 300ca0f and dfe866d.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (8)
  • cmd/cmd_utils.go (1 hunks)
  • cmd/terraform.go (3 hunks)
  • cmd/terraform_commands.go (1 hunks)
  • examples/quick-start-advanced/Dockerfile (1 hunks)
  • go.mod (6 hunks)
  • internal/exec/terraform.go (1 hunks)
  • internal/exec/terraform_utils.go (1 hunks)
  • website/docs/integrations/atlantis.mdx (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • cmd/terraform_commands.go
  • website/docs/integrations/atlantis.mdx
🧰 Additional context used
📓 Learnings (1)
examples/quick-start-advanced/Dockerfile (2)
Learnt from: aknysh
PR: cloudposse/atmos#775
File: examples/quick-start-advanced/Dockerfile:9-9
Timestamp: 2024-11-12T05:52:05.088Z
Learning: It is acceptable to set `ARG ATMOS_VERSION` to a future version like `1.105.0` in `examples/quick-start-advanced/Dockerfile` if that will be the next release.
Learnt from: osterman
PR: cloudposse/atmos#801
File: examples/quick-start-advanced/Dockerfile:9-9
Timestamp: 2024-11-23T00:13:22.004Z
Learning: When updating the `ATMOS_VERSION` in Dockerfiles, the team prefers to pin to the next future version when the PR merges, even if the version is not yet released.
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Build (macos-latest, macos)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Summary
🔇 Additional comments (11)
internal/exec/terraform.go (3)

56-57: Looks solid.
This newly added comment and function call help clarify why stack processing can be skipped when cleaning.


60-61: Good approach to selectively process templates.
This aligns with the PR’s goal of processing templates and YAML functions only on certain Terraform subcommands.


67-67: Error message update is reasonable.
It’s now more general and consistent with the new usage flow.

cmd/terraform.go (2)

6-6: Neat reorganization of imports and flags.
Using DisableFlagParsing and persistent flags allows for custom handling of Terraform arguments.

Also applies to: 24-29


54-66: Improved help and error handling.
Capturing and logging Usage() errors is a nice defensive measure. Clean integration with ExecuteTerraform.

internal/exec/terraform_utils.go (4)

33-46: Simple, readable logic for stack processing.
This function clarifies the conditions to skip or check stack usage, aiding maintainability.


48-70: Pragmatic backend config generation.
Auto-generating the backend.tf.json is concise. Consider unit testing for edge cases like invalid workspace.


72-87: Provider overrides handled well.
Keeping it JSON-based ensures consistency. Suggest verifying no sensitive data is unintentionally logged.


89-100: Command filtering improves performance.
Only processing templates for the listed commands helps meet the PR’s main objective efficiently.

examples/quick-start-advanced/Dockerfile (1)

9-9: LGTM! Version update aligns with team preferences.

The update to ATMOS_VERSION=1.160.0 is consistent with the team's practice of pinning to future versions when the PR merges.

go.mod (1)

12-14: LGTM! Routine dependency updates.

The dependency version updates are minor version bumps that keep the project up to date with the latest stable releases.

Also applies to: 16-16, 59-60, 95-95, 109-109, 122-122, 295-295

@aknysh aknysh merged commit 6272c53 into main Feb 5, 2025
48 of 49 checks passed
@aknysh aknysh deleted the update-terraform-commands branch February 5, 2025 14:02
@mergify mergify bot removed the needs-cloudposse Needs Cloud Posse assistance label Feb 5, 2025
Copy link

github-actions bot commented Feb 5, 2025

These changes were released in v1.160.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