argoproj / argo-cd

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

Can use list values inside generator #19106

Open afreyermuth98 opened 1 month ago

afreyermuth98 commented 1 month ago

Describe the bug

I'm trying to make a basic generator to deploy multiple apps with ArgoCD but I'm getting invalid map key on my values

To Reproduce

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: test
  namespace: argocd
spec:
  goTemplate: true
  goTemplateOptions: ["missingkey=error"]
  generators:
    # Generator for apps that should deploy to chosen cluster.
    - matrix:
        generators:
          - clusters: {}
          - list:
              elements:
              - appName: karpenter
                chartName: karpenter
                repoUrl: public.ecr.aws/karpenter
                targetRevision: 0.37.0
                namespace: karpenter
  template:
    metadata:
      name: "{{name}}-{{appName}}"
      annotations:
        argocd.argoproj.io/manifest-generate-paths: ".;.."
    spec:
      project: test
      source:
        chart: {{chartName}}
        repoURL: {{repoUrl}}
        targetRevision: {{targetRevision}}
        helm:
          valueFiles:
          - ../../values/{{name}}/{{appName}}/{{cluster}}/values.yaml
      destination:
        name: "{{cluster}}"
        namespace: "{{namespace}}"
      syncPolicy:
        automated:
          prune: true
          selfHeal: true
        syncOptions:
          - CreateNamespace=true
        retry:
          limit: 2

And when I'm applying this, I'm getting error: error parsing application_sets/karpenter.yaml: error converting YAML to JSON: yaml: invalid map key: map[interface {}]interface {}{"chartName":interface {}(nil)}

Expected behavior The kubectl apply to pass

Screenshots

Version

Paste the output from `argocd version` here.

Logs

Paste any relevant application logs here.
MairaTariq16 commented 1 month ago

Have you tried adding '.' before referring values? Like {{.chartName}}. And try to wrap in quotes as well.

afreyermuth98 commented 1 month ago

Yes same issue :

error: error parsing application_sets/karpenter.yaml: error converting YAML to JSON: yaml: invalid map key: map[interface {}]interface {}{".chartName":interface {}(nil)}
christianh814 commented 1 month ago

You have {{ name }} and `{{ cluster }}but you have nothing defining it

    - matrix:
        generators:
          # - clusters: {}
          - list:
              elements:
              - appName: karpenter
                chartName: karpenter
                repoUrl: public.ecr.aws/karpenter
                targetRevision: 0.37.0
                namespace: karpenter

You've commented out clusters therefore those two variables never get set

afreyermuth98 commented 1 month ago

@christianh814 sorry it's just a typo, it's not commented on my current setup 😬

christianh814 commented 1 month ago

@afreyermuth98 Does this work?

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: test
  namespace: argocd
spec:
  goTemplate: true
  goTemplateOptions: ["missingkey=error"]
  generators:
    # Generator for apps that should deploy to chosen cluster.
    - matrix:
        generators:
          - clusters: {}
          - list:
              elements:
              - appName: karpenter
                chartName: karpenter
                repoUrl: public.ecr.aws/karpenter
                targetRevision: 0.37.0
                namespace: karpenter
  template:
    metadata:
      name: '{{ .name }}-{{ .appName }}'
      annotations:
        argocd.argoproj.io/manifest-generate-paths: '.;..'
    spec:
      project: test
      source:
        chart: '{{ .chartName }}'
        repoURL: '{{ .repoUrl }}'
        targetRevision: '{{ .targetRevision }}'
        helm:
          valueFiles:
          - '../../values/{{ .name }}/{{ .appName }}/{{ .cluster }}/values.yaml'
      destination:
        name: '{{ .name }}'
        namespace: '{{ .namespace }}'
      syncPolicy:
        automated:
          prune: true
          selfHeal: true
        syncOptions:
          - CreateNamespace=true
        retry:
          limit: 2

Also {{ .cluster }} is never defined (see documentation) so I took it out. I think you meant "name"

afreyermuth98 commented 1 month ago

Yes it is thanks a lot ! It only needed the quotes I guess ?