Skip to content

Commit

Permalink
Upgrade to support Terragrunt 0.32.2 (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmattia authored Sep 17, 2021
1 parent 03b7a34 commit b0f809d
Show file tree
Hide file tree
Showing 15 changed files with 430 additions and 658 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=1.8.1
VERSION=1.9.0
PATH_BUILD=build/
FILE_COMMAND=terragrunt-atlantis-config
FILE_ARCH=darwin_amd64
Expand Down
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
This tool creates Atlantis YAML configurations for Terragrunt projects by:

- Finding all `terragrunt.hcl` in a repo
- Evaluating their "dependency" and "terraform" source blocks to find their dependencies
- Evaluating their `dependency`, `terraform`, `locals`, and other source blocks to find their dependencies
- Creating a Directed Acyclic Graph of all dependencies
- Constructing and logging YAML in Atlantis' config spec that reflects the graph

Expand Down Expand Up @@ -48,7 +48,7 @@ Then, make sure `terragrunt-atlantis-config` is present on your Atlantis server.

```hcl
variable "terragrunt_atlantis_config_version" {
default = "1.8.1"
default = "1.9.0"
}
build {
Expand All @@ -70,7 +70,7 @@ and just like that, your developers should never have to worry about an `atlanti

## Extra dependencies

For 99% of cases, this tool can sniff out all dependencies in a module. However, you may have times when you want to add in additional dependencies such as:
For basic cases, this tool can sniff out all dependencies in a module. However, you may have times when you want to add in additional dependencies such as:

- You use Terragrunt's `read_terragrunt_config` function in your locals, and want to depend on the read file
- Your Terragrunt module should be run anytime some non-terragrunt file is updated, such as a Dockerfile or Packer template
Expand Down Expand Up @@ -162,14 +162,22 @@ Enabling this feature may consume more resources like cpu, memory, network, and
As when defining the workspace this info is also needed when running `atlantis plan/apply -d ${git_root}/stage/app -w stage_app` to run the command on specific directory,
you can also use the `atlantis plan/apply -p stage_app` in case you have enabled the `create-project-name` cli argument (it is `false` by default).

## Rules for merging config

Each terragrunt module can have locals, but can also have zero to many `include` blocks that can specify parent terragrunt files that can also have locals.

In most cases (for string/boolean locals), the primary terragrunt module has the highest precedence, followed by the locals in the lowest appearing `include` block, etc. all the way until the lowest precedence at the locals in the first `include` block to appear.

However, there is one exception where the values are merged, which is the `atlantis_extra_dependencies` local. For this local, all values are appended to one another. This way, you can have `include` files declare their own dependencies.

## Local Installation and Usage

You can install this tool locally to checkout what kinds of config it will generate for your repo, though in production it is recommended to [install this tool directly onto your Atlantis server](##integrate-into-your-atlantis-server)

Recommended: Install any version via go get:

```bash
cd && GO111MODULE=on go get github.com/transcend-io/terragrunt-atlantis-config@v1.8.1 && cd -
cd && GO111MODULE=on go get github.com/transcend-io/terragrunt-atlantis-config@v1.9.0 && cd -
```

This module officially supports golang versions v1.13, v1.14, v1.15, and v1.16, tested on CircleCI with each build
Expand All @@ -192,7 +200,7 @@ Finally, check the log output (or your output file) for the YAML.

## Contributing

To test any changes you've made, run `make test`.
To test any changes you've made, run `make gotestsum` (or `make test` for standard golang testing).

Once all your changes are passing and your PR is reviewed, a merge into `master` will trigger a Github Actions job to build the new binary, test it, and deploy it's artifacts to Github Releases along with checksums.

Expand Down
8 changes: 8 additions & 0 deletions cmd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,11 @@ func TestFilterGlobFlagWithInfraLiveMySql(t *testing.T) {
filepath.Join("..", "test_examples", "terragrunt-infrastructure-live-example", "*", "*", "*", "mysql"),
})
}

func TestMultipleIncludes(t *testing.T) {
runTest(t, filepath.Join("golden", "multiple_includes.yaml"), []string{
"--root",
filepath.Join("..", "test_examples", "multiple_includes"),
"--terraform-version", "0.14.9001",
})
}
33 changes: 33 additions & 0 deletions cmd/golden/multiple_includes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
automerge: false
parallel_apply: true
parallel_plan: true
projects:
- autoplan:
enabled: false
when_modified:
- '*.hcl'
- '*.tf*'
dir: includes_tf_12_then_13
terraform_version: 0.13.9001
- autoplan:
enabled: false
when_modified:
- '*.hcl'
- '*.tf*'
dir: includes_tf_13_then_12
terraform_version: 0.12.9001
- autoplan:
enabled: false
when_modified:
- '*.hcl'
- '*.tf*'
dir: uses_terraform_12
terraform_version: 0.12.9001
- autoplan:
enabled: false
when_modified:
- '*.hcl'
- '*.tf*'
dir: uses_terraform_13
terraform_version: 0.13.9001
version: 3
5 changes: 4 additions & 1 deletion cmd/parse_hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func isParentModule(path string, terragruntOptions *options.TerragruntOptions) (
}

extensions := config.EvalContextExtensions{}
evalContext := config.CreateTerragruntEvalContext(path, terragruntOptions, extensions)
evalContext, err := config.CreateTerragruntEvalContext(path, terragruntOptions, extensions)
if err != nil {
return false, err
}

// Mock all the functions out so they don't do anything. Otherwise they may throw errors that we don't care about
evalContext.Functions = map[string]function.Function{}
Expand Down
65 changes: 36 additions & 29 deletions cmd/parse_locals.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,33 @@ func parseHcl(parser *hclparse.Parser, hcl string, filename string) (file *hcl.F
return file, nil
}

// Merges in values from a child into a parent set of `local` values
func mergeResolvedLocals(parent ResolvedLocals, child ResolvedLocals) ResolvedLocals {
if child.AtlantisWorkflow != "" {
parent.AtlantisWorkflow = child.AtlantisWorkflow
}

if child.TerraformVersion != "" {
parent.TerraformVersion = child.TerraformVersion
}

if child.AutoPlan != nil {
parent.AutoPlan = child.AutoPlan
}

if child.Skip != nil {
parent.Skip = child.Skip
}

if child.ApplyRequirements != nil || len(child.ApplyRequirements) > 0 {
parent.ApplyRequirements = child.ApplyRequirements
}

parent.ExtraAtlantisDependencies = append(parent.ExtraAtlantisDependencies, child.ExtraAtlantisDependencies...)

return parent
}

// Parses a given file, returning a map of all it's `local` values
func parseLocals(path string, terragruntOptions *options.TerragruntOptions, includeFromChild *config.IncludeConfig) (ResolvedLocals, error) {
configString, err := util.ReadFileAsString(path)
Expand All @@ -70,43 +97,23 @@ func parseLocals(path string, terragruntOptions *options.TerragruntOptions, incl
}

// Decode just the Base blocks. See the function docs for DecodeBaseBlocks for more info on what base blocks are.
localsAsCty, _, includeConfig, err := config.DecodeBaseBlocks(terragruntOptions, parser, file, path, includeFromChild)
decodeSectionTypes := []config.PartialDecodeSectionType{}
localsAsCty, trackInclude, err := config.DecodeBaseBlocks(terragruntOptions, parser, file, path, includeFromChild, decodeSectionTypes)
if err != nil {
return ResolvedLocals{}, err
}

// Recurse on the parent to merge in the locals from that file
parentLocals := ResolvedLocals{}
if includeConfig != nil && includeFromChild == nil {
// Ignore errors if the parent cannot be parsed. Terragrunt Errors still will be logged
parentLocals, _ = parseLocals(includeConfig.Path, terragruntOptions, includeConfig)
mergedParentLocals := ResolvedLocals{}
if trackInclude != nil && includeFromChild == nil {
for _, includeConfig := range trackInclude.CurrentList {
parentLocals, _ := parseLocals(includeConfig.Path, terragruntOptions, &includeConfig)
mergedParentLocals = mergeResolvedLocals(mergedParentLocals, parentLocals)
}
}
childLocals := resolveLocals(*localsAsCty)

// Merge in values from child => parent local values
if childLocals.AtlantisWorkflow != "" {
parentLocals.AtlantisWorkflow = childLocals.AtlantisWorkflow
}

if childLocals.TerraformVersion != "" {
parentLocals.TerraformVersion = childLocals.TerraformVersion
}

if childLocals.AutoPlan != nil {
parentLocals.AutoPlan = childLocals.AutoPlan
}

if childLocals.Skip != nil {
parentLocals.Skip = childLocals.Skip
}

if childLocals.ApplyRequirements != nil || len(childLocals.ApplyRequirements) > 0 {
parentLocals.ApplyRequirements = childLocals.ApplyRequirements
}

parentLocals.ExtraAtlantisDependencies = append(parentLocals.ExtraAtlantisDependencies, childLocals.ExtraAtlantisDependencies...)

return parentLocals, nil
return mergeResolvedLocals(mergedParentLocals, childLocals), nil
}

func resolveLocals(localsAsCty cty.Value) ResolvedLocals {
Expand Down
13 changes: 5 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,17 @@ require (
github.com/bmatcuk/doublestar v1.3.1 // indirect
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/go-errors/errors v1.1.1 // indirect
github.com/gruntwork-io/terragrunt v0.28.24
github.com/hashicorp/hcl/v2 v2.9.1
github.com/hashicorp/terraform v0.12.28 // indirect
github.com/gruntwork-io/terragrunt v0.32.2
github.com/hashicorp/hcl/v2 v2.10.0
github.com/hashicorp/terraform-config-inspect v0.0.0-20210625153042-09f34846faab
github.com/hashicorp/terraform-svchost v0.0.0-20191119180714-d2e4933b9136 // indirect
github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/mattn/go-zglob v0.0.2 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/sirupsen/logrus v1.6.0
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.7.0
github.com/urfave/cli v1.22.4 // indirect
github.com/zclconf/go-cty v1.8.1
github.com/zclconf/go-cty-yaml v1.0.2 // indirect
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
github.com/zclconf/go-cty v1.8.3
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
gopkg.in/ini.v1 v1.57.0 // indirect
)
Loading

0 comments on commit b0f809d

Please sign in to comment.