Hey! I have a question about list merging and reu...
# atmos
l
Hey! I have a question about list merging and reusing components. Lets say I have a component for creating a containerjob (cloudrun job/ containerapp jobs, etc) I might have an abstract component that defines a bunch of env variables and also a default command (which is also a list)
Copy code
components:
  terraform:
    container_job/defaults:
      metadata:
        type: abstract 
     vars:
      env:
       - name: foo
         value: bar
...
...
..,
      - name: foo100
        value: bar100
      command: ["echo","baz"]
Now if if I want to reuse this component and add some extra env variables in one verions of the job that works fine if I have the
list_merge_strategy = append
. However, if I also want to override the command to instead be
["echo", "foo"]
it becomes
["echo", "baz", "echo", "foo" ]
( due to the append) is there a way around this or a better way to handle it?
đź‘€ 1
a
Atmos
env
section is a map, not a list (similar to the
vars
section)
Copy code
env:
       foo: bar
maps are merged (new keys added, existing keys overridden via inheritance, not replaced as list by default)
change to maps and you'll have the behavior you want
oh sorry, you have the
env
var, not the
env
section
for this to work, you have a few choices :
1. In
atmos.yaml
(note this is a global setting, it will affect all lists in the stacks, we don't have the ability to change an individaul list yet, but we'll imprlrmrnt it)
Copy code
settings:
  # `list_merge_strategy` specifies how lists are merged in Atmos stack manifests.
  # Can also be set using 'ATMOS_SETTINGS_LIST_MERGE_STRATEGY' environment variable, or '--settings-list-merge-strategy' command-line argument
  # The following strategies are supported:
  # `replace`: Most recent list imported wins (the default behavior).
  # `append`:  The sequence of lists is appended in the same order as imports.
  # `merge`:   The items in the destination list are deep-merged with the items in the source list.
  #            The items in the source list take precedence.
  #            The items are processed starting from the first up to the length of the source list (the remaining items are not processed).
  #            If the source and destination lists have the same length, all items in the destination lists are
  #            deep-merged with all items in the source list.
  list_merge_strategy: replace
2. If you don't want to override it globally, you can add a few abstract components with different
vars.env
, then import them into the stack(s) and inherit from them. Different real components can inherit an abstract component with the required
vars.env.
See https://atmos.tools/design-patterns/abstract-component (this is similar to base abstract class in OOP)
3. Instead of the
Abstract Component Atmos Design Pattern
, you can also use the
Partial Component Configuration
pattern https://atmos.tools/design-patterns/partial-component-configuration. The difference is that in this case, you import different 'parts' of the same component (not anstract base) into the stacks, and you can have those parts defined with different
vars.env
configuration, and import one of those into the stack to add that 'part' to your component (this is similar to partial classes in OOP, e.g. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods)
both patterns allow you to define different parts of your components in separate manifests (YAML files), and then import them into the stacks and/or inherit from them
l
Thanks for the quick reply! I think I'll have to take a look at the partial-component pattern then. beacuse as I see it if I go with 1. Now I can override the command, but then I can't append env variables to the same job if I'd want to. 2. I already have an abstract component but it doesn't solve the issue for me really unfortunately. I think my preferred method would be to be able to override specifically on a list and not have it just be set globally, so nice to hear that it seems like you have thought about addign that
e
Based on what I see, I think the partial component pattern will allow you to have those imports that layer on and override only selectively what you want. It works quite nicely for that. We've seen customers use it for feature flagging—they use an import to turn on and off a feature.
l
I tried with partial components but I still get the same problem that lists are appended or merged depending on how the global setting is set. I'll just fix the terraform module to be able to handle it I think. This might be the preferred way to do it, I am new to atmos so it might be that I'm just not thinking about solutions in "the atmos way" . But I did look into this override functionality (https://atmos.tools/design-patterns/component-overrides/) and it would be nice to be able to override on a component level (as I understand it, and from my testing overrides are only availabe on a scope of all components), because then I could keep list_merge = append and when I need to override a list I can do it like this
Copy code
components:
  terraform:
    container_job/defaults:
      overrides: 
        vars:
          command:["echo","baz"]
      metadata:
        component: container_job
        inherit: container_job/defaults
and get what I want
e
If we take a step back, I admit I don’t fully understand your use-case to be able to advise on how to best achieve it. As you allude to, there might be a different way to go about it. Actually it’s common that we would say, that making your modules “smarter” is better than making configuration messier, so your conclusion might be right. https://xyproblem.info/
If you happen to use our null label, we also have this output specifically for the ECS env problem
tags_as_list_of_maps