From 94ea935cf13f4011c90b059f405779b13df16822 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 20 Sep 2024 13:56:32 -0400 Subject: [PATCH] pass ephemeral through various contexts --- internal/terraform/context_walk.go | 2 + internal/terraform/evaluate.go | 5 +++ internal/terraform/graph_walk_context.go | 40 ++++++++++--------- internal/terraform/node_module_expand.go | 4 ++ internal/terraform/node_module_expand_test.go | 7 +++- 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/internal/terraform/context_walk.go b/internal/terraform/context_walk.go index 37831ccc20..a4cdba8ad9 100644 --- a/internal/terraform/context_walk.go +++ b/internal/terraform/context_walk.go @@ -17,6 +17,7 @@ import ( "github.com/hashicorp/terraform/internal/plans/deferring" "github.com/hashicorp/terraform/internal/providers" "github.com/hashicorp/terraform/internal/refactoring" + "github.com/hashicorp/terraform/internal/resources/ephemeral" "github.com/hashicorp/terraform/internal/states" "github.com/hashicorp/terraform/internal/tfdiags" ) @@ -186,6 +187,7 @@ func (c *Context) graphWalker(graph *Graph, operation walkOperation, opts *graph PrevRunState: prevRunState, Changes: changes.SyncWrapper(), NamedValues: namedvals.NewState(), + EphemeralResources: ephemeral.NewResources(), Deferrals: deferred, Checks: checkState, InstanceExpander: instances.NewExpander(opts.Overrides), diff --git a/internal/terraform/evaluate.go b/internal/terraform/evaluate.go index eb5a7323c4..adc02b1f1e 100644 --- a/internal/terraform/evaluate.go +++ b/internal/terraform/evaluate.go @@ -21,6 +21,7 @@ import ( "github.com/hashicorp/terraform/internal/namedvals" "github.com/hashicorp/terraform/internal/plans" "github.com/hashicorp/terraform/internal/plans/deferring" + "github.com/hashicorp/terraform/internal/resources/ephemeral" "github.com/hashicorp/terraform/internal/states" "github.com/hashicorp/terraform/internal/tfdiags" ) @@ -50,6 +51,10 @@ type Evaluator struct { // variables, local values, and output values. NamedValues *namedvals.State + // EphemeralResources tracks the currently-open instances of any ephemeral + // resources. + EphemeralResources *ephemeral.Resources + // Deferrals tracks resources and modules that have had either their // expansion or their specific planned actions deferred to a future // plan/apply round. diff --git a/internal/terraform/graph_walk_context.go b/internal/terraform/graph_walk_context.go index 662ae0ff42..5f18184f3b 100644 --- a/internal/terraform/graph_walk_context.go +++ b/internal/terraform/graph_walk_context.go @@ -21,6 +21,7 @@ import ( "github.com/hashicorp/terraform/internal/providers" "github.com/hashicorp/terraform/internal/provisioners" "github.com/hashicorp/terraform/internal/refactoring" + "github.com/hashicorp/terraform/internal/resources/ephemeral" "github.com/hashicorp/terraform/internal/states" "github.com/hashicorp/terraform/internal/tfdiags" ) @@ -32,14 +33,15 @@ type ContextGraphWalker struct { // Configurable values Context *Context - State *states.SyncState // Used for safe concurrent access to state - RefreshState *states.SyncState // Used for safe concurrent access to state - PrevRunState *states.SyncState // Used for safe concurrent access to state - Changes *plans.ChangesSync // Used for safe concurrent writes to changes - Checks *checks.State // Used for safe concurrent writes of checkable objects and their check results - NamedValues *namedvals.State // Tracks evaluation of input variables, local values, and output values - InstanceExpander *instances.Expander // Tracks our gradual expansion of module and resource instances - Deferrals *deferring.Deferred // Tracks any deferred actions + State *states.SyncState // Used for safe concurrent access to state + RefreshState *states.SyncState // Used for safe concurrent access to state + PrevRunState *states.SyncState // Used for safe concurrent access to state + Changes *plans.ChangesSync // Used for safe concurrent writes to changes + Checks *checks.State // Used for safe concurrent writes of checkable objects and their check results + NamedValues *namedvals.State // Tracks evaluation of input variables, local values, and output values + InstanceExpander *instances.Expander // Tracks our gradual expansion of module and resource instances + Deferrals *deferring.Deferred // Tracks any deferred actions + EphemeralResources *ephemeral.Resources // Tracks active instances of ephemeral resources Imports []configs.Import MoveResults refactoring.MoveResults // Read-only record of earlier processing of move statements Operation walkOperation @@ -98,22 +100,24 @@ func (w *ContextGraphWalker) EvalContext() EvalContext { // so that we can safely run multiple evaluations at once across // different modules. evaluator := &Evaluator{ - Meta: w.Context.meta, - Config: w.Config, - Operation: w.Operation, - State: w.State, - Changes: w.Changes, - Plugins: w.Context.plugins, - Instances: w.InstanceExpander, - NamedValues: w.NamedValues, - Deferrals: w.Deferrals, - PlanTimestamp: w.PlanTimestamp, + Meta: w.Context.meta, + Config: w.Config, + Operation: w.Operation, + State: w.State, + Changes: w.Changes, + EphemeralResources: w.EphemeralResources, + Plugins: w.Context.plugins, + Instances: w.InstanceExpander, + NamedValues: w.NamedValues, + Deferrals: w.Deferrals, + PlanTimestamp: w.PlanTimestamp, } ctx := &BuiltinEvalContext{ StopContext: w.StopContext, Hooks: w.Context.hooks, InputValue: w.Context.uiInput, + EphemeralResourcesValue: w.EphemeralResources, InstanceExpanderValue: w.InstanceExpander, Plugins: w.Context.plugins, ExternalProviderConfigs: w.ExternalProviderConfigs, diff --git a/internal/terraform/node_module_expand.go b/internal/terraform/node_module_expand.go index 07b8953ee1..5a86c7f4f9 100644 --- a/internal/terraform/node_module_expand.go +++ b/internal/terraform/node_module_expand.go @@ -4,6 +4,7 @@ package terraform import ( + "context" "log" "github.com/hashicorp/terraform/internal/addrs" @@ -213,6 +214,9 @@ func (n *nodeCloseModule) Execute(ctx EvalContext, op walkOperation) (diags tfdi // any running plugins diags = diags.Append(ctx.ClosePlugins()) + // We also close up the ephemeral resource manager + diags = diags.Append(ctx.EphemeralResources().Close(context.TODO())) + switch op { case walkApply, walkDestroy: state := ctx.State().Lock() diff --git a/internal/terraform/node_module_expand_test.go b/internal/terraform/node_module_expand_test.go index 6c9f8a813a..84252fb539 100644 --- a/internal/terraform/node_module_expand_test.go +++ b/internal/terraform/node_module_expand_test.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/terraform/internal/addrs" "github.com/hashicorp/terraform/internal/configs" "github.com/hashicorp/terraform/internal/instances" + "github.com/hashicorp/terraform/internal/resources/ephemeral" "github.com/hashicorp/terraform/internal/states" ) @@ -43,7 +44,8 @@ func TestNodeCloseModuleExecute(t *testing.T) { state := states.NewState() state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey)) ctx := &MockEvalContext{ - StateState: state.SyncWrapper(), + StateState: state.SyncWrapper(), + EphemeralResourcesResources: ephemeral.NewResources(), } node := nodeCloseModule{addrs.Module{"child"}} diags := node.Execute(ctx, walkApply) @@ -74,7 +76,8 @@ func TestNodeCloseModuleExecute(t *testing.T) { state := states.NewState() state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey)) ctx := &MockEvalContext{ - StateState: state.SyncWrapper(), + StateState: state.SyncWrapper(), + EphemeralResourcesResources: ephemeral.NewResources(), } node := nodeCloseModule{addrs.Module{"child"}}