applicationset icon indicating copy to clipboard operation
applicationset copied to clipboard

Cannot template syncPolicy parameters because of applicationset validation

Open jemag opened this issue 2 years ago • 16 comments

In the optics of self-serve, I would like to let each team specify their "prune" and "selfHeal" parameters from syncPolicy. To do so, I am using the following applicationset:

apiVersion: argoproj.io/v1alpha1
  kind: ApplicationSet
  metadata:
    name: XX-bootstrap
    namespace: argocd
    annotations:
      argocd.argoproj.io/sync-options: Validate=false
  spec:
    generators:
    - git:
        repoURL: "somegitrepo"
        revision: HEAD
        files:
        - path: apps/dev/**/config.yaml
    template:
      metadata:
        name: "XX-{{app.name}}"
      spec:
        project: XX
        source:
          repoURL: "somegitrepo"
          targetRevision: HEAD
          path: "{{app.path}}"
        destination:
          server: https://kubernetes.default.svc
          namespace: XX
        syncPolicy:
          automated:
              allowEmpty: true
              prune: "{{app.sync.prune}}"
              selfHeal: "{{app.sync.selfHeal}}"

The problem arises because "{{app.sync.prune}}" is not a boolean but rather a string: image

I also cannot remove the double quotes, since my applicationset passes through kustomize. Therefore using the form {{app.sync.prune}} would result in Error: map[string]interface {}(nil): yaml: invalid map key: map[string]interface {}{"app.sync.prune":""}

As you can see in the yaml above, I also tried to disable the validation using

annotations:
      argocd.argoproj.io/sync-options: Validate=false

however that did not seem to help.

jemag avatar Nov 26 '21 16:11 jemag

Have you tried omitting the quotes around the templated values? I.e.

              prune: {{app.sync.prune}}
              selfHeal: {{app.sync.selfHeal}}

Your syntax-checker will probably be red, but it might just work :-)

olvesh avatar Dec 14 '21 14:12 olvesh

As mentioned in the original post, I cannot because it will cause problems with kustomize:

I also cannot remove the double quotes, since my applicationset passes through kustomize. Therefore using the form {{app.sync.prune}} would result in Error: map[string]interface {}(nil): yaml: invalid map key: map[string]interface {}{"app.sync.prune":""}

jemag avatar Dec 14 '21 15:12 jemag

@olvesh

Have you tried omitting the quotes around the templated values?

I even tried this on plain kubectl apply - doesn't work, since it's still being converted to JSON, and during conversion {{anything here}} being failed with error like:

invalid map key: map[interface {}]interface {}{"prune":interface {}(nil)}

ekbduffy avatar Dec 29 '21 19:12 ekbduffy

Looks like now the only solution is to annotate for non-deletion each separated resource created by the application definition. Here's the doc for it

ekbduffy avatar Dec 29 '21 20:12 ekbduffy

Is there any plan to support this feature? :( it's a quite common scenario. For example:

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: bookinfo
  namespace: argocd
  finalizers:
    - resources-finalizer.argocd.argoproj.io
spec:
  generators:
    - list:
        elements:
          - environment: dev
            cluster: lab-nonprod
            prune: true
            selfHeal: true
          - environment: qa
            cluster: lab-nonprod
            prune: true
            selfHeal: false
  template:
    metadata:
      name: 'bookinfo-{{environment}}'
    spec:
      project: "bookinfo"
      source:
        repoURL: https://myrepo.com/k8s-template-deployment.git
        targetRevision: HEAD
        path: '.deploy/kustomize/overlays/{{environment}}'
      destination:
        name: '{{cluster}}'
        namespace: 'project-bookinfo-{{environment}}'
      syncPolicy:
        automated:
          prune: '{{prune}}'
          selfHeal: '{{selfHeal}}'
        syncOptions:
          - CreateNamespace=true

is Helm the only clean solution right now?

brunocascio avatar Jan 20 '22 15:01 brunocascio

Any updates this problem??

igstbagusdharmaputra avatar Feb 07 '23 19:02 igstbagusdharmaputra

any update?

vl-kp avatar Jun 13 '23 02:06 vl-kp

Any update on this issue? I need to use a template for rendering boolean values for the skipCrds field.

alita1991 avatar Jul 04 '23 16:07 alita1991

Any update on this issue? It seems unclear how to manage different syncPolicies for different applications in the same application set?

kfy-incom avatar Aug 15 '23 09:08 kfy-incom

any updates? i'm still experiencing the same issues with skipCrds key, it expects a bool :(

skipCrds: "{{skipCrds}}"

EladAviczer avatar Aug 22 '23 13:08 EladAviczer

Any updates on this? I really need to be able to specify dynamic syncPolicy in the app file for appSets.

timharris777 avatar Aug 25 '23 15:08 timharris777

This is what I did for now until this feature is ready

  • Set skipCrds flag to true for all application sets, this will ignore all the CRDs that are placed inside of the crds folder of the chart
  • Place the CRDs you want to install inside of the templates directory of the chart, where the skipCrds flag will not have any effect (for more information, check helm docs)

I hope this feature will be ready soon.

alita1991 avatar Aug 25 '23 15:08 alita1991

I have also encountered this problem, Is there any chance this will be fixed soon? Doesn't Go lang convert string to bool?

vitaly-dt avatar Sep 04 '23 05:09 vitaly-dt

I hope this feature will be ready soon

igstbagusdharmaputra avatar Sep 04 '23 05:09 igstbagusdharmaputra

Hi 👋 Any updates on this ?

thib-mary avatar Oct 04 '23 14:10 thib-mary

It seems like the problem here is that ArgoCD first validates YAML, then runs templating engine, not vise-versa. Whatever you do you can not render parts of YAML in this case if it's not a string.

ptimofee avatar Oct 09 '23 19:10 ptimofee