Skip to content

Commit

Permalink
feat: enable concurrent manipulation of TF resources since they are s…
Browse files Browse the repository at this point in the history
…tored in separate directories
  • Loading branch information
SparkYuan committed Jun 28, 2024
1 parent b1d1e55 commit 1e31d93
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 13 deletions.
12 changes: 1 addition & 11 deletions pkg/engine/runtime/terraform/terraform_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/spf13/afero"
Expand All @@ -29,24 +28,19 @@ var tfEvents = cache.New(cache.NoExpiration, cache.NoExpiration)

type TerraformRuntime struct {
tfops.WorkSpace
mu *sync.Mutex
}

func NewTerraformRuntime(_ *apiv1.Resource) (runtime.Runtime, error) {
fs := afero.Afero{Fs: afero.NewOsFs()}
ws := tfops.NewWorkSpace(fs)
TFRuntime := &TerraformRuntime{
WorkSpace: *ws,
mu: &sync.Mutex{},
}
return TFRuntime, nil
}

// Apply Terraform resource
func (t *TerraformRuntime) Apply(ctx context.Context, request *runtime.ApplyRequest) *runtime.ApplyResponse {
t.mu.Lock()
defer t.mu.Unlock()

plan := request.PlanResource
stackPath := request.Stack.Path
key := plan.ResourceKey()
Expand Down Expand Up @@ -199,10 +193,8 @@ func (t *TerraformRuntime) Read(ctx context.Context, request *runtime.ReadReques
if priorResource == nil {
return &runtime.ReadResponse{Resource: nil, Status: nil}
}
var tfstate *tfops.StateRepresentation

t.mu.Lock()
defer t.mu.Unlock()
var tfstate *tfops.StateRepresentation
stackPath := request.Stack.Path
tfCacheDir := buildTFCacheDir(stackPath, planResource.ResourceKey())
t.WorkSpace.SetStackDir(stackPath)
Expand Down Expand Up @@ -266,8 +258,6 @@ func (t *TerraformRuntime) Import(ctx context.Context, request *runtime.ImportRe
func (t *TerraformRuntime) Delete(ctx context.Context, request *runtime.DeleteRequest) (res *runtime.DeleteResponse) {
stackPath := request.Stack.Path
tfCacheDir := buildTFCacheDir(stackPath, request.Resource.ResourceKey())
t.mu.Lock()
defer t.mu.Unlock()

t.WorkSpace.SetStackDir(stackPath)
t.WorkSpace.SetCacheDir(tfCacheDir)
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/runtime/terraform/tfops/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (w *WorkSpace) Apply(ctx context.Context) (*StateRepresentation, error) {
return nil, err
}

cmd := exec.CommandContext(ctx, "terraform", chdir, "apply", "-auto-approve", "-json", "-lock=false")
cmd := exec.CommandContext(ctx, "terraform", chdir, "apply", "-auto-approve", "-json")
cmd.Dir = w.stackDir
envs, err := w.initEnvs()
if err != nil {
Expand Down Expand Up @@ -301,7 +301,7 @@ func (w *WorkSpace) RefreshOnly(ctx context.Context) (*StateRepresentation, erro
if err != nil {
return nil, err
}
cmd := exec.CommandContext(ctx, "terraform", chdir, "apply", "-auto-approve", "-json", "--refresh-only", "-lock=false")
cmd := exec.CommandContext(ctx, "terraform", chdir, "apply", "-auto-approve", "-json", "--refresh-only")
cmd.Dir = w.stackDir

envs, err := w.initEnvs()
Expand Down

0 comments on commit 1e31d93

Please sign in to comment.