-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
describe_dependents.go
267 lines (224 loc) 路 8.59 KB
/
describe_dependents.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
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
package exec
import (
"fmt"
"reflect"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/spf13/cobra"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/schema"
)
// ExecuteDescribeDependentsCmd executes `describe dependents` command
func ExecuteDescribeDependentsCmd(cmd *cobra.Command, args []string) error {
info, err := processCommandLineArgs("", cmd, args, nil)
if err != nil {
return err
}
cliConfig, err := cfg.InitCliConfig(info, true)
if err != nil {
return err
}
if len(args) != 1 {
return errors.New("invalid arguments. The command requires one argument `component`")
}
flags := cmd.Flags()
stack, err := flags.GetString("stack")
if err != nil {
return err
}
format, err := flags.GetString("format")
if err != nil {
return err
}
file, err := flags.GetString("file")
if err != nil {
return err
}
component := args[0]
dependents, err := ExecuteDescribeDependents(cliConfig, component, stack)
if err != nil {
return err
}
err = printOrWriteToFile(format, file, dependents)
if err != nil {
return err
}
return nil
}
// ExecuteDescribeDependents produces a list of Atmos components in Atmos stacks that depend on the provided Atmos component
func ExecuteDescribeDependents(
cliConfig schema.CliConfiguration,
component string,
stack string,
) ([]schema.Dependent, error) {
dependents := []schema.Dependent{}
var ok bool
// Get all stacks with all components
stacks, err := ExecuteDescribeStacks(cliConfig, "", nil, nil, nil, false)
if err != nil {
return nil, err
}
providedComponentSection, err := ExecuteDescribeComponent(component, stack)
if err != nil {
return nil, err
}
// Get the provided component `vars`
var providedComponentVarsSection map[any]any
if providedComponentVarsSection, ok = providedComponentSection["vars"].(map[any]any); !ok {
return dependents, nil
}
// Convert the provided component `vars` section to the `Context` structure
var providedComponentVars schema.Context
err = mapstructure.Decode(providedComponentVarsSection, &providedComponentVars)
if err != nil {
return nil, err
}
// Iterate over all stacks and all components in the stacks
for stackName, stackSection := range stacks {
var stackSectionMap map[string]any
if stackSectionMap, ok = stackSection.(map[string]any); !ok {
continue
}
// Get the stack `components` section
var stackComponentsSection map[string]any
if stackComponentsSection, ok = stackSectionMap["components"].(map[string]any); !ok {
continue
}
for stackComponentType, stackComponentTypeSection := range stackComponentsSection {
var stackComponentTypeSectionMap map[string]any
if stackComponentTypeSectionMap, ok = stackComponentTypeSection.(map[string]any); !ok {
continue
}
for stackComponentName, stackComponent := range stackComponentTypeSectionMap {
var stackComponentMap map[string]any
if stackComponentMap, ok = stackComponent.(map[string]any); !ok {
continue
}
// Skip the stack component if it's the same as the provided component
if stackComponentName == component {
continue
}
// Skip abstract components
if metadataSection, ok := stackComponentMap["metadata"].(map[any]any); ok {
if metadataType, ok := metadataSection["type"].(string); ok {
if metadataType == "abstract" {
continue
}
}
}
// Get the stack component `vars`
var stackComponentVarsSection map[any]any
if stackComponentVarsSection, ok = stackComponentMap["vars"].(map[any]any); !ok {
return dependents, nil
}
// Convert the stack component `vars` section to the `Context` structure
var stackComponentVars schema.Context
err = mapstructure.Decode(stackComponentVarsSection, &stackComponentVars)
if err != nil {
return nil, err
}
// Get the stack component `settings`
var stackComponentSettingsSection map[any]any
if stackComponentSettingsSection, ok = stackComponentMap["settings"].(map[any]any); !ok {
continue
}
// Convert the `settings` section to the `Settings` structure
var stackComponentSettings schema.Settings
err = mapstructure.Decode(stackComponentSettingsSection, &stackComponentSettings)
if err != nil {
return nil, err
}
// Skip if the stack component has an empty `settings.depends_on` section
if reflect.ValueOf(stackComponentSettings).IsZero() ||
reflect.ValueOf(stackComponentSettings.DependsOn).IsZero() {
continue
}
// Check if the stack component is a dependent of the provided component
for _, dependsOn := range stackComponentSettings.DependsOn {
if dependsOn.Component != component {
continue
}
// Include the component from the stack if any of the following is true:
// - `namespace` is specified in `depends_on` and the provided component's namespace is equal to the namespace in `depends_on`
// - `namespace` is not specified in `depends_on` and the provided component is from the same namespace as the component in `depends_on`
if dependsOn.Namespace != "" {
if providedComponentVars.Namespace != dependsOn.Namespace {
continue
}
} else if providedComponentVars.Namespace != stackComponentVars.Namespace {
continue
}
// Include the component from the stack if any of the following is true:
// - `tenant` is specified in `depends_on` and the provided component's tenant is equal to the tenant in `depends_on`
// - `tenant` is not specified in `depends_on` and the provided component is from the same tenant as the component in `depends_on`
if dependsOn.Tenant != "" {
if providedComponentVars.Tenant != dependsOn.Tenant {
continue
}
} else if providedComponentVars.Tenant != stackComponentVars.Tenant {
continue
}
// Include the component from the stack if any of the following is true:
// - `environment` is specified in `depends_on` and the component's environment is equal to the environment in `depends_on`
// - `environment` is not specified in `depends_on` and the provided component is from the same environment as the component in `depends_on`
if dependsOn.Environment != "" {
if providedComponentVars.Environment != dependsOn.Environment {
continue
}
} else if providedComponentVars.Environment != stackComponentVars.Environment {
continue
}
// Include the component from the stack if any of the following is true:
// - `stage` is specified in `depends_on` and the provided component's stage is equal to the stage in `depends_on`
// - `stage` is not specified in `depends_on` and the provided component is from the same stage as the component in `depends_on`
if dependsOn.Stage != "" {
if providedComponentVars.Stage != dependsOn.Stage {
continue
}
} else if providedComponentVars.Stage != stackComponentVars.Stage {
continue
}
dependent := schema.Dependent{
Component: stackComponentName,
ComponentPath: BuildComponentPath(cliConfig, stackComponentMap, stackComponentType),
ComponentType: stackComponentType,
Stack: stackName,
StackSlug: fmt.Sprintf("%s-%s", stackName, strings.Replace(stackComponentName, "/", "-", -1)),
Namespace: stackComponentVars.Namespace,
Tenant: stackComponentVars.Tenant,
Environment: stackComponentVars.Environment,
Stage: stackComponentVars.Stage,
}
// Add Spacelift stack and Atlantis project if they are configured for the dependent stack component
if stackComponentType == "terraform" {
// Spacelift stack
configAndStacksInfo := schema.ConfigAndStacksInfo{
ComponentFromArg: stackComponentName,
Stack: stackName,
ComponentVarsSection: stackComponentVarsSection,
ComponentSettingsSection: stackComponentSettingsSection,
ComponentSection: map[string]any{
cfg.VarsSectionName: stackComponentVarsSection,
cfg.SettingsSectionName: stackComponentSettingsSection,
},
}
spaceliftStackName, err := BuildSpaceliftStackNameFromComponentConfig(cliConfig, configAndStacksInfo)
if err != nil {
return nil, err
}
dependent.SpaceliftStack = spaceliftStackName
// Atlantis project
atlantisProjectName, err := BuildAtlantisProjectNameFromComponentConfig(cliConfig, configAndStacksInfo)
if err != nil {
return nil, err
}
dependent.AtlantisProject = atlantisProjectName
}
dependents = append(dependents, dependent)
}
}
}
}
return dependents, nil
}