Skip to content

Fix | Redirect sources defined in ApplicationSet spec.templatePatch - #518

Open
anthonyhaussman wants to merge 1 commit into
dag-andersen:mainfrom
anthonyhaussman:tone/fix/redirect-templatepatch-sources
Open

anthonyhaussman wants to merge 1 commit into
dag-andersen:mainfrom
anthonyhaussman:tone/fix/redirect-templatepatch-sources

Conversation

@anthonyhaussman

Copy link
Copy Markdown

Problem

RedirectSources only rewrites targetRevision under spec.template.spec.source(s) of an ApplicationSet. When the sources are defined in spec.templatePatch (a Go-template string, often required for conditional sources such as {{- if eq (index .metadata.labels "env") "prod" }}), they are never redirected to the PR branch.

As a result, the base and target ApplicationSets are identical, they are dropped as identical copy, and the tool reports no changes even when the referenced chart or values changed.

Fix

RedirectSources now also processes spec.templatePatch:

  • Line-based rewrite, so Go-template directives ({{- if }}, {{ .name }}, ...) are kept untouched and the patch does not need to be valid YAML.
  • For each targetRevision: key, sibling keys of the same mapping (repoURL, chart) are collected and passed to the existing redirectSourceMap, so the repo selector and --redirect-target-revisions behave exactly as for regular sources.
  • Quoting style and trailing comments are preserved; block scalar contents (e.g. values: |) are ignored; no key is inserted when targetRevision is absent.

Tests

  • New pkg/argoapplication/patching_templatepatch_test.go covering: multiple sources inside an {{- if }} block with chart and foreign-repo sources, key ordering / quoting / trailing comments, redirectRevisions filtering, and block scalars.
  • make run-unit-tests and golangci-lint run ./... pass.
  • docs/how-it-works.md updated.

ApplicationSets defining sources in spec.templatePatch were never redirected,
so base and target renders were identical and the diff was empty.

Assisted-by: Claude Opus 5.5
@dag-andersen

Copy link
Copy Markdown
Owner

@anthonyhaussman Thanks for catching this!
My concern with rewriting templatePatch before Argo CD evaluates it is that we can miss templated repo URLs or pick up a URL from the wrong if/else branch.

I’d suggest letting Argo CD generate the Applications first, then reusing our existing source redirection logic. That means generating some unchanged ApplicationSets we could otherwise skip, but it avoids guessing what the template will produce.
I am thinking you can implement something like this: 5cc6c13

If you want a contribution please feel free to steal the code and update your PR.
Otherwise, i'll come back this PR next week and finish it :)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants