Skip to content

Commit 0095026

Browse files
authored
Configurable discovery files (#4491)
* Configurable discovery * Add test for mixed config * Mixed config parsing * Builder update * Mixed config test * Add tests for handling mix configuration * Lint fixes * Lint issues fixes
1 parent 7376129 commit 0095026

File tree

10 files changed

+93
-19
lines changed

10 files changed

+93
-19
lines changed

internal/discovery/discovery.go

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ type Discovery struct {
7575
// hiddenDirMemo is a memoization of hidden directories.
7676
hiddenDirMemo []string
7777

78+
// configFilenames is the list of config filenames to discover. If nil, defaults are used.
79+
configFilenames []string
80+
7881
// maxDependencyDepth is the maximum depth of the dependency tree to discover.
7982
maxDependencyDepth int
8083

@@ -103,6 +106,9 @@ type Discovery struct {
103106
// DiscoveryOption is a function that modifies a Discovery.
104107
type DiscoveryOption func(*Discovery)
105108

109+
// DefaultConfigFilenames are the default Terragrunt config filenames used in discovery.
110+
var DefaultConfigFilenames = []string{config.DefaultTerragruntConfigPath, config.DefaultStackFile}
111+
106112
// NewDiscovery creates a new Discovery.
107113
func NewDiscovery(dir string, opts ...DiscoveryOption) *Discovery {
108114
discovery := &Discovery{
@@ -190,6 +196,12 @@ func (d *Discovery) WithDiscoveryContext(discoveryContext *DiscoveryContext) *Di
190196
return d
191197
}
192198

199+
// WithConfigFilenames sets the configFilenames field to the given list.
200+
func (d *Discovery) WithConfigFilenames(filenames []string) *Discovery {
201+
d.configFilenames = filenames
202+
return d
203+
}
204+
193205
// String returns a string representation of a DiscoveredConfig.
194206
func (c *DiscoveredConfig) String() string {
195207
return c.Path
@@ -281,6 +293,12 @@ func (d *Discovery) isInHiddenDirectory(path string) bool {
281293
func (d *Discovery) Discover(ctx context.Context, l log.Logger, opts *options.TerragruntOptions) (DiscoveredConfigs, error) {
282294
var cfgs DiscoveredConfigs
283295

296+
// Set default config filenames if not set
297+
filenames := d.configFilenames
298+
if len(filenames) == 0 {
299+
filenames = DefaultConfigFilenames
300+
}
301+
284302
processFn := func(path string, info os.FileInfo, err error) error {
285303
if err != nil {
286304
return errors.New(err)
@@ -294,29 +312,26 @@ func (d *Discovery) Discover(ctx context.Context, l log.Logger, opts *options.Te
294312
return nil
295313
}
296314

297-
switch filepath.Base(path) {
298-
case config.DefaultTerragruntConfigPath:
299-
cfg := &DiscoveredConfig{
300-
Type: ConfigTypeUnit,
301-
Path: filepath.Dir(path),
302-
}
315+
base := filepath.Base(path)
316+
for _, fname := range filenames {
317+
if base == fname {
318+
cfgType := ConfigTypeUnit
319+
if fname == config.DefaultStackFile {
320+
cfgType = ConfigTypeStack
321+
}
303322

304-
if d.discoveryContext != nil {
305-
cfg.DiscoveryContext = d.discoveryContext
306-
}
323+
cfg := &DiscoveredConfig{
324+
Type: cfgType,
325+
Path: filepath.Dir(path),
326+
}
327+
if d.discoveryContext != nil {
328+
cfg.DiscoveryContext = d.discoveryContext
329+
}
307330

308-
cfgs = append(cfgs, cfg)
309-
case config.DefaultStackFile:
310-
cfg := &DiscoveredConfig{
311-
Type: ConfigTypeStack,
312-
Path: filepath.Dir(path),
313-
}
331+
cfgs = append(cfgs, cfg)
314332

315-
if d.discoveryContext != nil {
316-
cfg.DiscoveryContext = d.discoveryContext
333+
break
317334
}
318-
319-
cfgs = append(cfgs, cfg)
320335
}
321336

322337
return nil

internal/discovery/discovery_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,24 @@ exclude {
483483
assert.Nil(t, unit3.Parsed.Exclude)
484484
}
485485
}
486+
487+
func TestDiscoveryWithSingleCustomConfigFilename(t *testing.T) {
488+
t.Parallel()
489+
490+
tmpDir := t.TempDir()
491+
unit1Dir := filepath.Join(tmpDir, "unit1")
492+
err := os.MkdirAll(unit1Dir, 0755)
493+
require.NoError(t, err)
494+
err = os.WriteFile(filepath.Join(unit1Dir, "custom1.hcl"), []byte(""), 0644)
495+
require.NoError(t, err)
496+
497+
discoveryObj := discovery.NewDiscovery(tmpDir).WithConfigFilenames([]string{"custom1.hcl"})
498+
opts, err := options.NewTerragruntOptionsForTest(tmpDir)
499+
require.NoError(t, err)
500+
501+
configs, err := discoveryObj.Discover(t.Context(), logger.CreateLogger(), opts)
502+
require.NoError(t, err)
503+
504+
units := configs.Filter(discovery.ConfigTypeUnit).Paths()
505+
assert.ElementsMatch(t, []string{unit1Dir}, units)
506+
}

internal/runner/runnerpool/builder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package runnerpool
33
import (
44
"context"
55

6+
"github.com/gruntwork-io/terragrunt/config"
67
"github.com/gruntwork-io/terragrunt/internal/discovery"
78
"github.com/gruntwork-io/terragrunt/internal/runner/common"
89
"github.com/gruntwork-io/terragrunt/options"
@@ -19,6 +20,7 @@ func Build(ctx context.Context, l log.Logger, terragruntOptions *options.Terragr
1920
WithParseExclude().
2021
WithDiscoverDependencies().
2122
WithSuppressParseErrors().
23+
WithConfigFilenames([]string{config.DefaultTerragruntConfigPath}).
2224
WithDiscoveryContext(&discovery.DiscoveryContext{Cmd: terragruntOptions.TerraformCommand})
2325

2426
discovered, err := d.Discover(ctx, l, terragruntOptions)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

test/fixtures/mixed-config/app/terragrunt.hcl

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
unit "app1" {
3+
source = "../unit"
4+
path = "app1"
5+
}

test/fixtures/mixed-config/unit/main.tf

Whitespace-only changes.

test/fixtures/mixed-config/unit/terragrunt.hcl

Whitespace-only changes.

test/integration_runner_pool_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import (
1313
"github.com/stretchr/testify/require"
1414
)
1515

16+
const (
17+
testFixtureMixedConfig = "fixtures/mixed-config"
18+
)
19+
1620
func TestRunnerPoolDiscovery(t *testing.T) {
1721
t.Parallel()
1822

@@ -82,3 +86,16 @@ func TestRunnerPoolTerragruntDestroyOrder(t *testing.T) {
8286
// module-e must be destroyed before module-d
8387
assert.Less(t, index["module-e"], index["module-d"], "module-e should be destroyed before module-d")
8488
}
89+
90+
func TestRunnerPoolStackConfigIgnored(t *testing.T) {
91+
t.Parallel()
92+
93+
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureMixedConfig)
94+
helpers.CleanupTerraformFolder(t, tmpEnvPath)
95+
testPath := util.JoinPath(tmpEnvPath, testFixtureMixedConfig)
96+
97+
_, stderr, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt run --experiment runner-pool --queue-include-external --all --non-interactive --working-dir "+testPath+" -- apply")
98+
require.NoError(t, err)
99+
require.NotContains(t, stderr, "Error: Unsupported block type")
100+
require.NotContains(t, stderr, "Blocks of type \"unit\" are not expected here")
101+
}

test/integration_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4258,3 +4258,16 @@ func TestVersionIsInvokedInDifferentDirectory(t *testing.T) {
42584258
assert.Len(t, matches, 2, "Expected exactly one occurrence of '-version' command, found %d", len(matches))
42594259
assert.Contains(t, stderr, "prefix=dependency-with-custom-version msg=Running command: "+wrappedBinary()+" -version")
42604260
}
4261+
4262+
func TestMixedStackConfigIgnored(t *testing.T) {
4263+
t.Parallel()
4264+
4265+
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureMixedConfig)
4266+
helpers.CleanupTerraformFolder(t, tmpEnvPath)
4267+
testPath := util.JoinPath(tmpEnvPath, testFixtureMixedConfig)
4268+
4269+
_, stderr, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt run --all --non-interactive --working-dir "+testPath+" -- apply")
4270+
require.NoError(t, err)
4271+
require.NotContains(t, stderr, "Error: Unsupported block type")
4272+
require.NotContains(t, stderr, "Blocks of type \"unit\" are not expected here")
4273+
}

0 commit comments

Comments
 (0)