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: create --all-terragrunt creates Terragrunt stacks with cycles. #1777

Merged
merged 1 commit into from
Nov 29, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:
### Fixed

- Fix the command-line parsing of `run` and `script run` which were not failing from unknown flags.
- Fix `create --all-terragrunt` creating Terragrunt stacks with cycles.

## v0.11.3

Expand Down
19 changes: 18 additions & 1 deletion cmd/terramate/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -1467,12 +1467,29 @@ func (c *cli) initTerragrunt() {
if err != nil {
fatalWithDetailf(err, "creating stack UUID")
}
after := []string{}
for _, otherMod := range mod.After.Strings() {
// Parent stack modules must be excluded because of implicit filesystem ordering.
// Parent stacks are always executed before child stacks.
if mod.Path.HasPrefix(otherMod + "/") {
continue
}
// after stacks must not be defined as child stacks
// because it contradicts the Terramate implicit filesystem ordering.
if strings.HasPrefix(otherMod, mod.Path.String()+"/") {
fatalWithDetailf(
errors.E("Module %q is defined as a child of the module stack it depends on, which contradicts the Terramate implicit filesystem ordering.", otherMod),
"You may consider moving stack %s elsewhere not conflicting with filesystem ordering.", otherMod,
)
}
after = append(after, otherMod)
}
stackSpec := config.Stack{
Dir: mod.Path,
ID: stackID.String(),
Name: dirBasename,
Description: dirBasename,
After: mod.After.Strings(),
After: after,
}

err = stack.Create(c.cfg(), stackSpec)
Expand Down
94 changes: 87 additions & 7 deletions e2etests/core/create_all_terragrunt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package core_test

import (
"path/filepath"
"regexp"
"testing"

. "github.com/terramate-io/terramate/e2etests/internal/runner"
Expand Down Expand Up @@ -210,27 +211,27 @@ func TestCreateAllTerragrunt(t *testing.T) {
wantOrder: []string{"stack"},
},
{
name: "terragrunt module at root with dependencies at /modules",
name: "terragrunt module at /A with dependencies at /modules",
layout: []string{
hclfile("terragrunt.hcl", Doc(
hclfile("A/terragrunt.hcl", Doc(
Block("terraform",
Str("source", "github.com/some/repo"),
),
Block("dependencies",
Expr("paths", `["modules/mod1"]`),
Expr("paths", `["../modules/B"]`),
),
)),
hclfile("modules/mod1/terragrunt.hcl", Block("terraform",
hclfile("modules/B/terragrunt.hcl", Block("terraform",
Str("source", "github.com/some/repo"),
)),
},
want: RunExpected{
Stdout: nljoin(
"Created stack /",
"Created stack /modules/mod1",
"Created stack /A",
"Created stack /modules/B",
),
},
wantOrder: []string{"modules/mod1", "."},
wantOrder: []string{"modules/B", "A"},
},
{
name: "multiple orderings using dependencies block",
Expand Down Expand Up @@ -334,6 +335,85 @@ func TestCreateAllTerragrunt(t *testing.T) {
},
wantOrder: []string{"3", "2", "1"},
},
{
name: "nested terragrunt modules",
layout: []string{
hclfile("1/terragrunt.hcl", Doc(
Block("terraform",
Str("source", "github.com/some/repo"),
),
)),
hclfile("1/2/terragrunt.hcl", Doc(
Block("terraform",
Str("source", "github.com/some/repo"),
),
Block("dependency",
Labels("module3"),
Str("config_path", `..`),
),
),
),
},
want: RunExpected{
Stdout: nljoin(
"Created stack /1",
"Created stack /1/2",
),
},
wantOrder: []string{"1", "1/2"},
},
{
name: "nested terragrunt modules conflicting with Terramate filesystem ordering",
layout: []string{
hclfile("1/terragrunt.hcl", Doc(
Block("terraform",
Str("source", "github.com/some/repo"),
),
Block("dependency",
Labels("module3"),
Str("config_path", `./2`),
),
),
),
hclfile("1/2/terragrunt.hcl", Doc(
Block("terraform",
Str("source", "github.com/some/repo"),
),
)),
},
want: RunExpected{
Status: 1,
StderrRegex: regexp.QuoteMeta(`Module "/1/2" is defined as a child of the module stack it depends on`),
},
wantOrder: []string{"1", "1/2"},
},
{
name: "nested terragrunt modules with sharing same prefix path",
layout: []string{
hclfile("A/foo/terragrunt.hcl", Doc(
Block("terraform",
Str("source", "github.com/some/repo"),
),
Block("dependency",
Labels("module3"),
Str("config_path", `../foobar`),
),
),
),
hclfile("A/foobar/terragrunt.hcl", Doc(
Block("terraform",
Str("source", "github.com/some/repo"),
),
)),
},
want: RunExpected{
Stdout: nljoin(
"Created stack /A/foo",
"Created stack /A/foobar",
),
},
wantOrder: []string{"A/foobar", "A/foo"},
},
{
name: "Terragrunt definitions with ciclic ordering",
layout: []string{
Expand Down
Loading