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

Config Generation: Go implementation for executing Terraform binary #1592

Merged
merged 2 commits into from
May 29, 2024

Conversation

Duologic
Copy link
Member

This PR refactors the generator to use https://github.com/hashicorp/terraform-exec/ to execute the Terraform binary. This removes the need for the user to provide the Terraform binary as part of their runtime environment, allowing the generator to be used as a library with less friction.

Copy link

In order to lower resource usage and have a faster runtime, PRs will not run Cloud tests automatically.
To do so, a Grafana Labs employee must trigger the cloud acceptance tests workflow manually.

@@ -125,3 +128,5 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/hashicorp/terraform-exec v0.21.0 => github.com/hrmsk66/terraform-exec v0.21.0
Copy link
Member Author

Choose a reason for hiding this comment

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

Using a fork awaiting the merge of this PR: hashicorp/terraform-exec#446

func setupTerraform(cfg *Config) (*tfexec.Terraform, error) {
installer := &releases.ExactVersion{
Product: product.Terraform,
Version: version.Must(version.NewVersion("1.8.4")),
Copy link
Member Author

Choose a reason for hiding this comment

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

I've pinned the version here for now, should probably become configurable.

Comment on lines -30 to +10
state, err := runTerraformWithOutput(dir, "show", "-json")
func getState(ctx context.Context, cfg *Config) (*tfjson.State, error) {
Copy link
Member Author

Choose a reason for hiding this comment

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

tfexec implements tfjson to read the state, hence the big changeset in this file.

Comment on lines 85 to 79
stack := stack{
name: stackName,
managementKey: cfg.Grafana.Auth,
url: cfg.Grafana.URL,
smToken: "",
smURL: "",
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Synthesizing a stack here to reduce the signatures of the generate functions.

return fmt.Errorf("failed to run terraform init: %w", err)
}
cfg.Terraform = tf
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure if this is the best place to put it, probably not but, eh 🤷

@Duologic Duologic marked this pull request as ready for review May 29, 2024 13:39
@Duologic Duologic requested a review from a team as a code owner May 29, 2024 13:39
Copy link
Member

@julienduchesne julienduchesne left a comment

Choose a reason for hiding this comment

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

Nice!

go.mod Outdated
@@ -33,6 +33,14 @@ require (
golang.org/x/text v0.15.0
)

require (
Copy link
Member

Choose a reason for hiding this comment

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

Merge this with the upper block IMO

Copy link
Member Author

Choose a reason for hiding this comment

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

I've just used go mod edit -replace and go mod tidy, stupid tools doing stupid things :P

@julienduchesne julienduchesne merged commit d58abb4 into main May 29, 2024
24 checks passed
@julienduchesne julienduchesne deleted the duologic/gonative_tfexec branch May 29, 2024 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants