Skip to content

Commit

Permalink
Stack: stack values (#3877)
Browse files Browse the repository at this point in the history
* Terragrunt values struct

* CTY values parsing

* Add psasing of unit values

* Values passing in

* Cty clone update

* Stack clone example

* Lint issues fixes

* add stack values

* Added test for stack values

* Updated experiments docs

* added check for empty path
  • Loading branch information
denis256 authored Feb 15, 2025
1 parent b782646 commit cdba05b
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 9 deletions.
35 changes: 35 additions & 0 deletions cli/commands/stack/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"os"
"path/filepath"

"github.com/gruntwork-io/terragrunt/config"
"github.com/zclconf/go-cty/cty"

runall "github.com/gruntwork-io/terragrunt/cli/commands/run-all"
"github.com/gruntwork-io/terragrunt/internal/cli"

Expand All @@ -26,6 +29,10 @@ func RunGenerate(ctx context.Context, opts *options.TerragruntOptions) error {
return err
}

if err := populateStackValues(ctx, opts); err != nil {
return errors.New(err)
}

return generateStack(ctx, opts)
}

Expand All @@ -35,6 +42,10 @@ func Run(ctx context.Context, opts *options.TerragruntOptions) error {
return err
}

if err := populateStackValues(ctx, opts); err != nil {
return errors.New(err)
}

if err := RunGenerate(ctx, opts); err != nil {
return err
}
Expand All @@ -50,6 +61,10 @@ func RunOutput(ctx context.Context, opts *options.TerragruntOptions, index strin
return err
}

if err := populateStackValues(ctx, opts); err != nil {
return errors.New(err)
}

// collect outputs
outputs, err := generateOutput(ctx, opts)
if err != nil {
Expand Down Expand Up @@ -103,3 +118,23 @@ func checkStackExperiment(opts *options.TerragruntOptions) error {

return nil
}

func populateStackValues(ctx context.Context, opts *options.TerragruntOptions) error {
opts.TerragruntStackConfigPath = filepath.Join(opts.WorkingDir, defaultStackFile)

stackFile, err := config.ReadStackConfigFile(ctx, opts)
if err != nil {
return errors.Errorf("Failed to read stack file from %s %v", opts.WorkingDir, err)
}

unitValues := map[string]*cty.Value{}

for _, unit := range stackFile.Units {
path := filepath.Join(opts.WorkingDir, stackDir, unit.Path)
unitValues[path] = unit.Values
}

opts.StackValues = options.NewStackValues(&cty.NilVal, unitValues)

return nil
}
5 changes: 2 additions & 3 deletions cli/commands/stack/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ import (
)

const (
StackManifestName = ".terragrunt-stack-manifest"
ManifestName = ".terragrunt-stack-manifest"
)

func generateStack(ctx context.Context, opts *options.TerragruntOptions) error {
opts.TerragruntStackConfigPath = filepath.Join(opts.WorkingDir, defaultStackFile)
opts.Logger.Infof("Generating stack from %s", opts.TerragruntStackConfigPath)
stackFile, err := config.ReadStackConfigFile(ctx, opts)

Expand Down Expand Up @@ -64,7 +63,7 @@ func processStackFile(ctx context.Context, opts *options.TerragruntOptions, stac
src = unit.Source
}

if err := util.CopyFolderContentsWithFilter(opts.Logger, src, dest, StackManifestName, func(absolutePath string) bool {
if err := util.CopyFolderContentsWithFilter(opts.Logger, src, dest, ManifestName, func(absolutePath string) bool {
return true
}); err != nil {
return errors.New(err)
Expand Down
2 changes: 0 additions & 2 deletions cli/commands/stack/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"io"
"path/filepath"
"strings"

"github.com/hashicorp/hcl/v2/hclwrite"
Expand All @@ -20,7 +19,6 @@ import (
)

func generateOutput(ctx context.Context, opts *options.TerragruntOptions) (map[string]map[string]cty.Value, error) {
opts.TerragruntStackConfigPath = filepath.Join(opts.WorkingDir, defaultStackFile)
opts.Logger.Debugf("Generating output from %s", opts.TerragruntStackConfigPath)
stackFile, err := config.ReadStackConfigFile(ctx, opts)

Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ const (
MetadataErrors = "errors"
MetadataRetry = "retry"
MetadataIgnore = "ignore"
MetadataUnit = "unit"
MetadataValues = "values"
)

var (
Expand Down
10 changes: 10 additions & 0 deletions config/config_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ func createTerragruntEvalContext(ctx *ParsingContext, configPath string) (*hcl.E
evalCtx.Variables[MetadataInclude] = exposedInclude
}

// Add to context unit values
path := filepath.Dir(configPath)
unitValues := ctx.TerragruntOptions.StackValues.UnitValues(path)

if unitValues != nil {
evalCtx.Variables[MetadataUnit] = cty.ObjectVal(map[string]cty.Value{
MetadataValues: *unitValues,
})
}

return evalCtx, nil
}

Expand Down
3 changes: 3 additions & 0 deletions config/locals.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ func canEvaluateLocals(expression hcl.Expression, evaluatedLocals map[string]cty
case rootName == MetadataFeatureFlag:
// If the variable is `feature`

case rootName == MetadataUnit:
// If the variable is `unit`

case rootName != "local":
// We can't evaluate any variable other than `local`
detail = fmt.Sprintf("You can only reference to other local variables here, but it looks like you're referencing something else (%q is not defined)", rootName)
Expand Down
7 changes: 4 additions & 3 deletions config/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ type StackConfigFile struct {

// Unit represent unit from stack file.
type Unit struct {
Name string `hcl:",label"`
Source string `hcl:"source,attr"`
Path string `hcl:"path,attr"`
Name string `hcl:",label"`
Source string `hcl:"source,attr"`
Path string `hcl:"path,attr"`
Values *cty.Value `hcl:"values,attr"`
}

// ReadOutputs reads the outputs from the unit.
Expand Down
2 changes: 1 addition & 1 deletion docs/_docs/04_reference/06-experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ To transition the `stacks` feature to a stable release, the following must be ad

- [x] Add support for `stack run *` command
- [x] Add support for `stack output` commands to extend stack-level operations.
- [ ] Add support for stack "values".
- [x] Add support for stack "values".
- [ ] Add support for recursive stacks.
- [ ] Integration testing for recursive stack handling across typical workflows, ensuring smooth transitions during `plan`, `apply`, and `destroy` operations.
- [ ] Confirm compatibility with parallelism flags (e.g., `--parallel`), especially for stacks with dependencies.
Expand Down
60 changes: 60 additions & 0 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"strings"
"time"

"github.com/zclconf/go-cty/cty"

"github.com/gruntwork-io/terragrunt/internal/cli"
"github.com/gruntwork-io/terragrunt/internal/cloner"
"github.com/gruntwork-io/terragrunt/internal/errors"
Expand Down Expand Up @@ -396,6 +398,9 @@ type TerragruntOptions struct {

// StackOutputFormat format how the stack output is rendered.
StackOutputFormat string

// StackValues is a struct that holds the values of the stack and units.
StackValues *StackValues
}

// TerragruntOptionsFunc is a functional option type used to pass options in certain integration tests
Expand Down Expand Up @@ -511,6 +516,7 @@ func NewTerragruntOptionsWithWriters(stdout, stderr io.Writer) *TerragruntOption
ReadFiles: xsync.NewMapOf[string, []string](),
StrictControls: controls.New(),
Experiments: experiment.NewExperiments(),
StackValues: &StackValues{},
}
}

Expand Down Expand Up @@ -586,6 +592,7 @@ func (opts *TerragruntOptions) OptionsFromContext(ctx context.Context) *Terragru
func (opts *TerragruntOptions) Clone() *TerragruntOptions {
newOpts := cloner.Clone(opts)
newOpts.Logger = opts.Logger.Clone()
newOpts.StackValues = opts.StackValues.Clone()

return newOpts
}
Expand Down Expand Up @@ -962,5 +969,58 @@ func matchesAnyRegexpPattern(input string, patterns []*ErrorsPattern) bool {
return false
}

// StackValues represents the values of the stack.
type StackValues struct {
// Values of the stack.
stackValues *cty.Value
// Mapping of units values mapped to their path
unitValues map[string]*cty.Value
}

// NewStackValues creates a new StackValues object.
func NewStackValues(stackValues *cty.Value, unitValues map[string]*cty.Value) *StackValues {
return &StackValues{
stackValues: stackValues,
unitValues: unitValues,
}
}

// Clone function for StackValues
func (s *StackValues) Clone() *StackValues {
if s == nil {
return nil
}

newValues := NewStackValues(s.stackValues, make(map[string]*cty.Value, len(s.unitValues)))

for k, v := range s.unitValues {
if v != nil {
valCopy := *v
newValues.unitValues[k] = &valCopy
} else {
newValues.unitValues[k] = nil
}
}

return newValues
}

// UnitValues returns the values of the unit at the given path.
func (s *StackValues) UnitValues(path string) *cty.Value {
if s == nil {
return &cty.NilVal
}

if path == "" {
return &cty.NilVal
}

if value, found := s.unitValues[path]; found {
return value
}

return &cty.NilVal
}

// ErrRunTerragruntCommandNotSet is a custom error type indicating that the command is not set.
var ErrRunTerragruntCommandNotSet = errors.New("the RunTerragrunt option has not been set on this TerragruntOptions object")
23 changes: 23 additions & 0 deletions test/fixtures/stacks/unit-values/terragrunt.stack.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
locals {
project = "test-project"
}

unit "app1" {
source = "units/app"
path = "app1"

values = {
project = local.project
deployment = "app1"
}
}

unit "app2" {
source = "units/app"
path = "app2"

values = {
project = local.project
deployment = "app2"
}
}
18 changes: 18 additions & 0 deletions test/fixtures/stacks/unit-values/units/app/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

variable "deployment" {}

variable "project" {}

variable "data" {}

output "data" {
value = var.data
}

output "deployment" {
value = var.deployment
}

output "project" {
value = var.project
}
10 changes: 10 additions & 0 deletions test/fixtures/stacks/unit-values/units/app/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

locals {
data = "payload: ${unit.values.deployment}-${unit.values.project}"
}

inputs = {
deployment = unit.values.deployment
project = unit.values.project
data = local.data
}
40 changes: 40 additions & 0 deletions test/integration_stacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
testFixtureStacksRemote = "fixtures/stacks/remote"
testFixtureStacksInputs = "fixtures/stacks/inputs"
testFixtureStacksOutputs = "fixtures/stacks/outputs"
testFixtureStacksUnitValues = "fixtures/stacks/unit-values"
)

func TestStacksGenerateBasic(t *testing.T) {
Expand Down Expand Up @@ -343,6 +344,45 @@ func TestStackOutputsJsonFlag(t *testing.T) {
assert.Len(t, result, 4)
}

func TestStacksUnitValues(t *testing.T) {
t.Parallel()

helpers.CleanupTerraformFolder(t, testFixtureStacksUnitValues)
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureStacksUnitValues)
rootPath := util.JoinPath(tmpEnvPath, testFixtureStacksUnitValues)

stdout, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt stack run apply --experiment stacks --terragrunt-non-interactive --terragrunt-working-dir "+rootPath)
require.NoError(t, err)

assert.Contains(t, stdout, "deployment = \"app1\"")
assert.Contains(t, stdout, "deployment = \"app2\"")
assert.Contains(t, stdout, "project = \"test-project\"")
assert.Contains(t, stdout, "data = \"payload: app1-test-project\"")
assert.Contains(t, stdout, "data = \"payload: app2-test-project\"")
}

func TestStacksUnitValuesOutput(t *testing.T) {
t.Parallel()

helpers.CleanupTerraformFolder(t, testFixtureStacksUnitValues)
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureStacksUnitValues)
rootPath := util.JoinPath(tmpEnvPath, testFixtureStacksUnitValues)

helpers.RunTerragrunt(t, "terragrunt stack run apply --experiment stacks --terragrunt-non-interactive --terragrunt-working-dir "+rootPath)

stdout, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt stack output -json --experiment stacks --terragrunt-non-interactive --terragrunt-working-dir "+rootPath)
require.NoError(t, err)

var result map[string]interface{}
err = json.Unmarshal([]byte(stdout), &result)
require.NoError(t, err)

assert.Len(t, result, 2)
// check if app1 and app2 are present in the result
assert.Contains(t, result, "app1")
assert.Contains(t, result, "app2")
}

// check if the stack directory is created and contains files.
func validateStackDir(t *testing.T, path string) {
t.Helper()
Expand Down

0 comments on commit cdba05b

Please sign in to comment.