github.com/argoproj/argo-cd/v2@v2.10.9/docs/operator-manual/applicationset/Generators-Merge.md (about)

     1  # Merge Generator
     2  
     3  The Merge generator combines parameters produced by the base (first) generator with matching parameter sets produced by subsequent generators. A _matching_ parameter set has the same values for the configured _merge keys_. _Non-matching_ parameter sets are discarded. Override precedence is bottom-to-top: the values from a matching parameter set produced by generator 3 will take precedence over the values from the corresponding parameter set produced by generator 2.
     4  
     5  Using a Merge generator is appropriate when a subset of parameter sets require overriding.
     6  
     7  ## Example: Base Cluster generator + override Cluster generator + List generator 
     8  
     9  As an example, imagine that we have two clusters:
    10  
    11  - A `staging` cluster (at `https://1.2.3.4`)
    12  - A `production` cluster (at `https://2.4.6.8`)
    13  
    14  ```yaml
    15  apiVersion: argoproj.io/v1alpha1
    16  kind: ApplicationSet
    17  metadata:
    18    name: cluster-git
    19  spec:
    20    goTemplate: true
    21    goTemplateOptions: ["missingkey=error"]
    22    generators:
    23      # merge 'parent' generator
    24      - merge:
    25          mergeKeys:
    26            - server
    27          generators:
    28            - clusters:
    29                values:
    30                  kafka: 'true'
    31                  redis: 'false'
    32            # For clusters with a specific label, enable Kafka.
    33            - clusters:
    34                selector:
    35                  matchLabels:
    36                    use-kafka: 'false'
    37                values:
    38                  kafka: 'false'
    39            # For a specific cluster, enable Redis.
    40            - list:
    41                elements: 
    42                  - server: https://2.4.6.8
    43                    values.redis: 'true'
    44    template:
    45      metadata:
    46        name: '{{.name}}'
    47      spec:
    48        project: '{{index .metadata.labels "environment"}}'
    49        source:
    50          repoURL: https://github.com/argoproj/argo-cd.git
    51          targetRevision: HEAD
    52          path: app
    53          helm:
    54            parameters:
    55              - name: kafka
    56                value: '{{.values.kafka}}'
    57              - name: redis
    58                value: '{{.values.redis}}'
    59        destination:
    60          server: '{{.server}}'
    61          namespace: default
    62  ```
    63  
    64  The base Cluster generator scans the [set of clusters defined in Argo CD](Generators-Cluster.md), finds the staging and production cluster secrets, and produces two corresponding sets of parameters:
    65  ```yaml
    66  - name: staging
    67    server: https://1.2.3.4
    68    values.kafka: 'true'
    69    values.redis: 'false'
    70    
    71  - name: production
    72    server: https://2.4.6.8
    73    values.kafka: 'true'
    74    values.redis: 'false'
    75  ```
    76  
    77  The override Cluster generator scans the [set of clusters defined in Argo CD](Generators-Cluster.md), finds the staging cluster secret (which has the required label), and produces the following parameters:
    78  ```yaml
    79  - name: staging
    80    server: https://1.2.3.4
    81    values.kafka: 'false'
    82  ```
    83  
    84  When merged with the base generator's parameters, the `values.kafka` value for the staging cluster is set to `'false'`.
    85  ```yaml
    86  - name: staging
    87    server: https://1.2.3.4
    88    values.kafka: 'false'
    89    values.redis: 'false'
    90  
    91  - name: production
    92    server: https://2.4.6.8
    93    values.kafka: 'true'
    94    values.redis: 'false'
    95  ```
    96  
    97  Finally, the List cluster generates a single set of parameters:
    98  ```yaml
    99  - server: https://2.4.6.8
   100    values.redis: 'true'
   101  ```
   102  
   103  When merged with the updated base parameters, the `values.redis` value for the production cluster is set to `'true'`. This is the merge generator's final output:
   104  ```yaml
   105  - name: staging
   106    server: https://1.2.3.4
   107    values.kafka: 'false'
   108    values.redis: 'false'
   109  
   110  - name: production
   111    server: https://2.4.6.8
   112    values.kafka: 'true'
   113    values.redis: 'true'
   114  ```
   115  
   116  ## Example: Use value interpolation in merge
   117  
   118  Some generators support additional values and interpolating from generated variables to selected values. This can be used to teach the merge generator which generated variables to use to combine different generators.
   119  
   120  The following example combines discovered clusters and a git repository by cluster labels and the branch name:
   121  ```yaml
   122  apiVersion: argoproj.io/v1alpha1
   123  kind: ApplicationSet
   124  metadata:
   125    name: cluster-git
   126  spec:
   127    goTemplate: true
   128    goTemplateOptions: ["missingkey=error"]
   129    generators:
   130      # merge 'parent' generator:
   131      # Use the selector set by both child generators to combine them.
   132      - merge:
   133          mergeKeys:
   134            # Note that this would not work with goTemplate enabled,
   135            # nested merge keys are not supported there.
   136            - values.selector
   137          generators:
   138            # Assuming, all configured clusters have a label for their location:
   139            # Set the selector to this location.
   140            - clusters:
   141                values:
   142                  selector: '{{index .metadata.labels "location"}}'
   143            # The git repo may have different directories which correspond to the
   144            # cluster locations, using these as a selector.
   145            - git:
   146                repoURL: https://github.com/argoproj/argocd-example-apps/
   147                revision: HEAD
   148                directories:
   149                - path: '*'
   150                values:
   151                  selector: '{{.path.path}}'
   152    template:
   153      metadata:
   154        name: '{{.name}}'
   155      spec:
   156        project: '{{index .metadata.labels "environment"}}'
   157        source:
   158          repoURL: https://github.com/argoproj/argocd-example-apps/
   159          # The cluster values field for each generator will be substituted here:
   160          targetRevision: HEAD
   161          path: '{{.path.path}}'
   162        destination:
   163          server: '{{.server}}'
   164          namespace: default
   165  ```
   166  
   167  Assuming a cluster named `germany01` with the label `metadata.labels.location=Germany` and a git repository containing a directory called `Germany`, this could combine to values as follows:
   168  
   169  ```yaml
   170    # From the cluster generator
   171  - name: germany01
   172    server: https://1.2.3.4
   173    # From the git generator
   174    path: Germany
   175    # Combining selector with the merge generator
   176    values.selector: 'Germany'
   177    # More values from cluster & git generator
   178    # […]
   179  ```
   180  
   181  
   182  ## Restrictions
   183  
   184  1. You should specify only a single generator per array entry. This is not valid:
   185  
   186          - merge:
   187              generators:
   188              - list: # (...)
   189                git: # (...)
   190  
   191      - While this *will* be accepted by Kubernetes API validation, the controller will report an error on generation. Each generator should be specified in a separate array element, as in the examples above.
   192  
   193  1. The Merge generator does not support [`template` overrides](Template.md#generator-templates) specified on child generators. This `template` will not be processed:
   194  
   195          - merge:
   196              generators:
   197                - list:
   198                    elements:
   199                      - # (...)
   200                    template: { } # Not processed
   201  
   202  1. Combination-type generators (Matrix or Merge) can only be nested once. For example, this will not work:
   203  
   204          - merge:
   205              generators:
   206                - merge:
   207                    generators:
   208                      - merge:  # This third level is invalid.
   209                          generators:
   210                            - list:
   211                                elements:
   212                                  - # (...)
   213  
   214  1. Merging on nested values while using `goTemplate: true` is currently not supported, this will not work
   215  
   216          spec:
   217            goTemplate: true
   218            generators:
   219            - merge:
   220                mergeKeys:
   221                  - values.merge
   222  
   223  1. When using a Merge generator nested inside another Matrix or Merge generator, [Post Selectors](Generators-Post-Selector.md) for this nested generator's generators will only be applied when enabled via `spec.applyNestedSelectors`.
   224  
   225          - merge:
   226              generators:
   227                - merge:
   228                    generators:
   229                      - list
   230                          elements:
   231                            - # (...)
   232                        selector: { } # Only applied when applyNestedSelectors is true