argo-cd icon indicating copy to clipboard operation
argo-cd copied to clipboard

feat: Allow Matrix generator to have two Git child generators without conflict

Open Lobstrosity opened this issue 2 years ago β€’ 1 comments

Summary

Matrix generators don’t allow items from the child generators to have identical keys with distinct values. This prevents having a Matrix generator where both child generators are Git generators, since they’ll auto-generate conflicting path-related parameters.

Motivation

Ability to do something like the following, where there are no conflicting parameters:

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: my-apps
spec:
  generators:
    - matrix:
        generators:
          # `apps` repo defines apps to deploy. the `basename` values from this
          # repo are app names.
          - git:
              repoURL: https://github.com/org/apps.git
              pathParamPrefix: apps
              files:
                - path: '*'
          # `targets` repo defines clusters to which to deploy each app. there's
          # a directory per app interpolated into this repo's path (using
          # functionality from PR#9080) and the `basename` values from this repo
          # are the clusters that each app should deploy to.
          - git:
              repoURL: https://github.com/org/targets.git
              pathParamPrefix: targets
              files:
                - path: '{{apps.path.basename}}/*'
template:
  metadata:
    # app- and target-specific name for each `Application`.
    name: '{{apps.path.basename}}-{{targets.path.basename}}'
  spec:
    # ...
    destination:
      # assume there's a cluster secret for each target cluster where the
      # secret has the same name as the cluster.
      server: '{{targets.path.basename}}'

Proposal

Based on this Slack thread, add an optional prefix to Git generator config (pathParamPrefix in the sample above) that will be included in all path-related parameter names.

Lobstrosity avatar Sep 05 '22 04:09 Lobstrosity

Great idea! πŸ‘

richardjennings avatar Sep 09 '22 11:09 richardjennings

@Lobstrosity this is still on my radar, just don't want it to be a blocker for 2.5. 2.6 should have a relatively quick turnaround.

crenshaw-dev avatar Oct 04 '22 15:10 crenshaw-dev

Just a word to say it works like a charm in my setup, definitely something I was waiting for. Thanks a lot!

gaeljw avatar Dec 21 '22 16:12 gaeljw

This is exactly what I needed! Many thanks! πŸ™

My use-case might be useful for others as reference. This PR allows me to structure my repo in a way such that an app is only generated if there's an overlay defined for the specific environment.

This is an example app-set definition used for production:

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: production-apps
  namespace: argocd
spec:
  generators:
  - matrix:
      generators:
        - git:
            repoURL: https://github.com/xxxxxxxx/xxxxxxxx
            revision: main
            pathParamPrefix: "app"
            files:
            - path: "components/**/app-config.yaml"
        - git:
            repoURL: https://github.com/xxxxxxxx/xxxxxxxx
            revision: main
            pathParamPrefix: "overlay"
            files:
            - path: '{{app.path}}/overlays/production/kustomization.yaml'
  template:
    metadata:
      name: '{{app.path.basename}}'
    spec:
      project: default
      source:
        repoURL: https://github.com/xxxxxxxx/xxxxxxxx
        targetRevision: main
        path: '{{overlay.path}}'
      destination:
        server: https://kubernetes.default.svc

Example repo structure

apps
β”œβ”€β”€ webapp-1
β”‚Β Β  β”œβ”€β”€ app-config.yaml
β”‚Β Β  β”œβ”€β”€ base
β”‚Β Β  β”‚Β Β  └── kustomization.yaml
β”‚Β Β  └── overlays
β”‚Β Β      β”œβ”€β”€ production
β”‚Β Β      β”‚   └── kustomization.yaml
β”‚Β Β      └── staging
β”‚Β Β          └── kustomization.yaml
└─── webapp-2
 Β Β  β”œβ”€β”€ app-config.yaml
 Β Β  β”œβ”€β”€ base
 Β Β  β”‚Β Β  └── kustomization.yaml
 Β Β  └── overlays
 Β Β      └── staging
  Β          └── kustomization.yaml

With above example, only webapp-1 would be listed as webapp-2 has no production overlay defined.

Benefits of this folder structure

  • Can use the basename of app folder as the app name
  • All app specific configuration is bundled together under the app dir
  • When a solution for https://github.com/argoproj/argo-cd/issues/11164 is implemented app-config.yaml can contain app specific templating variables (e.g configuring autoSync)

boedy avatar Dec 26 '22 19:12 boedy

This is a very useful feature but why not to include the option to have a dynamic git repoURL in the child generator? Something like

spec:
  generators:
    - matrix:
        generators:
        - git: 
            repoURL: https://<gitpath>/_git/management
            revision: HEAD
            files:
            - path: config/customers/**/config.yaml
        - git:
            repoURL: "{{spec.sourceRepos[0]}}"
            revision: HEAD
            files:
            - path: stacks/**/config.json

Would it be possible? Or this feature is already part of 2.6 plan?

Thanks

mfabbri avatar Feb 02 '23 15:02 mfabbri