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

fix: move from semaphore to rwmutex #215

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/pkg/errors v0.9.1
github.com/spf13/afero v1.9.5
go.uber.org/zap v1.25.0
golang.org/x/sync v0.3.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
k8s.io/api v0.28.3
k8s.io/apiextensions-apiserver v0.28.3
Expand Down Expand Up @@ -87,6 +86,7 @@ require (
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
Expand Down
42 changes: 28 additions & 14 deletions internal/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ import (
"strconv"
"strings"

"sync"

"github.com/pkg/errors"
"golang.org/x/sync/semaphore"
)

// Error strings.
const (
errParse = "cannot parse Terraform output"
errWriteVarFile = "cannot write tfvars file"
errSemAcquire = "cannot acquire semaphore for tfinit"

errParse = "cannot parse Terraform output"
errWriteVarFile = "cannot write tfvars file"
errFmtInvalidConfig = "invalid Terraform configuration: found %d errors"

tfDefault = "default"
Expand Down Expand Up @@ -149,9 +148,12 @@ func WithInitArgs(v []string) InitOption {
}
}

// Semaphore to limit the number of concurrent terraform init commands to 1.
// This is needed to support a shared provider cache with concurrent reconciliations.
var sem = semaphore.NewWeighted(int64(1))
// RWMutex protects the terraform shared cache from corruption. If an init is
// performed, it requires a write lock. Only one write lock at a time. If another
// action is performed, a read lock is acquired. More than one read locks can be acquired.
// This prevents an init from inadvertently changing a plugin while it's in use.
// Prevents issues with `text file busy` errors.
var rwmutex = &sync.RWMutex{}

// Init initializes a Terraform configuration.
func (h Harness) Init(ctx context.Context, cache bool, o ...InitOption) error {
Expand All @@ -172,12 +174,9 @@ func (h Harness) Init(ctx context.Context, cache bool, o ...InitOption) error {
cmd.Env = append(cmd.Env, e)
}
cmd.Env = append(cmd.Env, "TF_CLI_CONFIG_FILE=./.terraformrc")
err := sem.Acquire(ctx, 1)
if err != nil {
return errors.Wrap(err, errSemAcquire)
}
defer sem.Release(1)
_, err = cmd.Output()
rwmutex.Lock()
defer rwmutex.Unlock()
_, err := cmd.Output()
return Classify(err)
}

Expand Down Expand Up @@ -231,6 +230,8 @@ func (h Harness) Workspace(ctx context.Context, name string) error {
// is somewhat optimistic, but it shouldn't hurt to try.
cmd = exec.CommandContext(ctx, h.Path, "workspace", "new", "-no-color", name) //nolint:gosec
cmd.Dir = h.Dir
rwmutex.RLock()
defer rwmutex.RUnlock()
_, err := cmd.Output()
return Classify(err)
}
Expand All @@ -257,6 +258,8 @@ func (h Harness) DeleteCurrentWorkspace(ctx context.Context) error {
cmd = exec.CommandContext(ctx, h.Path, "workspace", "delete", "-no-color", name) //nolint:gosec
cmd.Dir = h.Dir

rwmutex.RLock()
defer rwmutex.RUnlock()
_, err = cmd.Output()
if err == nil {
// We successfully deleted the workspace; we're done.
Expand Down Expand Up @@ -367,6 +370,8 @@ func (h Harness) Outputs(ctx context.Context) ([]Output, error) {

outputs := map[string]output{}

rwmutex.RLock()
defer rwmutex.RUnlock()
out, err := cmd.Output()
if jerr := json.Unmarshal(out, &outputs); jerr != nil {
// If stdout doesn't appear to be the JSON we expected we try to extract
Expand Down Expand Up @@ -411,6 +416,8 @@ func (h Harness) Resources(ctx context.Context) ([]string, error) {
cmd := exec.CommandContext(ctx, h.Path, "state", "list") //nolint:gosec
cmd.Dir = h.Dir

rwmutex.RLock()
defer rwmutex.RUnlock()
out, err := cmd.Output()
if err != nil {
return nil, Classify(err)
Expand Down Expand Up @@ -489,6 +496,9 @@ func (h Harness) Diff(ctx context.Context, o ...Option) (bool, error) {
cmd := exec.CommandContext(ctx, h.Path, args...) //nolint:gosec
cmd.Dir = h.Dir

rwmutex.RLock()
defer rwmutex.RUnlock()

// The -detailed-exitcode flag will make terraform plan return:
// 0 - Succeeded, diff is empty (no changes)
// 1 - Errored
Expand Down Expand Up @@ -517,6 +527,8 @@ func (h Harness) Apply(ctx context.Context, o ...Option) error {
cmd := exec.CommandContext(ctx, h.Path, args...) //nolint:gosec
cmd.Dir = h.Dir

rwmutex.RLock()
defer rwmutex.RUnlock()
_, err := cmd.Output()
return Classify(err)
}
Expand All @@ -538,6 +550,8 @@ func (h Harness) Destroy(ctx context.Context, o ...Option) error {
cmd := exec.CommandContext(ctx, h.Path, args...) //nolint:gosec
cmd.Dir = h.Dir

rwmutex.RLock()
defer rwmutex.RUnlock()
_, err := cmd.Output()
return Classify(err)
}
Loading