-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
graph_walk_context.go
137 lines (119 loc) · 4.85 KB
/
graph_walk_context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package terraform
import (
"context"
"sync"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/checks"
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/hashicorp/terraform/internal/instances"
"github.com/hashicorp/terraform/internal/plans"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/provisioners"
"github.com/hashicorp/terraform/internal/refactoring"
"github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/internal/tfdiags"
)
// ContextGraphWalker is the GraphWalker implementation used with the
// Context struct to walk and evaluate the graph.
type ContextGraphWalker struct {
NullGraphWalker
// 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
InstanceExpander *instances.Expander // Tracks our gradual expansion of module and resource instances
MoveResults refactoring.MoveResults // Read-only record of earlier processing of move statements
Operation walkOperation
StopContext context.Context
RootVariableValues InputValues
Config *configs.Config
// This is an output. Do not set this, nor read it while a graph walk
// is in progress.
NonFatalDiagnostics tfdiags.Diagnostics
once sync.Once
contexts map[string]*BuiltinEvalContext
contextLock sync.Mutex
variableValues map[string]map[string]cty.Value
variableValuesLock sync.Mutex
providerCache map[string]providers.Interface
providerSchemas map[string]*ProviderSchema
providerLock sync.Mutex
provisionerCache map[string]provisioners.Interface
provisionerSchemas map[string]*configschema.Block
provisionerLock sync.Mutex
}
func (w *ContextGraphWalker) EnterPath(path addrs.ModuleInstance) EvalContext {
w.contextLock.Lock()
defer w.contextLock.Unlock()
// If we already have a context for this path cached, use that
key := path.String()
if ctx, ok := w.contexts[key]; ok {
return ctx
}
ctx := w.EvalContext().WithPath(path)
w.contexts[key] = ctx.(*BuiltinEvalContext)
return ctx
}
func (w *ContextGraphWalker) EvalContext() EvalContext {
w.once.Do(w.init)
// Our evaluator shares some locks with the main context and the walker
// 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,
VariableValues: w.variableValues,
VariableValuesLock: &w.variableValuesLock,
}
ctx := &BuiltinEvalContext{
StopContext: w.StopContext,
Hooks: w.Context.hooks,
InputValue: w.Context.uiInput,
InstanceExpanderValue: w.InstanceExpander,
Plugins: w.Context.plugins,
MoveResultsValue: w.MoveResults,
ProviderCache: w.providerCache,
ProviderInputConfig: w.Context.providerInputConfig,
ProviderLock: &w.providerLock,
ProvisionerCache: w.provisionerCache,
ProvisionerLock: &w.provisionerLock,
ChangesValue: w.Changes,
ChecksValue: w.Checks,
StateValue: w.State,
RefreshStateValue: w.RefreshState,
PrevRunStateValue: w.PrevRunState,
Evaluator: evaluator,
VariableValues: w.variableValues,
VariableValuesLock: &w.variableValuesLock,
}
return ctx
}
func (w *ContextGraphWalker) init() {
w.contexts = make(map[string]*BuiltinEvalContext)
w.providerCache = make(map[string]providers.Interface)
w.providerSchemas = make(map[string]*ProviderSchema)
w.provisionerCache = make(map[string]provisioners.Interface)
w.provisionerSchemas = make(map[string]*configschema.Block)
w.variableValues = make(map[string]map[string]cty.Value)
// Populate root module variable values. Other modules will be populated
// during the graph walk.
w.variableValues[""] = make(map[string]cty.Value)
for k, iv := range w.RootVariableValues {
w.variableValues[""][k] = iv.Value
}
}
func (w *ContextGraphWalker) Execute(ctx EvalContext, n GraphNodeExecutable) tfdiags.Diagnostics {
// Acquire a lock on the semaphore
w.Context.parallelSem.Acquire()
defer w.Context.parallelSem.Release()
return n.Execute(ctx, w.Operation)
}