-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathterraform.go
More file actions
282 lines (240 loc) · 8.25 KB
/
Copy pathterraform.go
File metadata and controls
282 lines (240 loc) · 8.25 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package terraform
import (
"context"
"fmt"
"path"
tfjson "github.com/hashicorp/terraform-json"
console "github.com/pluralsh/console/go/client"
"github.com/pluralsh/console/go/polly/algorithms"
"github.com/samber/lo"
"k8s.io/klog/v2"
"github.com/pluralsh/console/go/deployment-operator/internal/helpers"
"github.com/pluralsh/console/go/deployment-operator/pkg/harness/exec"
securityv1 "github.com/pluralsh/console/go/deployment-operator/pkg/harness/security/v1"
tfapi "github.com/pluralsh/console/go/deployment-operator/pkg/harness/tool/terraform/api"
v1 "github.com/pluralsh/console/go/deployment-operator/pkg/harness/tool/v1"
"github.com/pluralsh/console/go/deployment-operator/pkg/log"
)
// State implements [v1.Tool] interface.
func (in *Terraform) State() (*console.StackStateAttributes, error) {
state, err := in.state()
if err != nil || state.Values == nil || state.Values.RootModule == nil {
return nil, err
}
resources := make([]*console.StackStateResourceAttributes, 0)
_ = algorithms.BFS(state.Values.RootModule, func(module *tfjson.StateModule) ([]*tfjson.StateModule, error) {
return module.ChildModules, nil
}, func(module *tfjson.StateModule) error {
klog.V(log.LogLevelTrace).InfoS("visiting module", "module", module)
resources = append(
resources,
tfapi.ToStackStateResourceAttributesList(module.Resources)...,
)
return nil
})
return &console.StackStateAttributes{
State: resources,
}, nil
}
// Plan implements [v1.Tool] interface.
func (in *Terraform) Plan() (*console.StackStateAttributes, error) {
plan, err := in.plan()
if err != nil {
return nil, err
}
return &console.StackStateAttributes{
Plan: &plan,
}, nil
}
// Output implements [v1.Tool] interface.
func (in *Terraform) Output() ([]*console.StackOutputAttributes, error) {
result := make([]*console.StackOutputAttributes, 0)
state, err := in.state()
if err != nil || state.Values == nil || state.Values.Outputs == nil {
return nil, err
}
for k, v := range state.Values.Outputs {
result = append(result, &console.StackOutputAttributes{
Name: k,
Value: tfapi.OutputValueString(v.Value),
Secret: lo.ToPtr(v.Sensitive),
})
}
return result, nil
}
// Modifier implements [v1.Tool] interface.
func (in *Terraform) Modifier(stage console.StepStage) v1.Modifier {
switch stage {
case console.StepStagePlan:
return in.NewPlanArgsModifier(in.planFileName)
case console.StepStageApply:
return in.NewApplyArgsModifier(in.dir, in.planFileName)
}
return v1.NewDefaultModifier()
}
// ConfigureStateBackend implements [v1.Tool] interface.
func (in *Terraform) ConfigureStateBackend(actor, deployToken string, urls *console.StackRunBaseFragment_StateUrls) error {
input := &OverrideTemplateInput{
Address: lo.FromPtr(urls.Terraform.Address),
LockAddress: lo.FromPtr(urls.Terraform.Lock),
UnlockAddress: lo.FromPtr(urls.Terraform.Unlock),
Actor: actor,
DeployToken: deployToken,
}
fileName, content, err := overrideTemplate(input)
if err != nil {
return err
}
if err = helpers.File().Create(path.Join(in.dir, fileName), content, 0644); err != nil {
return fmt.Errorf("failed configuring state backend file %q: %w", fileName, err)
}
return nil
}
func (in *Terraform) state() (*tfjson.State, error) {
state := new(tfjson.State)
output, err := exec.NewExecutable(
"terraform",
exec.WithArgs([]string{"show", "-json"}),
exec.WithDir(in.dir),
).RunWithOutput(context.Background())
if err != nil {
return state, fmt.Errorf("failed executing terraform show -json: %s: %w", string(output), err)
}
err = state.UnmarshalJSON(output)
if err != nil {
return nil, err
}
klog.V(log.LogLevelTrace).InfoS("terraform state read successfully", "state", state)
return state, nil
}
func (in *Terraform) Scan() ([]*console.StackPolicyViolationAttributes, error) {
result := make([]*console.StackPolicyViolationAttributes, 0)
if in.Scanner == nil {
klog.V(log.LogLevelDebug).Info("terraform scanner not configured, skipping")
return result, nil
}
result, err := in.Scanner.Scan(console.StackTypeTerraform, securityv1.WithTerraform(securityv1.TerraformScanOptions{
WorkDir: in.workDir,
Dir: in.dir,
PlanFileName: in.planFileName,
VariablesFileName: in.variablesFileName,
}))
klog.V(log.LogLevelTrace).InfoS("terraform scanner scan", "result", result)
return result, err
}
func (in *Terraform) plan() (string, error) {
output, err := exec.NewExecutable(
"terraform",
exec.WithArgs([]string{"show", in.planFileName}),
exec.WithDir(in.dir),
).RunWithOutput(context.Background())
if err != nil {
return "", fmt.Errorf("failed executing terraform show: %s: %w", string(output), err)
}
klog.V(log.LogLevelTrace).InfoS("terraform plan file read successfully", "file", in.planFileName, "output", string(output))
return string(output), nil
}
// planJSON parses the terraform plan file as JSON and returns a structured plan.
// This provides deterministic access to plan details including resource changes.
func (in *Terraform) planJSON() (*tfjson.Plan, error) {
plan := new(tfjson.Plan)
output, err := exec.NewExecutable(
"terraform",
exec.WithArgs([]string{"show", "-json", in.planFileName}),
exec.WithDir(in.dir),
).RunWithOutput(context.Background())
if err != nil {
return nil, fmt.Errorf("failed executing terraform show -json: %s: %w", string(output), err)
}
err = plan.UnmarshalJSON(output)
if err != nil {
return nil, fmt.Errorf("failed unmarshaling terraform plan JSON: %w", err)
}
klog.V(log.LogLevelTrace).InfoS("terraform plan JSON parsed successfully", "file", in.planFileName)
return plan, nil
}
// HasChanges deterministically checks if the terraform plan contains any changes.
// It analyzes the plan JSON to detect:
// - Resource changes (create, update, delete, replace)
// - Output changes
// - Resource drift
// - Deferred changes (Terraform 1.8+)
//
// Returns true if any changes are detected, false if the plan is a no-op.
func (in *Terraform) HasChanges() (bool, error) {
plan, err := in.planJSON()
if err != nil {
return false, err
}
// If there are deferred changes, we should consider this as having changes
if len(plan.DeferredChanges) > 0 {
klog.V(log.LogLevelDebug).InfoS("plan has deferred changes", "count", len(plan.DeferredChanges))
return true, nil
}
// Check resource changes
hasResourceChanges := false
for _, rc := range plan.ResourceChanges {
if rc.Change != nil && !rc.Change.Actions.NoOp() {
klog.V(log.LogLevelDebug).InfoS("plan has resource changes",
"resource", rc.Address,
"actions", rc.Change.Actions)
hasResourceChanges = true
break
}
}
if hasResourceChanges {
return true, nil
}
// Check output changes
hasOutputChanges := false
for name, oc := range plan.OutputChanges {
if oc != nil && !oc.Actions.NoOp() {
klog.V(log.LogLevelDebug).InfoS("plan has output changes",
"output", name,
"actions", oc.Actions)
hasOutputChanges = true
break
}
}
if hasOutputChanges {
return true, nil
}
// Check resource drift
// Note: Resource drift represents changes detected in actual infrastructure vs. state.
// Even if there are no planned changes, drift indicates infrastructure differences.
// TODO: determine if we want to treat drift as changes requiring apply.
if len(plan.ResourceDrift) > 0 {
klog.V(log.LogLevelDebug).InfoS("plan detected resource drift",
"count", len(plan.ResourceDrift))
return true, nil
}
klog.V(log.LogLevelInfo).InfoS("terraform plan has no changes")
return false, nil
}
func (in *Terraform) init() v1.Tool {
if len(in.dir) == 0 {
klog.Fatal("dir is required")
}
in.planFileName = "terraform.tfplan"
helpers.EnsureFileOrDie(path.Join(in.dir, in.planFileName), nil)
if in.variables != nil && len(*in.variables) > 0 {
in.variablesFileName = "plural.auto.tfvars.json"
helpers.EnsureFileOrDie(path.Join(in.dir, in.variablesFileName), in.variables)
}
return in
}
// New creates a Terraform structure that implements v1.Tool interface.
func New(config v1.Config) v1.Tool {
tf := &Terraform{
DefaultTool: v1.DefaultTool{Scanner: config.Scanner},
workDir: config.WorkDir,
dir: config.ExecDir,
variables: config.Variables,
}
if config.Run != nil {
tf.parallelism = config.Run.Parallelism
tf.refresh = config.Run.Refresh
tf.env = config.Run.Env()
}
return tf.init()
}