-
-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathstack_processor_merge.go
More file actions
742 lines (681 loc) · 25.3 KB
/
Copy pathstack_processor_merge.go
File metadata and controls
742 lines (681 loc) · 25.3 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
package exec
import (
"fmt"
errUtils "github.com/cloudposse/atmos/errors"
cfg "github.com/cloudposse/atmos/pkg/config"
m "github.com/cloudposse/atmos/pkg/merge"
"github.com/cloudposse/atmos/pkg/perf"
"github.com/cloudposse/atmos/pkg/schema"
"github.com/cloudposse/atmos/pkg/secrets"
)
// tagSecretsScopes stamps the position-derived scope onto each secrets layer before merging: the
// stack-level (global) layer is `stack`-scoped, every component-level layer (base, component,
// overrides) is `instance`-scoped. It returns the layers in merge order (lowest→highest priority).
// A declaration whose explicit `scope` conflicts with its position is rejected as invalid secrets.
func tagSecretsScopes(global, base, component, overrides map[string]any) ([]map[string]any, error) {
defer perf.Track(nil, "exec.tagSecretsScopes")()
layers := []struct {
section map[string]any
scope secrets.Scope
}{
{global, secrets.ScopeStack},
{base, secrets.ScopeInstance},
{component, secrets.ScopeInstance},
{overrides, secrets.ScopeInstance},
}
out := make([]map[string]any, 0, len(layers))
for _, l := range layers {
tagged, err := secrets.TagScope(l.section, l.scope)
if err != nil {
return nil, fmt.Errorf("%w: %w", errUtils.ErrInvalidComponentSecrets, err)
}
out = append(out, tagged)
}
return out, nil
}
// effectiveAtmosConfig returns an *AtmosConfiguration suitable for merging this
// component's sections. If any settings layer overrides list_merge_strategy, a
// shallow copy is returned with the new value; otherwise base is returned as-is
// (no allocation). Layers are applied in order — later entries win — matching
// the same precedence used when merging the settings section itself.
//
// Only Settings.ListMergeStrategy is written on the copy; all other fields
// remain shared with base. Do not mutate any other field on the returned copy
// without converting this to a deep copy first.
func effectiveAtmosConfig(base *schema.AtmosConfiguration, settingsLayers ...map[string]any) *schema.AtmosConfiguration {
strategy := base.Settings.ListMergeStrategy
for _, layer := range settingsLayers {
if v, ok := layer["list_merge_strategy"].(string); ok && v != "" {
strategy = v
}
}
if strategy == base.Settings.ListMergeStrategy {
return base
}
cfgCopy := *base
cfgCopy.Settings.ListMergeStrategy = strategy
return &cfgCopy
}
// mergeComponentConfigurations merges component configurations (vars, settings, env, etc.).
//
//nolint:gocognit,nestif,revive,cyclop,funlen // Complex configuration merging logic with multiple component types.
func mergeComponentConfigurations(atmosConfig *schema.AtmosConfiguration, opts *ComponentProcessorOptions, result *ComponentProcessorResult) (map[string]any, error) {
defer perf.Track(atmosConfig, "exec.mergeComponentConfigurations")()
// Resolve the effective list_merge_strategy for this component before any merge.
// Component-level settings (at any inheritance level) override the global atmos.yaml
// setting, so individual components can opt into a different list merge behavior
// without changing the global configuration. The layers are applied lowest-to-highest
// priority: global settings → base component settings → component settings → overrides.
// Using a local config copy avoids mutating the shared atmosConfig.
mergeConfig := effectiveAtmosConfig(
atmosConfig,
opts.GlobalSettings,
result.BaseComponentSettings,
result.ComponentSettings,
result.ComponentOverridesSettings,
)
// Merge vars using deferred merge to handle YAML functions.
finalComponentVars, varsCtx, err := m.MergeWithDeferred(
mergeConfig,
[]map[string]any{
opts.GlobalVars,
result.BaseComponentVars,
result.ComponentVars,
result.ComponentOverridesVars,
},
)
if err != nil {
return nil, err
}
// Apply deferred merges for vars (without YAML processing - already done earlier).
if err := m.ApplyDeferredMerges(varsCtx, finalComponentVars, mergeConfig, nil); err != nil {
return nil, err
}
// Merge settings using deferred merge to handle YAML functions.
finalComponentSettings, settingsCtx, err := m.MergeWithDeferred(
mergeConfig,
[]map[string]any{
opts.GlobalSettings,
result.BaseComponentSettings,
result.ComponentSettings,
result.ComponentOverridesSettings,
},
)
if err != nil {
return nil, err
}
// Apply deferred merges for settings (without YAML processing - already done earlier).
if err := m.ApplyDeferredMerges(settingsCtx, finalComponentSettings, mergeConfig, nil); err != nil {
return nil, err
}
// Merge env using deferred merge to handle YAML functions.
finalComponentEnv, envCtx, err := m.MergeWithDeferred(
mergeConfig,
[]map[string]any{
opts.GlobalEnv,
result.BaseComponentEnv,
result.ComponentEnv,
result.ComponentOverridesEnv,
},
)
if err != nil {
return nil, err
}
// Apply deferred merges for env (without YAML processing - already done earlier).
if err := m.ApplyDeferredMerges(envCtx, finalComponentEnv, mergeConfig, nil); err != nil {
return nil, err
}
// Merge auth using deferred merge to handle YAML functions.
finalComponentAuth, authCtx, err := m.MergeWithDeferred(
mergeConfig,
[]map[string]any{
opts.GlobalAuth,
result.BaseComponentAuth,
result.ComponentAuth,
result.ComponentOverridesAuth,
},
)
if err != nil {
return nil, err
}
// Apply deferred merges for auth (without YAML processing - already done earlier).
if err := m.ApplyDeferredMerges(authCtx, finalComponentAuth, mergeConfig, nil); err != nil {
return nil, err
}
// Terraform-specific: merge providers using deferred merge.
var finalComponentProviders map[string]any
if opts.ComponentType == cfg.TerraformComponentType {
var providersCtx *m.DeferredMergeContext
finalComponentProviders, providersCtx, err = m.MergeWithDeferred(
mergeConfig,
[]map[string]any{
opts.TerraformProviders,
result.BaseComponentProviders,
result.ComponentProviders,
result.ComponentOverridesProviders,
},
)
if err != nil {
return nil, err
}
// Apply deferred merges for providers (without YAML processing - already done earlier).
if err := m.ApplyDeferredMerges(providersCtx, finalComponentProviders, mergeConfig, nil); err != nil {
return nil, err
}
}
// Terraform-specific: merge required_providers using deferred merge (DEV-3124).
var finalComponentRequiredProviders map[string]any
if opts.ComponentType == cfg.TerraformComponentType {
var requiredProvidersCtx *m.DeferredMergeContext
finalComponentRequiredProviders, requiredProvidersCtx, err = m.MergeWithDeferred(
mergeConfig,
[]map[string]any{
opts.TerraformRequiredProviders,
result.BaseComponentRequiredProviders,
result.ComponentRequiredProviders,
result.ComponentOverridesRequiredProviders,
},
)
if err != nil {
return nil, err
}
// Apply deferred merges for required_providers (without YAML processing - already done earlier).
if err := m.ApplyDeferredMerges(requiredProvidersCtx, finalComponentRequiredProviders, mergeConfig, nil); err != nil {
return nil, err
}
}
// Terraform-specific: resolve required_version (DEV-3124).
// Uses the same precedence as command: global -> base component -> component -> overrides.
var finalComponentRequiredVersion string
if opts.ComponentType == cfg.TerraformComponentType {
if opts.TerraformRequiredVersion != "" {
finalComponentRequiredVersion = opts.TerraformRequiredVersion
}
if result.BaseComponentRequiredVersion != "" {
finalComponentRequiredVersion = result.BaseComponentRequiredVersion
}
if result.ComponentRequiredVersion != "" {
finalComponentRequiredVersion = result.ComponentRequiredVersion
}
if result.ComponentOverridesRequiredVersion != "" {
finalComponentRequiredVersion = result.ComponentOverridesRequiredVersion
}
}
// Merge hooks using deferred merge for component types with lifecycle hooks.
var finalComponentHooks map[string]any
if supportsComponentHooks(opts.ComponentType) {
var hooksCtx *m.DeferredMergeContext
finalComponentHooks, hooksCtx, err = m.MergeWithDeferred(
mergeConfig,
[]map[string]any{
opts.GlobalAndTerraformHooks,
result.BaseComponentHooks,
result.ComponentHooks,
result.ComponentOverridesHooks,
},
)
if err != nil {
return nil, err
}
// Apply deferred merges for hooks (without YAML processing - already done earlier).
if err := m.ApplyDeferredMerges(hooksCtx, finalComponentHooks, mergeConfig, nil); err != nil {
return nil, err
}
}
// Terraform-specific: merge test configuration.
var finalComponentTest map[string]any
var finalComponentMocks map[string]any
if opts.ComponentType == cfg.TerraformComponentType {
var testCtx *m.DeferredMergeContext
finalComponentTest, testCtx, err = m.MergeWithDeferred(
mergeConfig,
[]map[string]any{
result.BaseComponentTest,
result.ComponentTest,
},
)
if err != nil {
return nil, err
}
if err := m.ApplyDeferredMerges(testCtx, finalComponentTest, mergeConfig, nil); err != nil {
return nil, err
}
finalComponentMocks, err = m.Merge(mergeConfig, []map[string]any{
result.BaseComponentMocks,
result.ComponentMocks,
})
if err != nil {
return nil, err
}
}
// Merge secrets declarations (global stack → base → component → overrides). Available for
// all component types; inherits through the stack hierarchy like other sections. The
// stack-level (global) `secrets:` block lets providers/declarations be defined once per stack.
//
// Scope is derived from position and stamped onto each declaration BEFORE the merge: the
// global (stack-level) layer is `stack`-scoped, every component-level layer is `instance`-scoped.
// "Most-specific wins" then resolves overrides — a component re-declaring a stack secret pulls it
// to instance scope — and enforces the one-way rule, with no merge-engine changes.
var finalComponentSecrets map[string]any
if len(opts.GlobalSecrets) > 0 || len(result.BaseComponentSecrets) > 0 || len(result.ComponentSecrets) > 0 || len(result.ComponentOverridesSecrets) > 0 {
scopedSecrets, err := tagSecretsScopes(opts.GlobalSecrets, result.BaseComponentSecrets, result.ComponentSecrets, result.ComponentOverridesSecrets)
if err != nil {
return nil, err
}
finalComponentSecrets, err = m.Merge(mergeConfig, scopedSecrets)
if err != nil {
return nil, err
}
}
// Merge generate section using deferred merge.
// Merge order (lowest to highest priority):
// 1. Global + component-type-level generate
// 2. Base component generate (from metadata.inherits)
// 3. Component generate (component-specific generate section)
// 4. Component overrides generate (from overrides section)
var finalComponentGenerate map[string]any
if supportsGenerate(opts.ComponentType) {
var generateCtx *m.DeferredMergeContext
finalComponentGenerate, generateCtx, err = m.MergeWithDeferred(
mergeConfig,
[]map[string]any{
opts.GlobalAndTerraformGenerate,
result.BaseComponentGenerate,
result.ComponentGenerate,
result.ComponentOverridesGenerate,
},
)
if err != nil {
return nil, err
}
// Apply deferred merges for generate (without YAML processing - already done earlier).
if err := m.ApplyDeferredMerges(generateCtx, finalComponentGenerate, mergeConfig, nil); err != nil {
return nil, err
}
}
// Resolve the final executable command.
// Check for the binary in the following order:
// - `components.<type>.command` section in `atmos.yaml` CLI config file.
// - global `<type>.command` section.
// - base component(s) `command` section.
// - component `command` section.
// - `overrides.command` section.
finalComponentCommand := opts.ComponentType
if opts.ComponentType == cfg.TerraformComponentType && opts.AtmosConfig.Components.Terraform.Command != "" {
finalComponentCommand = opts.AtmosConfig.Components.Terraform.Command
}
if opts.ComponentType == cfg.HelmfileComponentType && opts.AtmosConfig.Components.Helmfile.Command != "" {
finalComponentCommand = opts.AtmosConfig.Components.Helmfile.Command
}
if opts.GlobalCommand != "" {
finalComponentCommand = opts.GlobalCommand
}
if result.BaseComponentCommand != "" {
finalComponentCommand = result.BaseComponentCommand
}
if result.ComponentCommand != "" {
finalComponentCommand = result.ComponentCommand
}
if result.ComponentOverridesCommand != "" {
finalComponentCommand = result.ComponentOverridesCommand
}
// Precedence (lowest to highest): stack-global Kubernetes defaults → base
// component → component instance.
finalComponentProvider := opts.GlobalKubernetesProvider
if result.BaseComponentProvider != "" {
finalComponentProvider = result.BaseComponentProvider
}
if result.ComponentProvider != "" {
finalComponentProvider = result.ComponentProvider
}
finalComponentPaths, err := mergeComponentAnySection(
mergeConfig,
cfg.PathsSectionName,
opts.GlobalKubernetesPaths,
result.BaseComponentPaths,
result.ComponentPaths,
)
if err != nil {
return nil, err
}
finalComponentManifests, err := mergeComponentAnySection(
mergeConfig,
cfg.ManifestsSectionName,
opts.GlobalKubernetesManifests,
result.BaseComponentManifests,
result.ComponentManifests,
)
if err != nil {
return nil, err
}
finalComponentValidate, err := mergeComponentAnySection(
mergeConfig,
cfg.ValidateSectionName,
opts.GlobalKubernetesValidate,
result.BaseComponentValidate,
result.ComponentValidate,
)
if err != nil {
return nil, err
}
var finalComponentRender map[string]any
if opts.ComponentType == cfg.KubernetesComponentType {
finalComponentRender, err = m.Merge(
mergeConfig,
[]map[string]any{
opts.GlobalKubernetesRender,
result.BaseComponentRender,
result.ComponentRender,
},
)
if err != nil {
return nil, err
}
}
// Process settings integrations.
finalSettings, err := processSettingsIntegrationsGithub(mergeConfig, finalComponentSettings)
if err != nil {
return nil, err
}
// Merge metadata (global + base component + component metadata).
// Priority (lowest to highest): global (stack-wide) → metadata.inherits base chain → component instance.
// Excluded from inheritance: 'inherits' and 'type' (already excluded during collection).
finalComponentMetadata := result.ComponentMetadata
metadataInheritEnabled := atmosConfig.Stacks.Inherit.IsMetadataInheritanceEnabled() && len(result.BaseComponentMetadata) > 0
if len(opts.GlobalMetadata) > 0 || metadataInheritEnabled {
layers := []map[string]any{opts.GlobalMetadata}
if metadataInheritEnabled {
layers = append(layers, result.BaseComponentMetadata)
}
layers = append(layers, result.ComponentMetadata)
finalComponentMetadata, err = m.Merge(mergeConfig, layers)
if err != nil {
return nil, err
}
}
// Merge dependencies (global + base component + component dependencies).
// Priority (lowest to highest): global/component-type → base component → component instance.
var finalComponentDependencies map[string]any
if len(opts.GlobalDependencies) > 0 || len(result.BaseComponentDependencies) > 0 || len(result.ComponentDependencies) > 0 {
finalComponentDependencies, err = m.Merge(
mergeConfig,
[]map[string]any{
opts.GlobalDependencies,
result.BaseComponentDependencies,
result.ComponentDependencies,
},
)
if err != nil {
return nil, err
}
}
// Merge locals (base component locals + component locals).
// Component locals take precedence over base component locals.
// Note: Locals are used for template processing, not passed to terraform/helmfile.
var finalComponentLocals map[string]any
if len(result.BaseComponentLocals) > 0 || len(result.ComponentLocals) > 0 {
finalComponentLocals, err = m.Merge(
mergeConfig,
[]map[string]any{
result.BaseComponentLocals,
result.ComponentLocals,
},
)
if err != nil {
return nil, err
}
}
// Merge retry config (base → component → overrides).
// Deep-merge handles top-level scalars (max_attempts, backoff_strategy, ...).
// The list-valued `conditions:` field follows the project's configured
// `settings.list_merge_strategy` (default: replace), so by default a concrete
// component's conditions list replaces the inherited one rather than extending it.
// Users who want additive conditions can opt in by setting list_merge_strategy: append.
var finalComponentRetry map[string]any
if len(result.BaseComponentRetry) > 0 || len(result.ComponentRetry) > 0 || len(result.ComponentOverridesRetry) > 0 {
finalComponentRetry, err = m.Merge(
atmosConfig,
[]map[string]any{
result.BaseComponentRetry,
result.ComponentRetry,
result.ComponentOverridesRetry,
},
)
if err != nil {
return nil, err
}
}
// Build final component map.
comp := map[string]any{
cfg.VarsSectionName: finalComponentVars,
cfg.SettingsSectionName: finalSettings,
cfg.EnvSectionName: finalComponentEnv,
cfg.AuthSectionName: finalComponentAuth,
cfg.CommandSectionName: finalComponentCommand,
cfg.InheritanceSectionName: result.ComponentInheritanceChain,
cfg.MetadataSectionName: finalComponentMetadata,
cfg.OverridesSectionName: result.ComponentOverrides,
}
// Add hooks for every component type that supports lifecycle hooks — kept
// here in one place (rather than duplicated per type-specific block below)
// so a new hooks-capable component type only needs to be added to
// supportsComponentHooks.
if supportsComponentHooks(opts.ComponentType) {
comp[cfg.HooksSectionName] = finalComponentHooks
}
// Add dependencies if present.
if len(finalComponentDependencies) > 0 {
comp[cfg.DependenciesSectionName] = finalComponentDependencies
}
// Add locals if present (for template processing, not passed to terraform/helmfile).
if len(finalComponentLocals) > 0 {
comp[cfg.LocalsSectionName] = finalComponentLocals
}
// Add retry config if present.
if len(finalComponentRetry) > 0 {
comp[cfg.RetrySectionName] = finalComponentRetry
}
// Add secrets declarations if present (all component types).
if len(finalComponentSecrets) > 0 {
comp[cfg.SecretsSectionName] = finalComponentSecrets
}
// Terraform-specific: process backends and add Terraform-specific fields.
if opts.ComponentType == cfg.TerraformComponentType {
// Process backend configuration.
finalComponentBackendType, finalComponentBackend, err := processTerraformBackend(
&terraformBackendConfig{
atmosConfig: atmosConfig,
component: opts.Component,
baseComponentName: result.BaseComponentName,
componentMetadata: finalComponentMetadata,
globalBackendType: opts.GlobalBackendType,
globalBackendSection: opts.GlobalBackendSection,
baseComponentBackendType: result.BaseComponentBackendType,
baseComponentBackendSection: result.BaseComponentBackendSection,
componentBackendType: result.ComponentBackendType,
componentBackendSection: result.ComponentBackendSection,
},
)
if err != nil {
return nil, err
}
// Process remote state backend configuration.
finalComponentRemoteStateBackendType, finalComponentRemoteStateBackend, err := processTerraformRemoteStateBackend(
&remoteStateBackendConfig{
atmosConfig: atmosConfig,
component: opts.Component,
finalComponentBackendType: finalComponentBackendType,
finalComponentBackendSection: map[string]any{finalComponentBackendType: finalComponentBackend},
globalRemoteStateBackendType: opts.GlobalRemoteStateBackendType,
globalRemoteStateBackendSection: opts.GlobalRemoteStateBackendSection,
baseComponentRemoteStateBackendType: result.BaseComponentRemoteStateBackendType,
baseComponentRemoteStateBackendSection: result.BaseComponentRemoteStateBackendSection,
componentRemoteStateBackendType: result.ComponentRemoteStateBackendType,
componentRemoteStateBackendSection: result.ComponentRemoteStateBackendSection,
},
)
if err != nil {
return nil, err
}
// Process auth configuration.
mergedAuth, err := processAuthConfig(mergeConfig, opts.AtmosGlobalAuthMap, finalComponentAuth)
if err != nil {
return nil, err
}
// Handle abstract components: remove spacelift workspace_enabled setting.
componentIsAbstract := false
if componentType, componentTypeAttributeExists := result.ComponentMetadata["type"].(string); componentTypeAttributeExists {
if componentType == cfg.AbstractSectionName {
componentIsAbstract = true
}
}
if componentIsAbstract {
if i, ok := finalSettings["spacelift"]; ok {
spaceliftSettings, ok := i.(map[string]any)
if !ok {
return nil, fmt.Errorf("%w: 'components.%s.%s.settings.spacelift'", errUtils.ErrInvalidSpaceLiftSettings, opts.ComponentType, opts.Component)
}
delete(spaceliftSettings, "workspace_enabled")
}
}
// Add Terraform-specific fields to component map.
comp[cfg.ProvidersSectionName] = finalComponentProviders
comp[cfg.RequiredProvidersSectionName] = finalComponentRequiredProviders
comp[cfg.RequiredVersionSectionName] = finalComponentRequiredVersion
if len(finalComponentTest) > 0 {
comp[cfg.TestSectionName] = finalComponentTest
}
if len(finalComponentMocks) > 0 {
comp[cfg.MocksSectionName] = finalComponentMocks
}
comp[cfg.GenerateSectionName] = finalComponentGenerate
comp[cfg.BackendTypeSectionName] = finalComponentBackendType
comp[cfg.BackendSectionName] = finalComponentBackend
comp[cfg.RemoteStateBackendTypeSectionName] = finalComponentRemoteStateBackendType
comp[cfg.RemoteStateBackendSectionName] = finalComponentRemoteStateBackend
comp[cfg.AuthSectionName] = mergedAuth
}
if opts.ComponentType == cfg.KubernetesComponentType {
if finalComponentProvider != "" {
comp[cfg.ProviderSectionName] = finalComponentProvider
}
if finalComponentPaths != nil {
comp[cfg.PathsSectionName] = finalComponentPaths
}
if finalComponentManifests != nil {
comp[cfg.ManifestsSectionName] = finalComponentManifests
}
if len(finalComponentRender) > 0 {
comp[cfg.RenderSectionName] = finalComponentRender
}
if finalComponentValidate != nil {
comp[cfg.ValidateSectionName] = finalComponentValidate
}
comp[cfg.GenerateSectionName] = finalComponentGenerate
}
if opts.ComponentType == cfg.HelmComponentType {
finalComponentHelm, err := m.Merge(
mergeConfig,
[]map[string]any{
result.BaseComponentHelm,
result.ComponentHelm,
},
)
if err != nil {
return nil, err
}
for key, value := range finalComponentHelm {
comp[key] = value
}
comp[cfg.GenerateSectionName] = finalComponentGenerate
}
// Merge the Helm CLI plugins list (helm and helmfile components).
// Base-component plugins (e.g. from an abstract/catalog component) are merged
// with the concrete component's plugins; the configured list_merge_strategy
// (default: replace) governs how the lists combine.
if supportsPlugins(opts.ComponentType) {
finalComponentPlugins, err := mergeComponentAnySection(
mergeConfig,
cfg.PluginsSectionName,
result.BaseComponentPlugins,
result.ComponentPlugins,
)
if err != nil {
return nil, err
}
if finalComponentPlugins != nil {
comp[cfg.PluginsSectionName] = finalComponentPlugins
}
}
// Process source and provision configuration.
if supportsSourceProvision(opts.ComponentType) {
finalComponentSource, err := m.Merge(
mergeConfig,
[]map[string]any{
opts.GlobalSourceSection,
result.BaseComponentSourceSection,
result.ComponentSourceSection,
},
)
if err != nil {
return nil, err
}
comp[cfg.SourceSectionName] = finalComponentSource
// Merge provision from global, base component, and component levels.
// Priority (lowest to highest): global → base component → component.
finalComponentProvision, err := m.Merge(
mergeConfig,
[]map[string]any{
opts.GlobalProvisionSection,
result.BaseComponentProvisionSection,
result.ComponentProvision,
},
)
if err != nil {
return nil, err
}
comp[cfg.ProvisionSectionName] = finalComponentProvision
}
// Add base component name if present.
if result.BaseComponentName != "" {
comp[cfg.ComponentSectionName] = result.BaseComponentName
}
return comp, nil
}
func mergeComponentAnySection(atmosConfig *schema.AtmosConfiguration, key string, values ...any) (any, error) {
sections := make([]map[string]any, 0, len(values))
for _, value := range values {
if value == nil {
continue
}
sections = append(sections, map[string]any{key: value})
}
if len(sections) == 0 {
return nil, nil
}
merged, err := m.Merge(atmosConfig, sections)
if err != nil {
return nil, err
}
return merged[key], nil
}
// processAuthConfig merges global and component-level auth configurations.
func processAuthConfig(atmosConfig *schema.AtmosConfiguration, globalAuthConfig map[string]any, authConfig map[string]any) (map[string]any, error) {
// Use the pre-converted global auth config to avoid race conditions.
// The globalAuthConfig parameter is pre-converted from atmosConfig.Auth before parallel processing starts.
mergedAuthConfig, mergeCtx, err := m.MergeWithDeferred(
atmosConfig,
[]map[string]any{
globalAuthConfig,
authConfig,
},
)
if err != nil {
return nil, fmt.Errorf("%w: merge auth config: %w", errUtils.ErrInvalidAuthConfig, err)
}
// Apply deferred merges (without YAML processing - already done earlier).
if err := m.ApplyDeferredMerges(mergeCtx, mergedAuthConfig, atmosConfig, nil); err != nil {
return nil, fmt.Errorf("%w: apply deferred merges for auth config: %w", errUtils.ErrInvalidAuthConfig, err)
}
return mergedAuthConfig, nil
}