Skip to content

Commit

Permalink
fix tf runtime concurrent execution (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
markliby committed Feb 8, 2023
1 parent 8192726 commit 7e20ed2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 12 additions & 1 deletion pkg/engine/runtime/terraform/terraform_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"
"path/filepath"
"sync"

"github.com/imdario/mergo"
"github.com/spf13/afero"
Expand All @@ -17,12 +18,16 @@ var _ runtime.Runtime = &TerraformRuntime{}

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

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

Expand All @@ -46,6 +51,7 @@ func (t *TerraformRuntime) Apply(ctx context.Context, request *runtime.ApplyRequ
}, Status: nil}
}

t.mu.Lock()
stackPath := request.Stack.GetPath()
tfCacheDir := filepath.Join(stackPath, "."+planState.ResourceKey())
t.WorkSpace.SetStackDir(stackPath)
Expand All @@ -71,6 +77,7 @@ func (t *TerraformRuntime) Apply(ctx context.Context, request *runtime.ApplyRequ
if err != nil {
return &runtime.ApplyResponse{Resource: nil, Status: status.NewErrorStatus(err)}
}
t.mu.Unlock()

// get terraform provider version
providerAddr, err := t.WorkSpace.GetProvider()
Expand Down Expand Up @@ -108,6 +115,7 @@ func (t *TerraformRuntime) Read(ctx context.Context, request *runtime.ReadReques
}
var tfstate *tfops.TFState

t.mu.Lock()
stackPath := request.Stack.GetPath()
tfCacheDir := filepath.Join(stackPath, "."+requestResource.ResourceKey())
t.WorkSpace.SetStackDir(stackPath)
Expand Down Expand Up @@ -136,6 +144,7 @@ func (t *TerraformRuntime) Read(ctx context.Context, request *runtime.ReadReques
if err != nil {
return &runtime.ReadResponse{Resource: nil, Status: status.NewErrorStatus(err)}
}
t.mu.Unlock()
if tfstate == nil || tfstate.Values == nil {
return &runtime.ReadResponse{Resource: nil, Status: nil}
}
Expand Down Expand Up @@ -164,12 +173,14 @@ func (t *TerraformRuntime) Delete(ctx context.Context, request *runtime.DeleteRe
stackPath := request.Stack.GetPath()
tfCacheDir := filepath.Join(stackPath, "."+request.Resource.ResourceKey())
defer os.RemoveAll(tfCacheDir)
t.mu.Lock()
t.WorkSpace.SetStackDir(stackPath)
t.WorkSpace.SetCacheDir(tfCacheDir)
t.WorkSpace.SetResource(request.Resource)
if err := t.WorkSpace.Destroy(ctx); err != nil {
return &runtime.DeleteResponse{Status: status.NewErrorStatus(err)}
}
t.mu.Unlock()

return &runtime.DeleteResponse{Status: nil}
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/engine/runtime/terraform/terraform_runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"
"path/filepath"
"sync"
"testing"

"github.com/spf13/afero"
Expand Down Expand Up @@ -34,7 +35,10 @@ func TestTerraformRuntime(t *testing.T) {
Path: filepath.Join(cwd, "fakePath"),
}
defer os.RemoveAll(stack.GetPath())
tfRuntime := TerraformRuntime{*tfops.NewWorkSpace(afero.Afero{Fs: afero.NewOsFs()})}
tfRuntime := TerraformRuntime{
WorkSpace: *tfops.NewWorkSpace(afero.Afero{Fs: afero.NewOsFs()}),
mu: &sync.Mutex{},
}

t.Run("ApplyDryRun", func(t *testing.T) {
response := tfRuntime.Apply(context.TODO(), &runtime.ApplyRequest{PlanResource: &testResource, DryRun: true, Stack: stack})
Expand Down

0 comments on commit 7e20ed2

Please sign in to comment.