Skip to content

Commit

Permalink
Initialize tfexec.Terraform once per request + refactoring/cleanup (r…
Browse files Browse the repository at this point in the history
…adius-project#6630)

# Description

* Terraform instance along with logging configuration is being called
multiple times for same request - consolidating it to be initialized
only once.

* Move working directory creation to tfexec.Terraform initialization to
remove duplicated code.

## Type of change

<!--

Please select **one** of the following options that describes your
change and delete the others. Clearly identifying the type of change you
are making will help us review your PR faster, and is used in authoring
release notes.

If you are making a bug fix or functionality change to Radius and do not
have an associated issue link please create one now.

-->

- This pull request fixes a bug in Radius and has an approved issue
(issue link required).

<!--

Please update the following to link the associated issue. This is
required for some kinds of changes (see above).

-->

Fixes: radius-project#6456

## Auto-generated summary

<!--
GitHub Copilot for docs will auto-generate a summary of the PR
-->

<!--
copilot:all
-->
### <samp>🤖 Generated by Copilot at b523d6e</samp>

### Summary
📝🛠️🚀

<!--
1. 📝 - This emoji represents the addition of the log.go file and the
types_test.go file, which implement and test new features for the
terraform package. The emoji also represents the refactoring of the test
cases and the error messages in the package, which improve the
documentation and clarity of the code.
2. 🛠️ - This emoji represents the refactoring of the terraform package
in execute.go and module_test.go, which use the tfexec package and the
new functions in types.go to simplify the function signatures and reduce
the duplication of parameters. The emoji also represents the fixing of
the import alias and the resource types in the show command test cases,
which improve the consistency and readability of the code.
3. 🚀 - This emoji represents the enhancement of the terraform package in
types.go, which adds functions and constants to create and configure a
working directory for Terraform execution and logging. The emoji also
represents the use of the tfLogWrapper struct in log.go, which streams
the Terraform logs to the Radius logger. These features improve the
performance and user experience of the terraform package.
-->
Refactored the terraform package to use the `tfexec.Terraform` type and
a custom logger. Improved the consistency and readability of the tests
and the code. Added new files `log.go` and `types_test.go` to implement
and test the new features.

> _Oh we're the `terraform` crew and we've got work to do_
> _We'll refactor and improve our code with `tfexec` too_
> _We'll stream our logs and test our types and make our imports clear_
> _So heave away, me hearties, heave away with cheer_

### Walkthrough
* Fix import alias for datastoresrp package in `show_test.go`
([link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-13fc0c59e89a1fa92926d4fdd0ef9a6ef372c9816c6c43ec6b003427b63e0852L33-R33),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-13fc0c59e89a1fa92926d4fdd0ef9a6ef372c9816c6c43ec6b003427b63e0852L49-R49),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-13fc0c59e89a1fa92926d4fdd0ef9a6ef372c9816c6c43ec6b003427b63e0852L58-R58),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-13fc0c59e89a1fa92926d4fdd0ef9a6ef372c9816c6c43ec6b003427b63e0852L67-R67),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-13fc0c59e89a1fa92926d4fdd0ef9a6ef372c9816c6c43ec6b003427b63e0852L76-R76),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-13fc0c59e89a1fa92926d4fdd0ef9a6ef372c9816c6c43ec6b003427b63e0852L114-R114),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-13fc0c59e89a1fa92926d4fdd0ef9a6ef372c9816c6c43ec6b003427b63e0852L148-R148),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-13fc0c59e89a1fa92926d4fdd0ef9a6ef372c9816c6c43ec6b003427b63e0852L190-R190),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-13fc0c59e89a1fa92926d4fdd0ef9a6ef372c9816c6c43ec6b003427b63e0852L225-R225))
* Refactor Terraform executor creation and configuration in `types.go`
and `log.go`
([link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-0ad85ed086350c77c3e366408120b4c3020d1cec542671ab3586fd0c34dafe6fL22-R25),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-0ad85ed086350c77c3e366408120b4c3020d1cec542671ab3586fd0c34dafe6fL30-R37),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-0ad85ed086350c77c3e366408120b4c3020d1cec542671ab3586fd0c34dafe6fL59-R71),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-0ad85ed086350c77c3e366408120b4c3020d1cec542671ab3586fd0c34dafe6fL71-R92),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-cd97896943008b717cf4f1a771f6dd35263008aebb0ebccb272755793d10a1acR1-R87),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-8242ca1f206aeefb27fdc2d73686357bf7cb70572c1003c0586e2ba189a6e7f6R1-R56))
* Simplify generateConfig, initAndApply, initAndDestroy,
downloadAndInspect, and downloadModule functions to accept a Terraform
executor instead of a working directory and an exec path in `execute.go`
and `module.go`
([link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-41ec31c0ceb8bd50c0329b55a8d27aeba69648059faef4e785302155f4e97482L23-R26),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-41ec31c0ceb8bd50c0329b55a8d27aeba69648059faef4e785302155f4e97482L45-L49),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-41ec31c0ceb8bd50c0329b55a8d27aeba69648059faef4e785302155f4e97482L92-R86),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-41ec31c0ceb8bd50c0329b55a8d27aeba69648059faef4e785302155f4e97482L99-R92),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-41ec31c0ceb8bd50c0329b55a8d27aeba69648059faef4e785302155f4e97482L105-R98),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-41ec31c0ceb8bd50c0329b55a8d27aeba69648059faef4e785302155f4e97482L141-R135),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-41ec31c0ceb8bd50c0329b55a8d27aeba69648059faef4e785302155f4e97482L148-R141),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-41ec31c0ceb8bd50c0329b55a8d27aeba69648059faef4e785302155f4e97482L168-R161),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-41ec31c0ceb8bd50c0329b55a8d27aeba69648059faef4e785302155f4e97482L201-R205),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-41ec31c0ceb8bd50c0329b55a8d27aeba69648059faef4e785302155f4e97482L222-R218),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-41ec31c0ceb8bd50c0329b55a8d27aeba69648059faef4e785302155f4e97482L243-R225),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-41ec31c0ceb8bd50c0329b55a8d27aeba69648059faef4e785302155f4e97482L301-R283),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-41ec31c0ceb8bd50c0329b55a8d27aeba69648059faef4e785302155f4e97482L307-R289),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-41ec31c0ceb8bd50c0329b55a8d27aeba69648059faef4e785302155f4e97482L320-R302),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-41ec31c0ceb8bd50c0329b55a8d27aeba69648059faef4e785302155f4e97482L353-R339),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-41ec31c0ceb8bd50c0329b55a8d27aeba69648059faef4e785302155f4e97482L385-R371),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-4f4cb2009f77e8f5e252eee95f3ed880148fc285798ef4b7bbeb970525ad7b5aR25),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-4f4cb2009f77e8f5e252eee95f3ed880148fc285798ef4b7bbeb970525ad7b5aL101-R103))
* Update TestGeneratedConfig, Test_GetTerraformConfig,
Test_GetTerraformConfig_EmptyRecipeName, and
Test_GetTerraformConfig_InvalidDirectory functions to use the
NewTerraform function and pass the test directory instead of the working
directory in `execute_test.go`
([link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-3b5890b2cb244038dacda842a1e285235330c421ad27e8bc02d6f979ee8869caL20-R23),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-3b5890b2cb244038dacda842a1e285235330c421ad27e8bc02d6f979ee8869caL30-R30),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-3b5890b2cb244038dacda842a1e285235330c421ad27e8bc02d6f979ee8869caL78-R38),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-3b5890b2cb244038dacda842a1e285235330c421ad27e8bc02d6f979ee8869caL85-L105),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-3b5890b2cb244038dacda842a1e285235330c421ad27e8bc02d6f979ee8869caL115-R58),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-3b5890b2cb244038dacda842a1e285235330c421ad27e8bc02d6f979ee8869caL128-L129),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-3b5890b2cb244038dacda842a1e285235330c421ad27e8bc02d6f979ee8869caL142-R81),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-3b5890b2cb244038dacda842a1e285235330c421ad27e8bc02d6f979ee8869caL151-L152),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-3b5890b2cb244038dacda842a1e285235330c421ad27e8bc02d6f979ee8869caL161-R98),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-3b5890b2cb244038dacda842a1e285235330c421ad27e8bc02d6f979ee8869caL167-R104),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-3b5890b2cb244038dacda842a1e285235330c421ad27e8bc02d6f979ee8869caL179-R115))
* Remove Test_DownloadModule_EmptyWorkingDirPath_Error test case as it
is no longer relevant in `module_test.go`
([link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-35f143c273df8b35733ba5b5e44bc5bb5e4dbe8467aeabc3e861b5e19354791bL20-R22),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-35f143c273df8b35733ba5b5e44bc5bb5e4dbe8467aeabc3e861b5e19354791bL92-L101))
* Rename test case for empty recipe name error in `execute_test.go`
([link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-3b5890b2cb244038dacda842a1e285235330c421ad27e8bc02d6f979ee8869caL78-R38))
* Change test directory name and error message in
Test_GetTerraformConfig_InvalidDirectory function in `execute_test.go`
([link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-3b5890b2cb244038dacda842a1e285235330c421ad27e8bc02d6f979ee8869caL167-R104),
[link](https://github.com/radius-project/radius/pull/6630/files?diff=unified&w=0#diff-3b5890b2cb244038dacda842a1e285235330c421ad27e8bc02d6f979ee8869caL179-R115))

---------

Signed-off-by: Karishma Chawla <kachawla@microsoft.com>
Co-authored-by: karishma-chawla <74574173+karishma-chawla@users.noreply.github.com>
  • Loading branch information
kachawla and kachawla authored Nov 3, 2023
1 parent c6a9049 commit d680f7d
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 181 deletions.
18 changes: 9 additions & 9 deletions pkg/cli/cmd/recipe/show/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/radius-project/radius/pkg/cli/output"
"github.com/radius-project/radius/pkg/cli/workspaces"
"github.com/radius-project/radius/pkg/corerp/api/v20231001preview"
datastorerp "github.com/radius-project/radius/pkg/datastoresrp/frontend/controller"
datastoresrp "github.com/radius-project/radius/pkg/datastoresrp/frontend/controller"
"github.com/radius-project/radius/pkg/recipes"
"github.com/radius-project/radius/pkg/to"
"github.com/radius-project/radius/test/radcli"
Expand All @@ -46,7 +46,7 @@ func Test_Validate(t *testing.T) {
testcases := []radcli.ValidateInput{
{
Name: "Valid Show Command",
Input: []string{"recipeName", "--resource-type", datastorerp.RedisCachesResourceType},
Input: []string{"recipeName", "--resource-type", datastoresrp.RedisCachesResourceType},
ExpectedValid: true,
ConfigHolder: framework.ConfigHolder{
ConfigFilePath: "",
Expand All @@ -55,7 +55,7 @@ func Test_Validate(t *testing.T) {
},
{
Name: "Show Command with incorrect fallback workspace",
Input: []string{"-e", "my-env", "-g", "my-env", "recipeName", "--resource-type", datastorerp.RedisCachesResourceType},
Input: []string{"-e", "my-env", "-g", "my-env", "recipeName", "--resource-type", datastoresrp.RedisCachesResourceType},
ExpectedValid: false,
ConfigHolder: framework.ConfigHolder{
ConfigFilePath: "",
Expand All @@ -64,7 +64,7 @@ func Test_Validate(t *testing.T) {
},
{
Name: "Show Command with too many positional args",
Input: []string{"recipeName", "arg2", "--resource-type", datastorerp.RedisCachesResourceType},
Input: []string{"recipeName", "arg2", "--resource-type", datastoresrp.RedisCachesResourceType},
ExpectedValid: false,
ConfigHolder: framework.ConfigHolder{
ConfigFilePath: "",
Expand All @@ -73,7 +73,7 @@ func Test_Validate(t *testing.T) {
},
{
Name: "Show Command with fallback workspace",
Input: []string{"-e", "my-env", "-w", "test-workspace", "recipeName", "--resource-type", datastorerp.RedisCachesResourceType},
Input: []string{"-e", "my-env", "-w", "test-workspace", "recipeName", "--resource-type", datastoresrp.RedisCachesResourceType},
ExpectedValid: true,
ConfigHolder: framework.ConfigHolder{
ConfigFilePath: "",
Expand Down Expand Up @@ -111,7 +111,7 @@ func Test_Run(t *testing.T) {
}
recipe := types.EnvironmentRecipe{
Name: "cosmosDB",
ResourceType: datastorerp.MongoDatabasesResourceType,
ResourceType: datastoresrp.MongoDatabasesResourceType,
TemplateKind: recipes.TemplateKindBicep,
TemplatePath: "ghcr.io/testpublicrecipe/bicep/modules/mongodatabases:v1",
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func Test_Run(t *testing.T) {
Workspace: &workspaces.Workspace{},
Format: "table",
RecipeName: "cosmosDB",
ResourceType: datastorerp.MongoDatabasesResourceType,
ResourceType: datastoresrp.MongoDatabasesResourceType,
}

err := runner.Run(context.Background())
Expand Down Expand Up @@ -187,7 +187,7 @@ func Test_Run(t *testing.T) {
}
recipe := types.EnvironmentRecipe{
Name: "cosmosDB",
ResourceType: datastorerp.MongoDatabasesResourceType,
ResourceType: datastoresrp.MongoDatabasesResourceType,
TemplateKind: recipes.TemplateKindTerraform,
TemplatePath: "Azure/cosmosdb/azurerm",
TemplateVersion: "1.1.0",
Expand Down Expand Up @@ -222,7 +222,7 @@ func Test_Run(t *testing.T) {
Workspace: &workspaces.Workspace{},
Format: "table",
RecipeName: "cosmosDB",
ResourceType: datastorerp.MongoDatabasesResourceType,
ResourceType: datastoresrp.MongoDatabasesResourceType,
}

err := runner.Run(context.Background())
Expand Down
74 changes: 24 additions & 50 deletions pkg/recipes/terraform/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"time"

install "github.com/hashicorp/hc-install"
"github.com/hashicorp/terraform-exec/tfexec"
tfjson "github.com/hashicorp/terraform-json"
"github.com/radius-project/radius/pkg/metrics"
"github.com/radius-project/radius/pkg/recipes"
Expand All @@ -42,11 +40,6 @@ import (
"k8s.io/client-go/kubernetes"
)

const (
executionSubDir = "deploy"
workingDirFileMode fs.FileMode = 0700
)

var (
// ErrRecipeNameEmpty is the error when the recipe name is empty.
ErrRecipeNameEmpty = errors.New("recipe name cannot be empty")
Expand Down Expand Up @@ -89,20 +82,20 @@ func (e *executor) Deploy(ctx context.Context, options Options) (*tfjson.State,
return nil, err
}

// Create Working Directory
workingDir, err := createWorkingDir(ctx, options.RootDir)
// Create a new instance of tfexec.Terraform with current Terraform installation path
tf, err := NewTerraform(ctx, options.RootDir, execPath)
if err != nil {
return nil, err
}

// Create Terraform config in the working directory
kubernetesBackendSuffix, err := e.generateConfig(ctx, workingDir, execPath, options)
kubernetesBackendSuffix, err := e.generateConfig(ctx, tf, options)
if err != nil {
return nil, err
}

// Run TF Init and Apply in the working directory
state, err := initAndApply(ctx, workingDir, execPath)
state, err := initAndApply(ctx, tf)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -138,14 +131,14 @@ func (e *executor) Delete(ctx context.Context, options Options) error {
return err
}

// Create Working Directory
workingDir, err := createWorkingDir(ctx, options.RootDir)
// Create a new instance of tfexec.Terraform with current Terraform installation path
tf, err := NewTerraform(ctx, options.RootDir, execPath)
if err != nil {
return err
}

// Create Terraform config in the working directory
kubernetesBackendSuffix, err := e.generateConfig(ctx, workingDir, execPath, options)
kubernetesBackendSuffix, err := e.generateConfig(ctx, tf, options)
if err != nil {
return err
}
Expand All @@ -165,7 +158,7 @@ func (e *executor) Delete(ctx context.Context, options Options) error {
}

// Run TF Destroy in the working directory to delete the resources deployed by the recipe
err = initAndDestroy(ctx, workingDir, execPath)
err = initAndDestroy(ctx, tf)
if err != nil {
return err
}
Expand Down Expand Up @@ -198,18 +191,18 @@ func (e *executor) GetRecipeMetadata(ctx context.Context, options Options) (map[
return nil, err
}

// Create Working Directory
workingDir, err := createWorkingDir(ctx, options.RootDir)
// Create a new instance of tfexec.Terraform with current Terraform installation path
tf, err := NewTerraform(ctx, options.RootDir, execPath)
if err != nil {
return nil, err
}

_, err = getTerraformConfig(ctx, workingDir, options)
_, err = getTerraformConfig(ctx, tf.WorkingDir(), options)
if err != nil {
return nil, err
}

result, err := downloadAndInspect(ctx, workingDir, execPath, options)
result, err := downloadAndInspect(ctx, tf, options)
if err != nil {
return nil, err
}
Expand All @@ -219,28 +212,17 @@ func (e *executor) GetRecipeMetadata(ctx context.Context, options Options) (map[
}, nil
}

func createWorkingDir(ctx context.Context, tfDir string) (string, error) {
logger := ucplog.FromContextOrDiscard(ctx)

workingDir := filepath.Join(tfDir, executionSubDir)
logger.Info(fmt.Sprintf("Creating Terraform working directory: %q", workingDir))
if err := os.MkdirAll(workingDir, workingDirFileMode); err != nil {
return "", fmt.Errorf("failed to create working directory for terraform execution: %w", err)
}

return workingDir, nil
}

// generateConfig generates Terraform configuration with required inputs for the module, providers and backend to be initialized and applied.
func (e *executor) generateConfig(ctx context.Context, workingDir, execPath string, options Options) (string, error) {
func (e *executor) generateConfig(ctx context.Context, tf *tfexec.Terraform, options Options) (string, error) {
logger := ucplog.FromContextOrDiscard(ctx)
workingDir := tf.WorkingDir()

tfConfig, err := getTerraformConfig(ctx, workingDir, options)
if err != nil {
return "", err
}

loadedModule, err := downloadAndInspect(ctx, workingDir, execPath, options)
loadedModule, err := downloadAndInspect(ctx, tf, options)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -298,13 +280,13 @@ func (e *executor) generateConfig(ctx context.Context, workingDir, execPath stri
}

// downloadAndInspect handles downloading the TF module and retrieving the necessary information
func downloadAndInspect(ctx context.Context, workingDir string, execPath string, options Options) (*moduleInspectResult, error) {
func downloadAndInspect(ctx context.Context, tf *tfexec.Terraform, options Options) (*moduleInspectResult, error) {
logger := ucplog.FromContextOrDiscard(ctx)

// Download the Terraform module to the working directory.
logger.Info(fmt.Sprintf("Downloading Terraform module: %s", options.EnvRecipe.TemplatePath))
downloadStartTime := time.Now()
if err := downloadModule(ctx, workingDir, execPath, options.EnvRecipe.TemplatePath); err != nil {
if err := downloadModule(ctx, tf, options.EnvRecipe.TemplatePath); err != nil {
metrics.DefaultRecipeEngineMetrics.RecordRecipeDownloadDuration(ctx, downloadStartTime,
metrics.NewRecipeAttributes(metrics.RecipeEngineOperationDownloadRecipe, options.EnvRecipe.Name,
options.EnvRecipe, recipes.RecipeDownloadFailed))
Expand All @@ -317,7 +299,7 @@ func downloadAndInspect(ctx context.Context, workingDir string, execPath string,
// Load the downloaded module to retrieve providers and variables required by the module.
// This is needed to add the appropriate providers config and populate the value of recipe context variable.
logger.Info(fmt.Sprintf("Inspecting the downloaded Terraform module: %s", options.EnvRecipe.TemplatePath))
loadedModule, err := inspectModule(workingDir, options.EnvRecipe.Name)
loadedModule, err := inspectModule(tf.WorkingDir(), options.EnvRecipe.Name)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -350,16 +332,11 @@ func getTerraformConfig(ctx context.Context, workingDir string, options Options)
}

// initAndApply runs Terraform init and apply in the provided working directory.
func initAndApply(ctx context.Context, workingDir, execPath string) (*tfjson.State, error) {
func initAndApply(ctx context.Context, tf *tfexec.Terraform) (*tfjson.State, error) {
logger := ucplog.FromContextOrDiscard(ctx)

tf, err := NewTerraform(ctx, workingDir, execPath)
if err != nil {
return nil, err
}
// Initialize Terraform
logger.Info("Initializing Terraform")

terraformInitStartTime := time.Now()
if err := tf.Init(ctx); err != nil {
metrics.DefaultRecipeEngineMetrics.RecordTerraformInitializationDuration(ctx, terraformInitStartTime,
Expand All @@ -382,19 +359,16 @@ func initAndApply(ctx context.Context, workingDir, execPath string) (*tfjson.Sta
}

// initAndDestroy runs Terraform init and destroy in the provided working directory.
func initAndDestroy(ctx context.Context, workingDir, execPath string) error {
func initAndDestroy(ctx context.Context, tf *tfexec.Terraform) error {
logger := ucplog.FromContextOrDiscard(ctx)

tf, err := NewTerraform(ctx, workingDir, execPath)
if err != nil {
return err
}

// Initialize Terraform
logger.Info("Initializing Terraform")

terraformInitStartTime := time.Now()
if err := tf.Init(ctx); err != nil {
metrics.DefaultRecipeEngineMetrics.RecordTerraformInitializationDuration(ctx, terraformInitStartTime,
[]attribute.KeyValue{metrics.OperationStateAttrKey.String(metrics.FailedOperationState)})

return fmt.Errorf("terraform init failure: %w", err)
}
metrics.DefaultRecipeEngineMetrics.RecordTerraformInitializationDuration(ctx, terraformInitStartTime, nil)
Expand Down
Loading

0 comments on commit d680f7d

Please sign in to comment.