argoproj / argo-cd

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

Interpolation does not work in git generator #18877

Open steve-todorov opened 3 days ago

steve-todorov commented 3 days ago

Checklist:

Describe the bug

We are using a matrix generator together with a merge generator and a git file generator. The interpolation inside the repoURL and revision parameters of the git file generator are not wodking. When you try to reference the {{.branch}} parameter you will get the literal {{.branch}} instead of the actual value.

I went back to v2.4.0 where this was introduced and it looks like the interpolation is supposed to work only for values which seems to be very odd?

https://github.com/argoproj/argo-cd/compare/v2.3.0...v2.4.0#diff-e18bda08876f1471ded096f197d58778d250cd942ca66539391b067d5f069f79

Here's the slack thread where I discussed this with @crenshaw-dev: https://cloud-native.slack.com/archives/C01TSERG0KZ/p1719585649594259

To Reproduce

spec:
  goTemplate: true
  goTemplateOptions: ["missingkey=error"]
  generators:
  - matrix:
      generators:
        - scmProvider:
            cloneProtocol: ssh
            github:
              organization: my-organization
              allBranches: true
              tokenRef:
                secretName: github-token
                key: token
            filters:
              - repositoryMatch: ^my-repository$
                branchMatch: ^feature-.*$
                pathsExist: [deployment/kubernetes/kustomization.yaml]
        - git:
            # None of the SCM variables work here.
            repoURL: "https://github.com/{{.organization}}/{{.repository}}"
            revision: "{{.branch}}"
            files:
              - path: "deployment/kubernetes/overrides-{{.branch}}.yaml"

Expected behavior

Interpolation should work in the git file generator parameters.

Version

argocd: v2.11.3+3f344d5
  BuildDate: 2024-06-06T09:27:36Z
  GitCommit: 3f344d54a4e0bbbb4313e1c19cfe1e544b162598
  GitTreeState: clean
  GoVersion: go1.21.10
  Compiler: gc
  Platform: linux/amd64
argocd-server: v2.11.0+d3f33c0
  BuildDate: 2024-05-07T16:01:41Z
  GitCommit: d3f33c00197e7f1d16f2a73ce1aeced464b07175
  GitTreeState: clean
  GoVersion: go1.21.9
  Compiler: gc
  Platform: linux/amd64
  Kustomize Version: v5.2.1 2023-10-19T20:13:51Z
  Helm Version: v3.14.3+gf03cc04
  Kubectl Version: v0.26.11
  Jsonnet Version: v0.20.0

Logs

Different variations of this error (depending on the version I tried)

ERRO[0048] error generating params                       error="Error during fetching repo: `git fetch origin {{.branch}} --tags --force --prune` failed exit status 128: fatal: couldn't find remote ref {{.branch}}" generator="&{0xc00186e270}" 
INFO[0208] Trace                                         args="[git fetch origin {{.branch}} --tags --force]" dir=/tmp/git@github.com_xyz_repo operation_name="exec git" time_ms=1366.6199119999999
christianh814 commented 3 days ago

@steve-todorov I read the thread. Do you have .spec.goTemplate set to true in your ApplicationSet resource?

steve-todorov commented 3 days ago

@christianh814 yes I have it (updated the ticket to reflect this, sorry)

spec:
  goTemplate: true
  goTemplateOptions: ["missingkey=error"]
christianh814 commented 3 days ago

@steve-todorov It shouldn't "matter" but have you tried using '' instead? Like '{{.branch}}' and 'https://github.com/{{.organization}}/{{.repository}}'

steve-todorov commented 2 days ago

@christianh814 My code slightly evolved since I've opened the ticket, because I've introduced a custom plugin that generates some additional parameters based on the branch name. I've tried a few combinations but none of them work (again showing either a missing .branch, .organization, repository error or .case_number one).

Basically the idea is to have the scmProvider scan for the repos, then have the plugin generate dynamic properties based on branch name convention and finally give an option to manually override properties via a git path in the same repository when a file with the same case number exists.

Example 1:

spec:
  goTemplate: true
  goTemplateOptions: ["missingkey=error"]
  generators:

    - matrix:
        generators:
          - scmProvider:
              cloneProtocol: ssh
              github:
                organization: my-organization
                allBranches: true
                tokenRef:
                  secretName: github-token
                  key: token
              filters:
                - repositoryMatch: ^my-repository$
                  branchMatch: ^kustomize|scm/([0-9]+).*$
                  pathsExist: [deployment/kubernetes/kustomization.yaml]

          - plugin:
              configMapRef:
                # produces some parameters based on the branch naming convention (i.e. case_number)
                name: applicationset-branch-generator-plugin
              input:
                parameters:
                  branch: '{{.branch}}'
                  project_code: 'exmpl'
                  organization: '{{.organization}}'
                  repository: '{{.repository}}'
              requeueAfterSeconds: 30

    - git:
        repoURL: 'git@github.com:{{.organization}}/{{.repository}}.git'
        revision: '{{.branch}}'
        files:
          - path: 'deployment/kubernetes/properties/{{.case_number}}.yml'

Example 2:

spec:
  goTemplate: true
  goTemplateOptions: ["missingkey=error"]
  generators:

    - matrix:
        generators:
          - scmProvider:
              cloneProtocol: ssh
              github:
                organization: my-organization
                allBranches: true
                tokenRef:
                  secretName: github-token
                  key: token
              filters:
                - repositoryMatch: ^my-repository$
                  branchMatch: ^kustomize|scm/([0-9]+).*$
                  pathsExist: [deployment/kubernetes/kustomization.yaml]

          - matrix:
              generators:
                - plugin:
                    configMapRef:
                      # produces some parameters based on the branch naming convention (i.e. case_number)
                      name: applicationset-branch-generator-plugin
                    input:
                      parameters:
                        branch: '{{.branch}}'
                        project_code: 'exmpl'
                        organization: '{{.organization}}'
                        repository: '{{.repository}}'
                    requeueAfterSeconds: 30

                - git:
                    repoURL: 'git@github.com:{{.organization}}/{{.repository}}.git'
                    revision: '{{.branch}}'
                    files:
                      - path: 'deployment/kubernetes/properties/{{.case_number}}.yml'

Here's an example output of the generator plugin:

10.0.0.8 - - [02/Jul/2024 12:19:19] "POST /api/v1/getparams.execute HTTP/1.1" 200 -
Received arguments: {'applicationSetName': 'exmpl-staging-scm-preview', 'input': {'parameters': {'branch': 'scm/35765-some-title', 'organization': 'my-organization', 'project_code': 'exmpl', 'repository': 'my-repository'}}}
Output parameters: {'case_number': '35765', 'subdomain': '35765-exmpl-staging-scm', 'memoryRequest': '500Mi', 'memoryLimit': '700Mi', 'keycloakServerUrl': 'https://keycloak-staging.local.domain/auth', 'branch': 'scm/35765-some-title', 'organization': 'my-organization', 'project_code': 'exmpl', 'repository': 'my-repository'}

So the plugin does receive the proper values and switching from double quotes " to single quotes ' seems to be resolving the .branch / .organization / .repository error. Unfortunately now it's unable to resolve the .case_number that's generated by the custom generator plugin.

ERRO[301695] error interpolating generator with other generator's parameter  error="failed to replace parameters in generator: failed to execute go template deployment/kubernetes/properties/{{.case_number}}.yml: template: :1:35: executing \"\" at <.case_number>: map has no entry for key \"case_number\"" interpolatedGenerator=nil
ERRO[301695] error interpolating params for generator      error="failed to replace parameters in generator: failed to execute go template deployment/kubernetes/properties/{{.case_number}}.yml: template: :1:35: executing \"\" at <.case_number>: map has no entry for key \"case_number\"" genParams="map[branch:kustomize branchNormalized:kustomize labels: organization:my-organization repository:my-repository sha:xdfe8ccb3609e14d1ada9cf1967568f9e277075 short_sha:abdfe8cc short_sha_7:abdfe8c url:git@github.com:my-organization/my-repository.git]"
ERRO[301695] error generating params                       error="failed to get params for second generator in the matrix generator: child generator returned an error on parameter generation: failed to replace parameters in generator: failed to execute go template deployment/kubernetes/properties/{{.case_number}}.yml: template: :1:35: executing \"\" at <.case_number>: map has no entry for key \"case_number\"" generator="&{map[ClusterDecisionResource:0xc001383590 Clusters:0xc001383540 Git:0xc0013e7a70 List:0x75e9dc0 Matrix:0xc0013980e0 Merge:0xc0013980e8 Plugin:0xc001560bc0 PullRequest:0xc001395680 SCMProvider:0xc001395620]}"
ERRO[301695] error generating application from params      applicationset=argocd/exmpl-staging-scm-preview error="failed to get params for second generator in the matrix generator: child generator returned an error on parameter generation: failed to replace parameters in generator: failed to execute go template deployment/kubernetes/properties/{{.case_number}}.yml: template: :1:35: executing \"\" at <.case_number>: map has no entry for key \"case_number\"" generator="{nil nil nil nil nil nil &MatrixGenerator{Generators:[]ApplicationSetNestedGenerator{ApplicationSetNestedGenerator{List:nil,Clusters:nil,Git:nil,SCMProvider:&SCMProviderGenerator{Github:&SCMProviderGeneratorGithub{Organization:my-organization,API:,TokenRef:&SecretRef{SecretName:github-token,Key:token,},AppSecretName:,AllBranches:true,},Gitlab:nil,Bitbucket:nil,BitbucketServer:nil,Gitea:nil,AzureDevOps:nil,Filters:[]SCMProviderGeneratorFilter{SCMProviderGeneratorFilter{RepositoryMatch:*^my-repository$,PathsExist:[deployment/kubernetes/kustomization.yaml],PathsDoNotExist:[],LabelMatch:nil,BranchMatch:*^kustomize|scm/([0-9]+).*$,},},CloneProtocol:ssh,RequeueAfterSeconds:nil,Template:ApplicationSetTemplate{ApplicationSetTemplateMeta:ApplicationSetTemplateMeta{Name:,Namespace:,Labels:map[string]string{},Annotations:map[string]string{},Finalizers:[],},Spec:ApplicationSpec{Source:nil,Destination:ApplicationDestination{Server:,Namespace:,Name:,},Project:,SyncPolicy:nil,IgnoreDifferences:[]ResourceIgnoreDifferences{},Info:[]Info{},RevisionHistoryLimit:nil,Sources:[]ApplicationSource{},},},Values:map[string]string{},AWSCodeCommit:nil,},ClusterDecisionResource:nil,PullRequest:nil,Matrix:nil,Merge:nil,Selector:nil,Plugin:nil,},ApplicationSetNestedGenerator{List:nil,Clusters:nil,Git:nil,SCMProvider:nil,ClusterDecisionResource:nil,PullRequest:nil,Matrix:&v11.JSON{Raw:*[ shortened ],},Merge:nil,Selector:nil,Plugin:nil,},},Template:ApplicationSetTemplate{ApplicationSetTemplateMeta:ApplicationSetTemplateMeta{Name:,Namespace:,Labels:map[string]string{},Annotations:map[string]string{},Finalizers:[],},Spec:ApplicationSpec{Source:nil,Destination:ApplicationDestination{Server:,Namespace:,Name:,},Project:,SyncPolicy:nil,IgnoreDifferences:[]ResourceIgnoreDifferences{},Info:[]Info{},RevisionHistoryLimit:nil,Sources:[]ApplicationSource{},},},} nil nil nil}"
INFO[301720] generated 1 applications

Not sure what I'm doing wrong here. Any ideas?