Skip to content

Commit

Permalink
fix: create --all-terragrunt creates Terragrunt stacks with cycles.
Browse files Browse the repository at this point in the history
Signed-off-by: i4k <[email protected]>
  • Loading branch information
i4ki committed Nov 28, 2024
1 parent 8ae3bd7 commit f04b09f
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 8 deletions.
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/foo", "A/foobar"},
},
{
name: "Terragrunt definitions with ciclic ordering",
layout: []string{
Expand Down

0 comments on commit f04b09f

Please sign in to comment.