Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Proposal: Allow variable interpolation in generator values field #371

Open
Sinjo opened this issue Sep 16, 2021 · 1 comment
Open

Proposal: Allow variable interpolation in generator values field #371

Sinjo opened this issue Sep 16, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@Sinjo
Copy link

Sinjo commented Sep 16, 2021

Context

While using the cluster generator, we recently ran into a situation where being able to template the contents of the values map based on the parameters of each cluster.

The specific situation was that we wanted to make it possible for devs to opt into deploying an environment-specific image from a branch, somewhat like this example from the docs, but where the environment and branch are per-developer.

For anyone not opted in, they hit a different selector and fall back to images built from the main branch.

Current workaround

Ideally, we would be able to construct the image URL as a value based on the cluster parameters (we're using the environment name for it). That would make it easy to supply a different, hardcoded value of main for any deployments that aren't opted into environment-specific images.

The workaround we're currently using is to have different generator templates on the two selectors, which leads to a fair amount of duplication due to the way the merge algorithm works.

Proposal

We would like to add the same parameter templating that's available in in the template itself to the map values in the values key of the selector. Running with the example from the cluster generator docs, it would look something like:

spec:
  generators:
  - clusters:
      selector:
        matchLabels:
          type: 'dev'
      # A key-value map for arbitrary parameters
      values: 
        revision: "dev-{{metadata.labels.environment}}" # dev clusters use per-environment branches
  - clusters:
      selector:
        matchLabels:
          type: 'staging'
      # A key-value map for arbitrary parameters
      values: 
        revision: HEAD # staging clusters use HEAD branch
  - clusters:
      selector:
        matchLabels:
          type: 'production'
      values: 
        # production uses a different revision value, for 'stable' branch
        revision: stable
  template:
    metadata:
      name: '{{name}}-guestbook'
    spec:
      project: "default"
      source:
        repoURL: https://github.com/argoproj/argocd-example-apps/
        # The cluster values field for each generator will be substituted here:
        targetRevision: '{{values.revision}}'
        path: guestbook
      destination:
        server: '{{server}}'
        namespace: guestbook        

If you're on board with the idea, I'm happy to go ahead and put a PR together that adds this functionality to the controller!

@jgwest jgwest added the enhancement New feature or request label Sep 29, 2021
@Sinjo
Copy link
Author

Sinjo commented Nov 2, 2021

Hey, sorry for the bump. Just want to check if you're on board with this before I write code for it.

@jgwest can I take the addition of the enhancement label as a 👍🏻?

blakepettersson added a commit to blakepettersson/argo-cd that referenced this issue Apr 29, 2022
Allow the interpolation of `values` found in the cluster generator.
This allows interpolation of `{{name}}`, `{{server}}`,
`{{metadata.labels.*}}` and `{{metadata.annotations.*}}`. See
argoproj/applicationset#371.

This interpolation could potentially be extended to the list and
duck-type generators if desired.
blakepettersson added a commit to blakepettersson/argo-cd that referenced this issue Jun 24, 2022
Allow the interpolation of `values` found in the cluster generator.
This allows interpolation of `{{name}}`, `{{server}}`,
`{{metadata.labels.*}}` and `{{metadata.annotations.*}}`. See
argoproj/applicationset#371.

This interpolation could potentially be extended to the list and
duck-type generators if desired.

Signed-off-by: Blake Pettersson <blake.pettersson@gmail.com>
crenshaw-dev pushed a commit to argoproj/argo-cd that referenced this issue Jun 27, 2022
* feat: allow interpolation of generator values

Allow the interpolation of `values` found in the cluster generator.
This allows interpolation of `{{name}}`, `{{server}}`,
`{{metadata.labels.*}}` and `{{metadata.annotations.*}}`. See
argoproj/applicationset#371.

This interpolation could potentially be extended to the list and
duck-type generators if desired.

Signed-off-by: Blake Pettersson <blake.pettersson@gmail.com>

* docs: add values interpolation usage instructions

Add a basic example of how values interpolation can be used with the
cluster generator.

Signed-off-by: Blake Pettersson <blake.pettersson@gmail.com>

* fix: remove billion-laughs attack vector

The previous implementation was vulnerable to a billion-laughs attack,
where someone could interpolate values based upon other values,
something like:

```yaml

values:
  lol1: lol
  lol2: '{{values.lol1}}{{values.lol1}}' #
  lol3: '{{values.lol2}}{{values.lol2}}{{values.lol2}}{{values.lol2}}'
```

To counteract that, instead of directly manipulating the `params` map,
we create a map to keep track of the interpolated values, and only
template the values which have been previously whitelisted. Once we go
through all the values, we then merge the interpolated values map back
to the `params` map.

Signed-off-by: Blake Pettersson <blake.pettersson@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants