-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathdescribe_stacks.go
More file actions
1138 lines (987 loc) · 43.9 KB
/
describe_stacks.go
File metadata and controls
1138 lines (987 loc) · 43.9 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//nolint:revive // File length justified: describe_stacks is core stack processing with complex logic.
package exec
import (
"errors"
"fmt"
"path/filepath"
"strings"
"github.com/go-viper/mapstructure/v2"
errUtils "github.com/cloudposse/atmos/errors"
"github.com/cloudposse/atmos/internal/tui/templates/term"
"github.com/cloudposse/atmos/pkg/auth"
cfg "github.com/cloudposse/atmos/pkg/config"
log "github.com/cloudposse/atmos/pkg/logger"
m "github.com/cloudposse/atmos/pkg/merge"
"github.com/cloudposse/atmos/pkg/pager"
"github.com/cloudposse/atmos/pkg/perf"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
atmosYaml "github.com/cloudposse/atmos/pkg/yaml"
)
// componentInfoKey is the key used for component info in stack sections.
const componentInfoKey = "component_info"
// logFieldStack is the log field key for stack names.
const logFieldStack = "stack"
type DescribeStacksArgs struct {
Query string
FilterByStack string
Components []string
ComponentTypes []string
Sections []string
IgnoreMissingFiles bool
ProcessTemplates bool
ProcessYamlFunctions bool
IncludeEmptyStacks bool
Skip []string
Format string
File string
AuthManager auth.AuthManager // Optional: Auth manager for credential management (from --identity flag).
}
//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -source=$GOFILE -destination=mock_$GOFILE -package=$GOPACKAGE
type DescribeStacksExec interface {
Execute(atmosConfig *schema.AtmosConfiguration, args *DescribeStacksArgs) error
}
type describeStacksExec struct {
pageCreator pager.PageCreator
isTTYSupportForStdout func() bool
printOrWriteToFile func(atmosConfig *schema.AtmosConfiguration, format string, file string, data any) error
executeDescribeStacks func(
atmosConfig *schema.AtmosConfiguration,
filterByStack string,
components []string,
componentTypes []string,
sections []string,
ignoreMissingFiles bool,
processTemplates bool,
processYamlFunctions bool,
includeEmptyStacks bool,
skip []string,
authManager auth.AuthManager,
) (map[string]any, error)
}
func NewDescribeStacksExec() DescribeStacksExec {
defer perf.Track(nil, "exec.NewDescribeStacksExec")()
return &describeStacksExec{
pageCreator: pager.New(),
isTTYSupportForStdout: term.IsTTYSupportForStdout,
printOrWriteToFile: printOrWriteToFile,
executeDescribeStacks: ExecuteDescribeStacks,
}
}
// Execute executes `describe stacks` command.
func (d *describeStacksExec) Execute(atmosConfig *schema.AtmosConfiguration, args *DescribeStacksArgs) error {
defer perf.Track(atmosConfig, "exec.DescribeStacksExec.Execute")()
finalStacksMap, err := d.executeDescribeStacks(
atmosConfig,
args.FilterByStack,
args.Components,
args.ComponentTypes,
args.Sections,
false,
args.ProcessTemplates,
args.ProcessYamlFunctions,
args.IncludeEmptyStacks,
args.Skip,
args.AuthManager,
)
if err != nil {
return err
}
var res any
if args.Query != "" {
res, err = u.EvaluateYqExpression(atmosConfig, finalStacksMap, args.Query)
if err != nil {
return err
}
} else {
res = finalStacksMap
}
return viewWithScroll(&viewWithScrollProps{
pageCreator: d.pageCreator,
isTTYSupportForStdout: d.isTTYSupportForStdout,
printOrWriteToFile: d.printOrWriteToFile,
atmosConfig: atmosConfig,
displayName: "Stacks",
format: args.Format,
file: args.File,
res: res,
})
}
// ExecuteDescribeStacks processes stack manifests and returns the final map of stacks and components.
func ExecuteDescribeStacks(
atmosConfig *schema.AtmosConfiguration,
filterByStack string,
components []string,
componentTypes []string,
sections []string,
ignoreMissingFiles bool,
processTemplates bool,
processYamlFunctions bool,
includeEmptyStacks bool,
skip []string,
authManager auth.AuthManager,
) (map[string]any, error) {
defer perf.Track(atmosConfig, "exec.ExecuteDescribeStacks")()
stacksMap, _, err := FindStacksMap(atmosConfig, ignoreMissingFiles)
if err != nil {
return nil, err
}
finalStacksMap := make(map[string]any)
processedStacks := make(map[string]bool)
var varsSection map[string]any
var metadataSection map[string]any
var authSection map[string]any
var settingsSection map[string]any
var envSection map[string]any
var providersSection map[string]any
var hooksSection map[string]any
var overridesSection map[string]any
var backendSection map[string]any
var backendTypeSection string
var stackName string
var stackManifestName string
for stackFileName, stackSection := range stacksMap {
var context schema.Context
// Delete the stack-wide imports.
delete(stackSection.(map[string]any), "imports")
// Extract the stack-level 'name' field (logical name override).
stackManifestName = getStackManifestName(stackSection)
// Check if the `components` section exists and has explicit components.
hasExplicitComponents := false
if componentsSection, ok := stackSection.(map[string]any)[cfg.ComponentsSectionName]; ok {
if componentsSection != nil {
if terraformSection, ok := componentsSection.(map[string]any)[cfg.TerraformSectionName].(map[string]any); ok {
hasExplicitComponents = len(terraformSection) > 0
}
if helmfileSection, ok := componentsSection.(map[string]any)[cfg.HelmfileSectionName].(map[string]any); ok {
hasExplicitComponents = hasExplicitComponents || len(helmfileSection) > 0
}
if packerSection, ok := componentsSection.(map[string]any)[cfg.PackerSectionName].(map[string]any); ok {
hasExplicitComponents = hasExplicitComponents || len(packerSection) > 0
}
}
}
// Also check for imports.
hasImports := false
if importsSection, ok := stackSection.(map[string]any)["import"].([]any); ok {
hasImports = len(importsSection) > 0
}
// Skip stacks without components or imports when includeEmptyStacks is false.
if !includeEmptyStacks && !hasExplicitComponents && !hasImports {
continue
}
stackName = stackFileName
if processedStacks[stackName] {
continue
}
processedStacks[stackName] = true
if !u.MapKeyExists(finalStacksMap, stackName) {
finalStacksMap[stackName] = make(map[string]any)
finalStacksMap[stackName].(map[string]any)[cfg.ComponentsSectionName] = make(map[string]any)
}
if componentsSection, ok := stackSection.(map[string]any)[cfg.ComponentsSectionName].(map[string]any); ok {
// Terraform.
if len(componentTypes) == 0 || u.SliceContainsString(componentTypes, cfg.TerraformSectionName) {
if terraformSection, ok := componentsSection[cfg.TerraformSectionName].(map[string]any); ok {
for componentName, compSection := range terraformSection {
componentSection, ok := compSection.(map[string]any)
if !ok {
return nil, fmt.Errorf("invalid 'components.terraform.%s' section in the file '%s'", componentName, stackFileName)
}
if comp, ok := componentSection[cfg.ComponentSectionName].(string); !ok || comp == "" {
componentSection[cfg.ComponentSectionName] = componentName
}
// Find all derived components of the provided components and include them in the output.
derivedComponents, err := FindComponentsDerivedFromBaseComponents(stackFileName, terraformSection, components)
if err != nil {
return nil, err
}
if varsSection, ok = componentSection[cfg.VarsSectionName].(map[string]any); !ok {
varsSection = map[string]any{}
}
if metadataSection, ok = componentSection[cfg.MetadataSectionName].(map[string]any); !ok {
metadataSection = map[string]any{}
}
// Process metadata inheritance to resolve metadata.terraform_workspace and other inherited metadata fields.
// This ensures that BuildTerraformWorkspace sees the correctly inherited metadata.
if atmosConfig.Stacks.Inherit.IsMetadataInheritanceEnabled() {
if inheritList, hasInherits := metadataSection[cfg.InheritsSectionName].([]any); hasInherits && len(inheritList) > 0 {
// Initialize base component config accumulator.
baseComponentConfig := &schema.BaseComponentConfig{
BaseComponentVars: make(map[string]any),
BaseComponentSettings: make(map[string]any),
BaseComponentEnv: make(map[string]any),
BaseComponentAuth: make(map[string]any),
BaseComponentMetadata: make(map[string]any),
BaseComponentProviders: make(map[string]any),
BaseComponentHooks: make(map[string]any),
}
baseComponents := []string{}
// Process each inherited component in order (left-to-right merge).
for _, inheritValue := range inheritList {
inheritFrom, ok := inheritValue.(string)
if !ok {
continue // Skip invalid entries.
}
err := ProcessBaseComponentConfig(
atmosConfig,
baseComponentConfig,
terraformSection, // allComponentsMap (contains all components in this stack).
componentName, // component name.
stackFileName, // stack name.
inheritFrom, // base component to inherit from.
"", // componentBasePath (empty for describe stacks).
false, // checkBaseComponentExists (false to be lenient).
&baseComponents, // accumulates inheritance chain.
)
if err != nil {
return nil, err
}
}
// Merge base metadata with component's own metadata.
// Component metadata wins on conflicts (component overrides base).
if len(baseComponentConfig.BaseComponentMetadata) > 0 {
merged, err := m.Merge(
atmosConfig,
[]map[string]any{
baseComponentConfig.BaseComponentMetadata, // Base (lower priority).
metadataSection, // Component (higher priority).
})
if err != nil {
return nil, err
}
metadataSection = merged
}
}
// If component has explicit terraform_workspace, remove pattern/template.
// This ensures the explicit workspace takes precedence over inherited/imported patterns.
// The pattern may come from imports or base components, but explicit workspace should win.
if _, hasExplicitWorkspace := metadataSection["terraform_workspace"].(string); hasExplicitWorkspace {
delete(metadataSection, "terraform_workspace_pattern")
delete(metadataSection, "terraform_workspace_template")
}
}
if settingsSection, ok = componentSection[cfg.SettingsSectionName].(map[string]any); !ok {
settingsSection = map[string]any{}
}
if envSection, ok = componentSection[cfg.EnvSectionName].(map[string]any); !ok {
envSection = map[string]any{}
}
if authSection, ok = componentSection[cfg.AuthSectionName].(map[string]any); !ok {
authSection = map[string]any{}
}
if providersSection, ok = componentSection[cfg.ProvidersSectionName].(map[string]any); !ok {
providersSection = map[string]any{}
}
if hooksSection, ok = componentSection[cfg.HooksSectionName].(map[string]any); !ok {
hooksSection = map[string]any{}
}
if overridesSection, ok = componentSection[cfg.OverridesSectionName].(map[string]any); !ok {
overridesSection = map[string]any{}
}
if backendSection, ok = componentSection[cfg.BackendSectionName].(map[string]any); !ok {
backendSection = map[string]any{}
}
if backendTypeSection, ok = componentSection[cfg.BackendTypeSectionName].(string); !ok {
backendTypeSection = ""
}
configAndStacksInfo := schema.ConfigAndStacksInfo{
ComponentFromArg: componentName,
Stack: stackName,
StackManifestName: stackManifestName,
ComponentMetadataSection: metadataSection,
ComponentVarsSection: varsSection,
ComponentSettingsSection: settingsSection,
ComponentEnvSection: envSection,
ComponentAuthSection: authSection,
ComponentProvidersSection: providersSection,
ComponentHooksSection: hooksSection,
ComponentOverridesSection: overridesSection,
ComponentBackendSection: backendSection,
ComponentBackendType: backendTypeSection,
ComponentSection: map[string]any{
cfg.VarsSectionName: varsSection,
cfg.MetadataSectionName: metadataSection,
cfg.SettingsSectionName: settingsSection,
cfg.EnvSectionName: envSection,
cfg.AuthSectionName: authSection,
cfg.ProvidersSectionName: providersSection,
cfg.HooksSectionName: hooksSection,
cfg.OverridesSectionName: overridesSection,
cfg.BackendSectionName: backendSection,
cfg.BackendTypeSectionName: backendTypeSection,
},
}
// Populate AuthContext from AuthManager if provided (from --identity flag).
if authManager != nil {
managerStackInfo := authManager.GetStackInfo()
if managerStackInfo != nil && managerStackInfo.AuthContext != nil {
configAndStacksInfo.AuthContext = managerStackInfo.AuthContext
}
}
if comp, ok := configAndStacksInfo.ComponentSection[cfg.ComponentSectionName].(string); !ok || comp == "" {
configAndStacksInfo.ComponentSection[cfg.ComponentSectionName] = componentName
}
// Stack name precedence: name (from manifest) > name_template > name_pattern > filename.
switch {
case stackManifestName != "":
stackName = stackManifestName
case atmosConfig.Stacks.NameTemplate != "":
stackName, err = ProcessTmpl(atmosConfig, "describe-stacks-name-template", atmosConfig.Stacks.NameTemplate, configAndStacksInfo.ComponentSection, false)
if err != nil {
return nil, err
}
case GetStackNamePattern(atmosConfig) != "":
context = cfg.GetContextFromVars(varsSection)
configAndStacksInfo.Context = context
stackName, err = cfg.GetContextPrefix(stackFileName, context, GetStackNamePattern(atmosConfig), stackFileName)
if err != nil {
// Fall back to filename when pattern validation fails.
log.Debug("Pattern validation failed, using filename as stack name",
logFieldStack, stackFileName, "error", err)
stackName = stackFileName
}
default:
// Default: use stack filename when no name, template, or pattern is configured.
stackName = stackFileName
}
if filterByStack != "" && filterByStack != stackFileName && filterByStack != stackName {
continue
}
if stackName == "" {
stackName = stackFileName
}
// Only create the stack entry if it doesn't exist.
if !u.MapKeyExists(finalStacksMap, stackName) {
finalStacksMap[stackName] = make(map[string]any)
}
configAndStacksInfo.ComponentSection["atmos_component"] = componentName
configAndStacksInfo.ComponentSection["atmos_stack"] = stackName
configAndStacksInfo.ComponentSection["stack"] = stackName
configAndStacksInfo.ComponentSection["atmos_stack_file"] = stackFileName
configAndStacksInfo.ComponentSection["atmos_manifest"] = stackFileName
if len(components) == 0 || u.SliceContainsString(components, componentName) || u.SliceContainsString(derivedComponents, componentName) {
if !u.MapKeyExists(finalStacksMap[stackName].(map[string]any), "components") {
finalStacksMap[stackName].(map[string]any)["components"] = make(map[string]any)
}
if !u.MapKeyExists(finalStacksMap[stackName].(map[string]any)["components"].(map[string]any), "terraform") {
finalStacksMap[stackName].(map[string]any)["components"].(map[string]any)["terraform"] = make(map[string]any)
}
if !u.MapKeyExists(finalStacksMap[stackName].(map[string]any)["components"].(map[string]any)["terraform"].(map[string]any), componentName) {
finalStacksMap[stackName].(map[string]any)["components"].(map[string]any)["terraform"].(map[string]any)[componentName] = make(map[string]any)
}
// Atmos component, stack, and stack manifest file.
configAndStacksInfo.Stack = stackName
componentSection["atmos_component"] = componentName
componentSection["atmos_stack"] = stackName
componentSection["stack"] = stackName
componentSection["atmos_stack_file"] = stackFileName
componentSection["atmos_manifest"] = stackFileName
// Terraform workspace.
workspace, err := BuildTerraformWorkspace(atmosConfig, configAndStacksInfo)
if err != nil {
return nil, err
}
componentSection["workspace"] = workspace
configAndStacksInfo.ComponentSection["workspace"] = workspace
// Add componentInfoKey with component_path.
componentInfo := buildComponentInfo(atmosConfig, componentSection, cfg.TerraformSectionName)
componentSection[componentInfoKey] = componentInfo
configAndStacksInfo.ComponentSection[componentInfoKey] = componentInfo
// Process `Go` templates.
if processTemplates {
componentSectionStr, err := atmosYaml.ConvertToYAMLPreservingDelimiters(componentSection, atmosConfig.Templates.Settings.Delimiters)
if err != nil {
return nil, err
}
var settingsSectionStruct schema.Settings
err = mapstructure.Decode(settingsSection, &settingsSectionStruct)
if err != nil {
return nil, err
}
// Restore env vars that mapstructure:"-" dropped during Decode.
if envMap := extractEnvFromRawMap(settingsSection); len(envMap) > 0 {
settingsSectionStruct.Templates.Settings.Env = envMap
}
componentSectionProcessed, err := ProcessTmplWithDatasources(
atmosConfig,
&configAndStacksInfo,
settingsSectionStruct,
"describe-stacks-all-sections",
componentSectionStr,
configAndStacksInfo.ComponentSection,
true,
)
if err != nil {
return nil, err
}
componentSectionConverted, err := u.UnmarshalYAML[schema.AtmosSectionMapType](componentSectionProcessed)
if err != nil {
if !atmosConfig.Templates.Settings.Enabled {
if strings.Contains(componentSectionStr, "{{") || strings.Contains(componentSectionStr, "}}") {
errorMessage := "the stack manifests contain Go templates, but templating is disabled in atmos.yaml in 'templates.settings.enabled'\n" +
"to enable templating, refer to https://atmos.tools/core-concepts/stacks/templates"
err = errors.Join(err, errors.New(errorMessage))
}
}
errUtils.CheckErrorPrintAndExit(err, "", "")
}
componentSection = componentSectionConverted
}
// Process YAML functions.
if processYamlFunctions {
componentSectionConverted, err := ProcessCustomYamlTags(
atmosConfig,
componentSection,
configAndStacksInfo.Stack,
skip,
&configAndStacksInfo,
)
if err != nil {
return nil, err
}
componentSection = componentSectionConverted
}
// Check if we should include empty sections.
includeEmpty := true // Default to true if `setting` is not provided.
if atmosConfig.Describe.Settings.IncludeEmpty != nil {
includeEmpty = *atmosConfig.Describe.Settings.IncludeEmpty
}
// Add sections.
for sectionName, section := range componentSection {
// Skip empty sections if includeEmpty is false.
if !includeEmpty {
if sectionMap, ok := section.(map[string]any); ok {
if len(sectionMap) == 0 {
continue
}
}
}
if len(sections) == 0 || u.SliceContainsString(sections, sectionName) {
finalStacksMap[stackName].(map[string]any)["components"].(map[string]any)["terraform"].(map[string]any)[componentName].(map[string]any)[sectionName] = section
}
}
}
}
}
}
// Helmfile.
if len(componentTypes) == 0 || u.SliceContainsString(componentTypes, cfg.HelmfileSectionName) {
if helmfileSection, ok := componentsSection[cfg.HelmfileSectionName].(map[string]any); ok {
for componentName, compSection := range helmfileSection {
componentSection, ok := compSection.(map[string]any)
if !ok {
return nil, fmt.Errorf("invalid 'components.helmfile.%s' section in the file '%s'", componentName, stackFileName)
}
if comp, ok := componentSection[cfg.ComponentSectionName].(string); !ok || comp == "" {
componentSection[cfg.ComponentSectionName] = componentName
}
// Find all derived components of the provided components and include them in the output.
derivedComponents, err := FindComponentsDerivedFromBaseComponents(stackFileName, helmfileSection, components)
if err != nil {
return nil, err
}
if varsSection, ok = componentSection[cfg.VarsSectionName].(map[string]any); !ok {
varsSection = map[string]any{}
}
if metadataSection, ok = componentSection[cfg.MetadataSectionName].(map[string]any); !ok {
metadataSection = map[string]any{}
}
if settingsSection, ok = componentSection[cfg.SettingsSectionName].(map[string]any); !ok {
settingsSection = map[string]any{}
}
if envSection, ok = componentSection[cfg.EnvSectionName].(map[string]any); !ok {
envSection = map[string]any{}
}
if authSection, ok = componentSection[cfg.AuthSectionName].(map[string]any); !ok {
authSection = map[string]any{}
}
if providersSection, ok = componentSection[cfg.ProvidersSectionName].(map[string]any); !ok {
providersSection = map[string]any{}
}
if hooksSection, ok = componentSection[cfg.HooksSectionName].(map[string]any); !ok {
hooksSection = map[string]any{}
}
if overridesSection, ok = componentSection[cfg.OverridesSectionName].(map[string]any); !ok {
overridesSection = map[string]any{}
}
if backendSection, ok = componentSection[cfg.BackendSectionName].(map[string]any); !ok {
backendSection = map[string]any{}
}
if backendTypeSection, ok = componentSection[cfg.BackendTypeSectionName].(string); !ok {
backendTypeSection = ""
}
configAndStacksInfo := schema.ConfigAndStacksInfo{
ComponentFromArg: componentName,
Stack: stackName,
StackManifestName: stackManifestName,
ComponentMetadataSection: metadataSection,
ComponentVarsSection: varsSection,
ComponentSettingsSection: settingsSection,
ComponentEnvSection: envSection,
ComponentAuthSection: authSection,
ComponentProvidersSection: providersSection,
ComponentHooksSection: hooksSection,
ComponentOverridesSection: overridesSection,
ComponentBackendSection: backendSection,
ComponentBackendType: backendTypeSection,
ComponentSection: map[string]any{
cfg.VarsSectionName: varsSection,
cfg.MetadataSectionName: metadataSection,
cfg.SettingsSectionName: settingsSection,
cfg.EnvSectionName: envSection,
cfg.AuthSectionName: authSection,
cfg.ProvidersSectionName: providersSection,
cfg.HooksSectionName: hooksSection,
cfg.OverridesSectionName: overridesSection,
cfg.BackendSectionName: backendSection,
cfg.BackendTypeSectionName: backendTypeSection,
},
}
// Populate AuthContext from AuthManager if provided (from --identity flag).
if authManager != nil {
managerStackInfo := authManager.GetStackInfo()
if managerStackInfo != nil && managerStackInfo.AuthContext != nil {
configAndStacksInfo.AuthContext = managerStackInfo.AuthContext
}
}
if comp, ok := configAndStacksInfo.ComponentSection[cfg.ComponentSectionName].(string); !ok || comp == "" {
configAndStacksInfo.ComponentSection[cfg.ComponentSectionName] = componentName
}
// Stack name precedence: name (from manifest) > name_template > name_pattern > filename.
switch {
case stackManifestName != "":
stackName = stackManifestName
case atmosConfig.Stacks.NameTemplate != "":
stackName, err = ProcessTmpl(atmosConfig, "describe-stacks-name-template", atmosConfig.Stacks.NameTemplate, configAndStacksInfo.ComponentSection, false)
if err != nil {
return nil, err
}
case GetStackNamePattern(atmosConfig) != "":
context = cfg.GetContextFromVars(varsSection)
configAndStacksInfo.Context = context
stackName, err = cfg.GetContextPrefix(stackFileName, context, GetStackNamePattern(atmosConfig), stackFileName)
if err != nil {
// Fall back to filename when pattern validation fails.
log.Debug("Pattern validation failed, using filename as stack name",
logFieldStack, stackFileName, "error", err)
stackName = stackFileName
}
default:
// Default: use stack filename when no name, template, or pattern is configured.
stackName = stackFileName
}
if filterByStack != "" && filterByStack != stackFileName && filterByStack != stackName {
continue
}
if stackName == "" {
stackName = stackFileName
}
// Only create the stack entry if it doesn't exist.
if !u.MapKeyExists(finalStacksMap, stackName) {
finalStacksMap[stackName] = make(map[string]any)
}
configAndStacksInfo.Stack = stackName
configAndStacksInfo.ComponentSection["atmos_component"] = componentName
configAndStacksInfo.ComponentSection["atmos_stack"] = stackName
configAndStacksInfo.ComponentSection["stack"] = stackName
configAndStacksInfo.ComponentSection["atmos_stack_file"] = stackFileName
configAndStacksInfo.ComponentSection["atmos_manifest"] = stackFileName
if len(components) == 0 || u.SliceContainsString(components, componentName) || u.SliceContainsString(derivedComponents, componentName) {
if !u.MapKeyExists(finalStacksMap[stackName].(map[string]any), "components") {
finalStacksMap[stackName].(map[string]any)["components"] = make(map[string]any)
}
if !u.MapKeyExists(finalStacksMap[stackName].(map[string]any)["components"].(map[string]any), "helmfile") {
finalStacksMap[stackName].(map[string]any)["components"].(map[string]any)["helmfile"] = make(map[string]any)
}
if !u.MapKeyExists(finalStacksMap[stackName].(map[string]any)["components"].(map[string]any)["helmfile"].(map[string]any), componentName) {
finalStacksMap[stackName].(map[string]any)["components"].(map[string]any)["helmfile"].(map[string]any)[componentName] = make(map[string]any)
}
// Atmos component, stack, and stack manifest file.
componentSection["atmos_component"] = componentName
componentSection["atmos_stack"] = stackName
componentSection["stack"] = stackName
componentSection["atmos_stack_file"] = stackFileName
componentSection["atmos_manifest"] = stackFileName
// Add componentInfoKey with component_path.
componentInfo := buildComponentInfo(atmosConfig, componentSection, cfg.HelmfileSectionName)
componentSection[componentInfoKey] = componentInfo
configAndStacksInfo.ComponentSection[componentInfoKey] = componentInfo
// Process `Go` templates.
if processTemplates {
componentSectionStr, err := atmosYaml.ConvertToYAMLPreservingDelimiters(componentSection, atmosConfig.Templates.Settings.Delimiters)
if err != nil {
return nil, err
}
var settingsSectionStruct schema.Settings
err = mapstructure.Decode(settingsSection, &settingsSectionStruct)
if err != nil {
return nil, err
}
// Restore env vars that mapstructure:"-" dropped during Decode.
if envMap := extractEnvFromRawMap(settingsSection); len(envMap) > 0 {
settingsSectionStruct.Templates.Settings.Env = envMap
}
componentSectionProcessed, err := ProcessTmplWithDatasources(
atmosConfig,
&configAndStacksInfo,
settingsSectionStruct,
"templates-describe-stacks-all-atmos-sections",
componentSectionStr,
configAndStacksInfo.ComponentSection,
true,
)
if err != nil {
return nil, err
}
componentSectionConverted, err := u.UnmarshalYAML[schema.AtmosSectionMapType](componentSectionProcessed)
if err != nil {
if !atmosConfig.Templates.Settings.Enabled {
if strings.Contains(componentSectionStr, "{{") || strings.Contains(componentSectionStr, "}}") {
errorMessage := "the stack manifests contain Go templates, but templating is disabled in atmos.yaml in 'templates.settings.enabled'\n" +
"to enable templating, refer to https://atmos.tools/core-concepts/stacks/templates"
err = errors.Join(err, errors.New(errorMessage))
}
}
errUtils.CheckErrorPrintAndExit(err, "", "")
}
componentSection = componentSectionConverted
}
// Process YAML functions.
if processYamlFunctions {
componentSectionConverted, err := ProcessCustomYamlTags(
atmosConfig,
componentSection,
configAndStacksInfo.Stack,
skip,
&configAndStacksInfo,
)
if err != nil {
return nil, err
}
componentSection = componentSectionConverted
}
// Add sections.
for sectionName, section := range componentSection {
if len(sections) == 0 || u.SliceContainsString(sections, sectionName) {
finalStacksMap[stackName].(map[string]any)["components"].(map[string]any)["helmfile"].(map[string]any)[componentName].(map[string]any)[sectionName] = section
}
}
}
}
}
}
// Packer.
if len(componentTypes) == 0 || u.SliceContainsString(componentTypes, cfg.PackerSectionName) {
if packerSection, ok := componentsSection[cfg.PackerSectionName].(map[string]any); ok {
for componentName, compSection := range packerSection {
componentSection, ok := compSection.(map[string]any)
if !ok {
return nil, fmt.Errorf("invalid 'components.packer.%s' section in the file '%s'", componentName, stackFileName)
}
if comp, ok := componentSection[cfg.ComponentSectionName].(string); !ok || comp == "" {
componentSection[cfg.ComponentSectionName] = componentName
}
// Find all derived components of the provided components and include them in the output.
derivedComponents, err := FindComponentsDerivedFromBaseComponents(stackFileName, packerSection, components)
if err != nil {
return nil, err
}
if varsSection, ok = componentSection[cfg.VarsSectionName].(map[string]any); !ok {
varsSection = map[string]any{}
}
if metadataSection, ok = componentSection[cfg.MetadataSectionName].(map[string]any); !ok {
metadataSection = map[string]any{}
}
if settingsSection, ok = componentSection[cfg.SettingsSectionName].(map[string]any); !ok {
settingsSection = map[string]any{}
}
if envSection, ok = componentSection[cfg.EnvSectionName].(map[string]any); !ok {
envSection = map[string]any{}
}
if authSection, ok = componentSection[cfg.AuthSectionName].(map[string]any); !ok {
authSection = map[string]any{}
}
if providersSection, ok = componentSection[cfg.ProvidersSectionName].(map[string]any); !ok {
providersSection = map[string]any{}
}
if hooksSection, ok = componentSection[cfg.HooksSectionName].(map[string]any); !ok {
hooksSection = map[string]any{}
}
if overridesSection, ok = componentSection[cfg.OverridesSectionName].(map[string]any); !ok {
overridesSection = map[string]any{}
}
if backendSection, ok = componentSection[cfg.BackendSectionName].(map[string]any); !ok {
backendSection = map[string]any{}
}
if backendTypeSection, ok = componentSection[cfg.BackendTypeSectionName].(string); !ok {
backendTypeSection = ""
}
configAndStacksInfo := schema.ConfigAndStacksInfo{
ComponentFromArg: componentName,
Stack: stackName,
StackManifestName: stackManifestName,
ComponentMetadataSection: metadataSection,
ComponentVarsSection: varsSection,
ComponentSettingsSection: settingsSection,
ComponentEnvSection: envSection,
ComponentAuthSection: authSection,
ComponentProvidersSection: providersSection,
ComponentHooksSection: hooksSection,
ComponentOverridesSection: overridesSection,
ComponentBackendSection: backendSection,
ComponentBackendType: backendTypeSection,
ComponentSection: map[string]any{
cfg.VarsSectionName: varsSection,
cfg.MetadataSectionName: metadataSection,
cfg.SettingsSectionName: settingsSection,
cfg.EnvSectionName: envSection,
cfg.AuthSectionName: authSection,
cfg.ProvidersSectionName: providersSection,
cfg.HooksSectionName: hooksSection,
cfg.OverridesSectionName: overridesSection,
cfg.BackendSectionName: backendSection,
cfg.BackendTypeSectionName: backendTypeSection,
},
}
// Populate AuthContext from AuthManager if provided (from --identity flag).
if authManager != nil {
managerStackInfo := authManager.GetStackInfo()
if managerStackInfo != nil && managerStackInfo.AuthContext != nil {
configAndStacksInfo.AuthContext = managerStackInfo.AuthContext
}
}
if comp, ok := configAndStacksInfo.ComponentSection[cfg.ComponentSectionName].(string); !ok || comp == "" {
configAndStacksInfo.ComponentSection[cfg.ComponentSectionName] = componentName
}
// Stack name precedence: name (from manifest) > name_template > name_pattern > filename.
switch {
case stackManifestName != "":
stackName = stackManifestName
case atmosConfig.Stacks.NameTemplate != "":
stackName, err = ProcessTmpl(atmosConfig, "describe-stacks-name-template", atmosConfig.Stacks.NameTemplate, configAndStacksInfo.ComponentSection, false)
if err != nil {
return nil, err
}
case GetStackNamePattern(atmosConfig) != "":
context = cfg.GetContextFromVars(varsSection)
configAndStacksInfo.Context = context
stackName, err = cfg.GetContextPrefix(stackFileName, context, GetStackNamePattern(atmosConfig), stackFileName)
if err != nil {
// Fall back to filename when pattern validation fails.
log.Debug("Pattern validation failed, using filename as stack name",
logFieldStack, stackFileName, "error", err)
stackName = stackFileName
}
default:
// Default: use stack filename when no name, template, or pattern is configured.
stackName = stackFileName
}
if filterByStack != "" && filterByStack != stackFileName && filterByStack != stackName {
continue
}
if stackName == "" {
stackName = stackFileName
}
// Only create the stack entry if it doesn't exist.
if !u.MapKeyExists(finalStacksMap, stackName) {
finalStacksMap[stackName] = make(map[string]any)
}
configAndStacksInfo.Stack = stackName
configAndStacksInfo.ComponentSection["atmos_component"] = componentName
configAndStacksInfo.ComponentSection["atmos_stack"] = stackName
configAndStacksInfo.ComponentSection["stack"] = stackName
configAndStacksInfo.ComponentSection["atmos_stack_file"] = stackFileName
configAndStacksInfo.ComponentSection["atmos_manifest"] = stackFileName
if len(components) == 0 || u.SliceContainsString(components, componentName) || u.SliceContainsString(derivedComponents, componentName) {
if !u.MapKeyExists(finalStacksMap[stackName].(map[string]any), cfg.ComponentsSectionName) {
finalStacksMap[stackName].(map[string]any)[cfg.ComponentsSectionName] = make(map[string]any)
}
if !u.MapKeyExists(finalStacksMap[stackName].(map[string]any)[cfg.ComponentsSectionName].(map[string]any), cfg.PackerSectionName) {
finalStacksMap[stackName].(map[string]any)[cfg.ComponentsSectionName].(map[string]any)[cfg.PackerSectionName] = make(map[string]any)
}
if !u.MapKeyExists(finalStacksMap[stackName].(map[string]any)[cfg.ComponentsSectionName].(map[string]any)[cfg.PackerSectionName].(map[string]any), componentName) {
finalStacksMap[stackName].(map[string]any)[cfg.ComponentsSectionName].(map[string]any)[cfg.PackerSectionName].(map[string]any)[componentName] = make(map[string]any)
}
// Atmos component, stack, and stack manifest file.
componentSection["atmos_component"] = componentName
componentSection["atmos_stack"] = stackName
componentSection["stack"] = stackName
componentSection["atmos_stack_file"] = stackFileName
componentSection["atmos_manifest"] = stackFileName
// Add componentInfoKey with component_path.
componentInfo := buildComponentInfo(atmosConfig, componentSection, cfg.PackerSectionName)
componentSection[componentInfoKey] = componentInfo
configAndStacksInfo.ComponentSection[componentInfoKey] = componentInfo
// Process `Go` templates.
if processTemplates {
componentSectionStr, err := atmosYaml.ConvertToYAMLPreservingDelimiters(componentSection, atmosConfig.Templates.Settings.Delimiters)
if err != nil {
return nil, err
}
var settingsSectionStruct schema.Settings
err = mapstructure.Decode(settingsSection, &settingsSectionStruct)
if err != nil {
return nil, err
}
// Restore env vars that mapstructure:"-" dropped during Decode.
if envMap := extractEnvFromRawMap(settingsSection); len(envMap) > 0 {
settingsSectionStruct.Templates.Settings.Env = envMap
}
componentSectionProcessed, err := ProcessTmplWithDatasources(
atmosConfig,
&configAndStacksInfo,
settingsSectionStruct,
"templates-describe-stacks-all-atmos-sections",
componentSectionStr,
configAndStacksInfo.ComponentSection,
true,
)
if err != nil {
return nil, err
}
componentSectionConverted, err := u.UnmarshalYAML[schema.AtmosSectionMapType](componentSectionProcessed)
if err != nil {
if !atmosConfig.Templates.Settings.Enabled {
if strings.Contains(componentSectionStr, "{{") || strings.Contains(componentSectionStr, "}}") {
errorMessage := "the stack manifests contain Go templates, but templating is disabled in atmos.yaml in 'templates.settings.enabled'\n" +
"to enable templating, refer to https://atmos.tools/core-concepts/stacks/templates"
err = errors.Join(err, errors.New(errorMessage))
}
}
errUtils.CheckErrorPrintAndExit(err, "", "")
}
componentSection = componentSectionConverted
}
// Process YAML functions.
if processYamlFunctions {
componentSectionConverted, err := ProcessCustomYamlTags(
atmosConfig,
componentSection,
configAndStacksInfo.Stack,
skip,
&configAndStacksInfo,
)
if err != nil {
return nil, err
}