argoproj / argo-cd

Declarative Continuous Deployment for Kubernetes
https://argo-cd.readthedocs.io
Apache License 2.0
17.47k stars 5.31k forks source link

The matchExpressions selector in a git generator, part of a matrix generator, cannot use parameters from the other generator with goTemplate: true #17610

Open vladimir-shopov opened 5 months ago

vladimir-shopov commented 5 months ago

Checklist:

Describe the bug

When trying to refer to a parameter in a matchExpressions post selector in a git generator, which is part of a matrix generator, where the parameter is derived from the other generator, part of the matrix generator, the following error is produced:

failed to get params for second generator in the matrix generator: child generator returned an error on parameter generation: error parsing label selector: key: Invalid value: "{{(index . .name).appEnabled}}": name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')

The whole idea behind this is to be able to control the enablement of the application for a particular cluster via a centralized config file and an enable flag for each cluster.

This might be partially related to https://github.com/argoproj/argo-cd/issues/12524.

To Reproduce

The following snippet can be used to reproduce the issue:

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: test
  namespace: argocd
spec:
  generators:
    - matrix:
        generators:
          - clusters: {}
          - git:
              files:
                - path: config.yml
              repoURL: '<config repo>'
              revision: main
            selector:
              matchExpressions:
                - key: '{{(index . .name).appEnabled}}'
                  operator: Exists
  goTemplate: true
  template:
    metadata:
      name: 'test-{{.name}}'
    spec:
      destination:
        name: '{{.name}}'
        namespace: default
      project: test
      source:
        path: .
        repoURL: '<manifests repo>'
        targetRevision: main
      syncPolicy: {}

The content of the config.yml file, used in the git generator, is as follows:

test-cluster-1:
  appEnabled: dummy

Expected behavior

I expect to be able to use the .name parameter, derived from the cluster generator, and Go templates in the post selector of the git generator.

Screenshots

A screenshot depicting the error message:

argocd error

Version

{
    "Version": "v2.10.3+0fd6344",
    "BuildDate": "2024-03-13T19:03:00Z",
    "GitCommit": "0fd6344537eb948cff602824a1d060421ceff40e",
    "GitTreeState": "clean",
    "GoVersion": "go1.21.3",
    "Compiler": "gc",
    "Platform": "linux/amd64",
    "KustomizeVersion": "v5.2.1 2023-10-19T20:13:51Z",
    "HelmVersion": "v3.14.2+gc309b6f",
    "KubectlVersion": "v0.26.11",
    "JsonnetVersion": "v0.20.0"
}
irizzant commented 2 months ago

I ran into the same problem.

There seem to be a workaround here https://github.com/argoproj/argo-cd/issues/16830#issuecomment-2088750814.