-
-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathdescribe_affected_deleted.go
More file actions
315 lines (269 loc) · 9.78 KB
/
describe_affected_deleted.go
File metadata and controls
315 lines (269 loc) · 9.78 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package exec
import (
"fmt"
"strings"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/perf"
"github.com/cloudposse/atmos/pkg/schema"
)
// detectDeletedComponents detects components and stacks that exist in BASE (remoteStacks)
// but have been deleted in HEAD (currentStacks).
// This enables CI/CD pipelines to identify resources that need terraform destroy.
func detectDeletedComponents(
remoteStacks *map[string]any,
currentStacks *map[string]any,
atmosConfig *schema.AtmosConfiguration,
stackToFilter string,
) ([]schema.Affected, error) {
defer perf.Track(atmosConfig, "exec.detectDeletedComponents")()
var deleted []schema.Affected
// Iterate over BASE stacks to find deletions.
for stackName, remoteStackSection := range *remoteStacks {
// If --stack filter is provided, skip other stacks.
if stackToFilter != "" && stackToFilter != stackName {
continue
}
remoteStackMap, ok := remoteStackSection.(map[string]any)
if !ok {
continue
}
remoteComponentsSection, ok := remoteStackMap["components"].(map[string]any)
if !ok {
continue
}
// Check if the stack exists in HEAD.
currentStackSection, stackExistsInHead := (*currentStacks)[stackName]
if !stackExistsInHead {
// Entire stack was deleted - add all non-abstract components.
stackDeleted := processDeletedStack(
stackName,
remoteComponentsSection,
atmosConfig,
)
deleted = append(deleted, stackDeleted...)
} else {
// Stack exists but check for deleted components within.
componentDeleted, err := processDeletedComponentsInStack(
stackName,
remoteComponentsSection,
currentStackSection,
atmosConfig,
)
if err != nil {
return nil, err
}
deleted = append(deleted, componentDeleted...)
}
}
return deleted, nil
}
// processDeletedStack handles the case where an entire stack was deleted.
// All non-abstract components in the stack are marked as deleted with deletion_type: "stack".
func processDeletedStack(
stackName string,
remoteComponentsSection map[string]any,
atmosConfig *schema.AtmosConfiguration,
) []schema.Affected {
defer perf.Track(atmosConfig, "exec.processDeletedStack")()
return processAllComponentsAsDeleted(
stackName,
remoteComponentsSection,
atmosConfig,
affectedReasonDeletedStack,
deletionTypeStack,
)
}
// processAllComponentsAsDeleted marks all non-abstract components in a stack as deleted.
// This is used both when an entire stack is deleted and when a stack exists but has no components section.
func processAllComponentsAsDeleted(
stackName string,
remoteComponentsSection map[string]any,
atmosConfig *schema.AtmosConfiguration,
affectedReason string,
deletionType string,
) []schema.Affected {
var deleted []schema.Affected
// Process each component type.
for _, componentType := range []string{cfg.TerraformComponentType, cfg.HelmfileComponentType, cfg.PackerComponentType} {
componentTypeSection, ok := remoteComponentsSection[componentType].(map[string]any)
if !ok {
continue
}
for componentName, compSection := range componentTypeSection {
componentSection, ok := compSection.(map[string]any)
if !ok {
continue
}
// Skip abstract components - they are not provisioned.
if isAbstractComponent(componentSection) {
continue
}
affected := createDeletedAffectedItem(&deletedItemParams{
componentName: componentName,
stackName: stackName,
componentType: componentType,
componentSection: &componentSection,
affectedReason: affectedReason,
deletionType: deletionType,
atmosConfig: atmosConfig,
})
deleted = append(deleted, affected)
}
}
return deleted
}
// processDeletedComponentsInStack handles the case where a stack exists but some components were deleted.
// Components that exist in BASE but not in HEAD are marked as deleted with deletion_type: "component".
//
//nolint:funlen,revive // function-length: logic is straightforward, splitting would reduce readability.
func processDeletedComponentsInStack(
stackName string,
remoteComponentsSection map[string]any,
currentStackSection any,
atmosConfig *schema.AtmosConfiguration,
) ([]schema.Affected, error) {
defer perf.Track(atmosConfig, "exec.processDeletedComponentsInStack")()
currentStackMap, ok := currentStackSection.(map[string]any)
if !ok {
return nil, nil
}
currentComponentsSection, ok := currentStackMap["components"].(map[string]any)
if !ok {
// Stack exists in HEAD but has no components section - all BASE components are deleted.
// Use deletion_type: "component" (not "stack") since the stack itself still exists.
return processAllComponentsAsDeleted(
stackName,
remoteComponentsSection,
atmosConfig,
affectedReasonDeleted,
deletionTypeComponent,
), nil
}
var deleted []schema.Affected
// Process each component type.
for _, componentType := range []string{cfg.TerraformComponentType, cfg.HelmfileComponentType, cfg.PackerComponentType} {
remoteTypeSection, ok := remoteComponentsSection[componentType].(map[string]any)
if !ok {
continue
}
currentTypeSection, _ := currentComponentsSection[componentType].(map[string]any)
for componentName, compSection := range remoteTypeSection {
componentSection, ok := compSection.(map[string]any)
if !ok {
continue
}
// Skip abstract components - they are not provisioned.
if isAbstractComponent(componentSection) {
continue
}
// Check if component exists in HEAD.
if currentTypeSection != nil {
if _, existsInHead := currentTypeSection[componentName]; existsInHead {
// Component exists in HEAD - not deleted.
continue
}
}
// Component was deleted.
affected := createDeletedAffectedItem(&deletedItemParams{
componentName: componentName,
stackName: stackName,
componentType: componentType,
componentSection: &componentSection,
affectedReason: affectedReasonDeleted,
deletionType: deletionTypeComponent,
atmosConfig: atmosConfig,
})
deleted = append(deleted, affected)
}
}
return deleted, nil
}
// isAbstractComponent checks if a component has metadata.type = "abstract".
func isAbstractComponent(componentSection map[string]any) bool {
metadataSection, ok := componentSection[sectionNameMetadata].(map[string]any)
if !ok {
return false
}
metadataType, ok := metadataSection["type"].(string)
if !ok {
return false
}
return metadataType == "abstract"
}
// deletedItemParams holds parameters for creating a deleted affected item.
type deletedItemParams struct {
componentName string
stackName string
componentType string
componentSection *map[string]any
affectedReason string
deletionType string
atmosConfig *schema.AtmosConfiguration
}
// createDeletedAffectedItem creates an Affected item for a deleted component.
func createDeletedAffectedItem(params *deletedItemParams) schema.Affected {
affected := schema.Affected{
Component: params.componentName,
ComponentType: params.componentType,
Stack: params.stackName,
Affected: params.affectedReason,
AffectedAll: []string{params.affectedReason},
Deleted: true,
DeletionType: params.deletionType,
}
// Build component path from the BASE component section.
affected.ComponentPath = BuildComponentPath(params.atmosConfig, params.componentSection, params.componentType, params.componentName)
affected.StackSlug = fmt.Sprintf("%s-%s", params.stackName, strings.ReplaceAll(params.componentName, "/", "-"))
// Extract metadata from the component's vars section (same as non-deleted items).
if params.componentSection != nil {
populateDeletedItemMetadata(&affected, params)
}
return affected
}
// populateDeletedItemMetadata extracts and populates metadata fields from the component section.
func populateDeletedItemMetadata(affected *schema.Affected, params *deletedItemParams) {
componentSection := *params.componentSection
// Extract vars section and use GetContextFromVars for consistency with the rest of the codebase.
varsSection, ok := componentSection[cfg.VarsSectionName].(map[string]any)
if !ok {
return
}
// Use cfg.GetContextFromVars for consistency with spacelift_utils.go and atlantis_utils.go.
context := cfg.GetContextFromVars(varsSection)
// Populate context metadata fields.
affected.Namespace = context.Namespace
affected.Tenant = context.Tenant
affected.Environment = context.Environment
affected.Stage = context.Stage
// For Terraform components, also populate Spacelift stack and Atlantis project names.
if params.componentType == cfg.TerraformComponentType {
populateDeletedItemIntegrations(affected, params, varsSection, componentSection)
}
}
// populateDeletedItemIntegrations populates Spacelift and Atlantis names for deleted Terraform components.
func populateDeletedItemIntegrations(
affected *schema.Affected,
params *deletedItemParams,
varsSection map[string]any,
componentSection map[string]any,
) {
settingsSection, _ := componentSection[cfg.SettingsSectionName].(map[string]any)
configAndStacksInfo := schema.ConfigAndStacksInfo{
ComponentFromArg: params.componentName,
Stack: params.stackName,
ComponentVarsSection: varsSection,
ComponentSettingsSection: settingsSection,
ComponentSection: map[string]any{
cfg.VarsSectionName: varsSection,
cfg.SettingsSectionName: settingsSection,
},
}
// Build Spacelift stack name (ignore errors - field is optional).
if spaceliftStackName, err := BuildSpaceliftStackNameFromComponentConfig(params.atmosConfig, configAndStacksInfo); err == nil {
affected.SpaceliftStack = spaceliftStackName
}
// Build Atlantis project name (ignore errors - field is optional).
if atlantisProjectName, err := BuildAtlantisProjectNameFromComponentConfig(params.atmosConfig, configAndStacksInfo); err == nil {
affected.AtlantisProject = atlantisProjectName
}
}