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

tfexec: Initial support for Terraform 0.14 -chdir global option #100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bflad
Copy link
Member

@bflad bflad commented Oct 19, 2020

Closes #99

@paultyng
Copy link
Contributor

paultyng commented Dec 7, 2020

Does chdir replace the work dir args that are in use in some commands already? We may need to look at this a bit more holistically, so we can utilize those options or something.

@bflad
Copy link
Member Author

bflad commented Feb 2, 2021

@paultyng I believe so, please see hashicorp/terraform#27664

@bflad
Copy link
Member Author

bflad commented Feb 2, 2021

Rebased 👍 We will probably want to go through and mark all the removed "args" with tf.compatible(ctx, nil, tf0_15_0) as well -- happy to do that here or leave as a followup item.

Base automatically changed from master to main February 25, 2021 18:08
Copy link
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

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

Sorry for the long delay in reviewing this @bflad - I finally found a spare moment to look at this PR of yours!

Do you have a use case (in SDK or elsewhere) for changing workdir during the lifetime of *Terraform instance?

I always worked with the assumption that the *Terraform is short-lived in a similar way to context.Context, which coincides with the fact that many commands didn't have an explicit argument (until recently) to run in a directory different to the one provided via NewTerraform(workdir, ...).

I think it is fair to support both workflows, but I think it will require a little bit more effort involved to make this safe.

We currently persist version output in the struct

versionLock sync.Mutex
execVersion *version.Version
provVersions map[string]*version.Version

to be able to run compatibility checks without having to re-execute version repeatedly before each command.

The core version is (naturally) scoped to the Terraform binary (execPath), but the version command also outputs versions of providers which are scoped to the workdir. I worry slightly that users won't realize this side effect while reading the cached output

func (tf *Terraform) Version(ctx context.Context, skipCache bool) (tfVersion *version.Version, providerVersions map[string]*version.Version, err error) {
tf.versionLock.Lock()
defer tf.versionLock.Unlock()
if tf.execVersion == nil || skipCache {
tf.execVersion, tf.provVersions, err = tf.version(ctx)
if err != nil {
return nil, nil, err
}
}

Obviously this problem already exists given that we support the dir args in various commands already, but this is much more radical change which brings this problem more into spotlight IMO.

One way we could address this is by scoping the cache by workdir - as a map, or more likely as sync.Map to make it safe for multiple commands to be executed at once. We should probably also consider some mechanism for purging the cache, like Cleanup(workdir string) - something that can be (optionally) called by downstream long-running process. This is to prevent any unnecessary memory consumption in case the consumer e.g. creates a high number of temporary directories in which it runs Terraform commands.

On a related note there is still a few commands which are missing the opts argument for passing chdir:

  • ProvidersSchema
  • Version
  • Validate

I think the skipCache argument should probably stay there as 1st class argument (as opposed to being hidden under opts ...VersionOption) to communicate the side effect of the builtin cache.

We will probably want to go through and mark all the removed "args" with tf.compatible(ctx, nil, tf0_15_0) as well -- happy to do that here

Yes please - if you can do that as part of this PR that would be great! 👍🏻

func (opt *ChdirOption) configureProvidersSchema(conf *providersSchemaConfig) {
conf.chdir = opt.path
}

// ProvidersSchema represents the terraform providers schema -json subcommand.
func (tf *Terraform) ProvidersSchema(ctx context.Context) (*tfjson.ProviderSchemas, error) {
Copy link
Member

Choose a reason for hiding this comment

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

I think in order to actually expose the chdir flag here we'd need to introduce opts ...ProvidersSchemaOption here as well, no?

@@ -10,20 +10,44 @@ import (
func TestProvidersSchemaCmd(t *testing.T) {
td := testTempDir(t)

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
tf, err := NewTerraform(td, tfVersion(t, testutil.Latest014))
Copy link
Member

Choose a reason for hiding this comment

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

I remember there were some format changes at some point - is it perhaps safer to test both 0.12 and >=0.14 which introduced the format change?

@@ -24,6 +45,37 @@ func (tf *Terraform) WorkspaceList(ctx context.Context) ([]string, string, error
return ws, current, nil
}

func (tf *Terraform) workspaceListCmd(ctx context.Context, opts ...WorkspaceListCmdOption) (*exec.Cmd, error) {
// TODO: [DIR] param option
Copy link
Member

Choose a reason for hiding this comment

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

I think this is no longer relevant?

Suggested change
// TODO: [DIR] param option

// WorkspaceList represents the workspace list subcommand to the Terraform CLI.
func (tf *Terraform) WorkspaceList(ctx context.Context) ([]string, string, error) {
func (tf *Terraform) WorkspaceList(ctx context.Context, opts ...WorkspaceListCmdOption) ([]string, string, error) {
// TODO: [DIR] param option
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// TODO: [DIR] param option

That doesn't seem relevant anymore

@radeksimko radeksimko added enhancement New feature or request waiting-response labels Aug 12, 2021
@kmoe kmoe added this to the v1.0.0 milestone Sep 17, 2021
@hashicorp-cla
Copy link

hashicorp-cla commented Mar 12, 2022

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request waiting-response
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Terraform 0.14.0 and later -chdir Global Option
5 participants