Merge Generator

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.

Using a Merge generator is appropriate when a subset of parameter sets require overriding.

Example: Base Cluster generator + override Cluster generator + List generator

As an example, imagine that we have two clusters:

  • A staging cluster (at https://1.2.3.4)
  • A production cluster (at https://2.4.6.8)
  1. apiVersion: argoproj.io/v1alpha1
  2. kind: ApplicationSet
  3. metadata:
  4. name: cluster-git
  5. spec:
  6. generators:
  7. # merge 'parent' generator
  8. - merge:
  9. mergeKeys:
  10. - server
  11. generators:
  12. - clusters:
  13. values:
  14. kafka: 'true'
  15. redis: 'false'
  16. # For clusters with a specific label, enable Kafka.
  17. - clusters:
  18. selector:
  19. matchLabels:
  20. use-kafka: 'false'
  21. values:
  22. kafka: 'false'
  23. # For a specific cluster, enable Redis.
  24. - list:
  25. elements:
  26. - server: https://2.4.6.8
  27. values.redis: 'true'
  28. template:
  29. metadata:
  30. name: '{{name}}'
  31. spec:
  32. project: '{{metadata.labels.environment}}'
  33. source:
  34. repoURL: https://github.com/argoproj/argo-cd.git
  35. targetRevision: HEAD
  36. path: app
  37. helm:
  38. parameters:
  39. - name: kafka
  40. value: '{{values.kafka}}'
  41. - name: redis
  42. value: '{{values.redis}}'
  43. destination:
  44. server: '{{server}}'
  45. namespace: default

The base Cluster generator scans the set of clusters defined in Argo CD, finds the staging and production cluster secrets, and produces two corresponding sets of parameters:

  1. - name: staging
  2. server: https://1.2.3.4
  3. values.kafka: 'true'
  4. values.redis: 'false'
  5. - name: production
  6. server: https://2.4.6.8
  7. values.kafka: 'true'
  8. values.redis: 'false'

The override Cluster generator scans the set of clusters defined in Argo CD, finds the staging cluster secret (which has the required label), and produces the following parameters:

  1. - name: staging
  2. server: https://1.2.3.4
  3. values.kafka: 'false'

When merged with the base generator’s parameters, the values.kafka value for the staging cluster is set to 'false'.

  1. - name: staging
  2. server: https://1.2.3.4
  3. values.kafka: 'false'
  4. values.redis: 'false'
  5. - name: production
  6. server: https://2.4.6.8
  7. values.kafka: 'true'
  8. values.redis: 'false'

Finally, the List cluster generates a single set of parameters:

  1. - server: https://2.4.6.8
  2. values.redis: 'true'

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:

  1. - name: staging
  2. server: https://1.2.3.4
  3. values.kafka: 'false'
  4. values.redis: 'false'
  5. - name: production
  6. server: https://2.4.6.8
  7. values.kafka: 'true'
  8. values.redis: 'true'

Restrictions

  1. You should specify only a single generator per array entry. This is not valid:

    1. - merge:
    2. generators:
    3. - list: # (...)
    4. git: # (...)
    • 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.
  2. The Merge generator does not support template overrides specified on child generators. This template will not be processed:

    1. - merge:
    2. generators:
    3. - list:
    4. elements:
    5. - # (...)
    6. template: { } # Not processed
  3. Combination-type generators (Matrix or Merge) can only be nested once. For example, this will not work:

    1. - merge:
    2. generators:
    3. - merge:
    4. generators:
    5. - merge: # This third level is invalid.
    6. generators:
    7. - list:
    8. elements:
    9. - # (...)