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

Added exclude_from_copy to config #3543

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 13 additions & 4 deletions cli/commands/terraform/download_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@ func downloadTerraformSource(ctx context.Context, source string, terragruntOptio

terragruntOptions.Logger.Debugf("Copying files from %s into %s", terragruntOptions.WorkingDir, terraformSource.WorkingDir)

var includeInCopy []string
var includeInCopy, excludeFromCopy []string

if terragruntConfig.Terraform != nil && terragruntConfig.Terraform.IncludeInCopy != nil {
includeInCopy = *terragruntConfig.Terraform.IncludeInCopy
}
if terragruntConfig.Terraform != nil && terragruntConfig.Terraform.ExcludeFromCopy != nil {
excludeFromCopy = *terragruntConfig.Terraform.ExcludeFromCopy
}

// Always include the .tflint.hcl file, if it exists
includeInCopy = append(includeInCopy, tfLintConfig)
if err := util.CopyFolderContents(terragruntOptions.Logger, terragruntOptions.WorkingDir, terraformSource.WorkingDir, ModuleManifestName, includeInCopy); err != nil {
if err := util.CopyFolderContents(terragruntOptions.Logger, terragruntOptions.WorkingDir, terraformSource.WorkingDir, ModuleManifestName, includeInCopy, excludeFromCopy); err != nil {
return nil, err
}

Expand Down Expand Up @@ -209,12 +214,16 @@ func updateGetters(terragruntOptions *options.TerragruntOptions, terragruntConfi

for getterName, getterValue := range getter.Getters {
if getterName == "file" {
var includeInCopy []string
var includeInCopy, excludeFromCopy []string

if terragruntConfig.Terraform != nil && terragruntConfig.Terraform.IncludeInCopy != nil {
includeInCopy = *terragruntConfig.Terraform.IncludeInCopy
}
if terragruntConfig.Terraform != nil && terragruntConfig.Terraform.ExcludeFromCopy != nil {
includeInCopy = *terragruntConfig.Terraform.ExcludeFromCopy
}

client.Getters[getterName] = &FileCopyGetter{IncludeInCopy: includeInCopy, Logger: terragruntOptions.Logger}
client.Getters[getterName] = &FileCopyGetter{IncludeInCopy: includeInCopy, Logger: terragruntOptions.Logger, ExcludeFromCopy: excludeFromCopy}
} else {
client.Getters[getterName] = getterValue
}
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/terraform/download_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,6 @@ func copyFolder(t *testing.T, src string, dest string) {
logger := log.New()
logger.SetOptions(log.WithOutput(io.Discard))

err := util.CopyFolderContents(logger, filepath.FromSlash(src), filepath.FromSlash(dest), ".terragrunt-test", nil)
err := util.CopyFolderContents(logger, filepath.FromSlash(src), filepath.FromSlash(dest), ".terragrunt-test", nil, nil)
require.NoError(t, err)
}
5 changes: 3 additions & 2 deletions cli/commands/terraform/file_copy_getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type FileCopyGetter struct {

// List of glob paths that should be included in the copy. This can be used to override the default behavior of
// Terragrunt, which will skip hidden folders.
IncludeInCopy []string
IncludeInCopy []string
ExcludeFromCopy []string

Logger log.Logger
}
Expand All @@ -42,7 +43,7 @@ func (g *FileCopyGetter) Get(dst string, u *url.URL) error {
return errors.Errorf("source path must be a directory")
}

return util.CopyFolderContents(g.Logger, path, dst, SourceManifestName, g.IncludeInCopy)
return util.CopyFolderContents(g.Logger, path, dst, SourceManifestName, g.IncludeInCopy, g.ExcludeFromCopy)
}

// GetFile The original FileGetter already knows how to do file copying so long as we set the Copy flag to true, so just
Expand Down
8 changes: 5 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ type ErrorHook struct {
func (conf *Hook) String() string {
return fmt.Sprintf("Hook{Name = %s, Commands = %v}", conf.Name, len(conf.Commands))
}

func (conf *ErrorHook) String() string {
return fmt.Sprintf("Hook{Name = %s, Commands = %v}", conf.Name, len(conf.Commands))
}
Expand All @@ -446,7 +447,8 @@ type TerraformConfig struct {

// Ideally we can avoid the pointer to list slice, but if it is not a pointer, Terraform requires the attribute to
// be defined and we want to make this optional.
IncludeInCopy *[]string `hcl:"include_in_copy,attr"`
IncludeInCopy *[]string `hcl:"include_in_copy,attr"`
ExcludeFromCopy *[]string `hcl:"exclude_from_copy,attr"`

CopyTerraformLockFile *bool `hcl:"copy_terraform_lock_file,attr"`
}
Expand Down Expand Up @@ -1380,7 +1382,7 @@ func (cfg *TerragruntConfig) GetMapFieldMetadata(fieldType, fieldName string) (m
return nil, false
}

var result = make(map[string]string)
result := make(map[string]string)
for key, value := range value {
result[key] = fmt.Sprintf("%v", value)
}
Expand All @@ -1394,7 +1396,7 @@ func (cfg *TerragruntConfig) EngineOptions() (*options.EngineOptions, error) {
return nil, nil
}
// in case of Meta is null, set empty meta
var meta = map[string]interface{}{}
meta := map[string]interface{}{}

if cfg.Engine.Meta != nil {
parsedMeta, err := ParseCtyValueToMap(*cfg.Engine.Meta)
Expand Down
2 changes: 2 additions & 0 deletions config/config_as_cty.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ type CtyTerraformConfig struct {
ExtraArgs map[string]TerraformExtraArguments `cty:"extra_arguments"`
Source *string `cty:"source"`
IncludeInCopy *[]string `cty:"include_in_copy"`
ExcludeFromCopy *[]string `cty:"exclude_from_copy"`
CopyTerraformLockFile *bool `cty:"copy_terraform_lock_file"`
BeforeHooks map[string]Hook `cty:"before_hook"`
AfterHooks map[string]Hook `cty:"after_hook"`
Expand All @@ -525,6 +526,7 @@ func terraformConfigAsCty(config *TerraformConfig) (cty.Value, error) {
configCty := CtyTerraformConfig{
Source: config.Source,
IncludeInCopy: config.IncludeInCopy,
ExcludeFromCopy: config.ExcludeFromCopy,
CopyTerraformLockFile: config.CopyTerraformLockFile,
ExtraArgs: map[string]TerraformExtraArguments{},
BeforeHooks: map[string]Hook{},
Expand Down
12 changes: 12 additions & 0 deletions config/include.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,18 @@ func (cfg *TerragruntConfig) DeepMerge(sourceConfig *TerragruntConfig, terragrun
}
}

if sourceConfig.Terraform.ExcludeFromCopy != nil {
srcList := *sourceConfig.Terraform.ExcludeFromCopy

if cfg.Terraform.ExcludeFromCopy != nil {
targetList := *cfg.Terraform.ExcludeFromCopy
combinedList := append(srcList, targetList...)
cfg.Terraform.ExcludeFromCopy = &combinedList
} else {
cfg.Terraform.ExcludeFromCopy = &srcList
}
}

mergeExtraArgs(terragruntOptions, sourceConfig.Terraform.ExtraArgs, &cfg.Terraform.ExtraArgs)

mergeHooks(terragruntOptions, sourceConfig.Terraform.BeforeHooks, &cfg.Terraform.BeforeHooks)
Expand Down
11 changes: 11 additions & 0 deletions config/include_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ func TestMergeConfigIntoIncludedConfig(t *testing.T) {
&config.TerragruntConfig{Terraform: &config.TerraformConfig{IncludeInCopy: &[]string{"abc"}}},
&config.TerragruntConfig{Terraform: &config.TerraformConfig{CopyTerraformLockFile: &[]bool{false}[0], IncludeInCopy: &[]string{"abc"}}},
},
{
&config.TerragruntConfig{Terraform: &config.TerraformConfig{CopyTerraformLockFile: &[]bool{false}[0]}},
&config.TerragruntConfig{Terraform: &config.TerraformConfig{ExcludeFromCopy: &[]string{"abc"}}},
&config.TerragruntConfig{Terraform: &config.TerraformConfig{CopyTerraformLockFile: &[]bool{false}[0], ExcludeFromCopy: &[]string{"abc"}}},
},
}

for _, testCase := range testCases {
Expand Down Expand Up @@ -324,6 +329,12 @@ func TestDeepMergeConfigIntoIncludedConfig(t *testing.T) {
&config.TerragruntConfig{Terraform: &config.TerraformConfig{IncludeInCopy: &[]string{"abc"}}},
&config.TerragruntConfig{Terraform: &config.TerraformConfig{CopyTerraformLockFile: &[]bool{false}[0], IncludeInCopy: &[]string{"abc"}}},
},
{
"terraform copy_terraform_lock_file",
&config.TerragruntConfig{Terraform: &config.TerraformConfig{CopyTerraformLockFile: &[]bool{false}[0]}},
&config.TerragruntConfig{Terraform: &config.TerraformConfig{ExcludeFromCopy: &[]string{"abc"}}},
&config.TerragruntConfig{Terraform: &config.TerraformConfig{CopyTerraformLockFile: &[]bool{false}[0], ExcludeFromCopy: &[]string{"abc"}}},
},
}

for _, tt := range tc {
Expand Down
4 changes: 4 additions & 0 deletions docs/_docs/04_reference/config-blocks-and-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ The `terraform` block supports the following arguments:
can specify that in this list to ensure it gets copied over to the scratch copy
(e.g., `include_in_copy = [".python-version"]`).

- `exclude_from_copy` (attribute): A list of glob patterns (e.g., `["*.txt"]`) that should always be skipped when copying into the
OpenTofu/Terraform working directory. All examples valid for `include_in_copy` can be used here.
When `include_in_copy` is provided, you can still provide `exclude_from_copy` to skip provided glob, or nested glob from `include_in_copy`

- `copy_terraform_lock_file` (attribute): In certain use cases, you don't want to check the terraform provider lock
file into your source repository from your working directory as described in
[Lock File Handling]({{site.baseurl}}/docs/features/lock-file-handling/). This attribute allows you to disable the copy
Expand Down
Loading