Argo CD ApplicationSet配置:Dev自动同步,Prod手动同步
解决Argo CD ApplicationSet同步策略的模板语法错误
你的错误源于Go模板的{{- trim标记破坏了YAML结构,当条件满足时,automated字段会被错误拼接在上一行末尾,引发YAML解析失败。
修正后的完整配置
apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: my-application-deployment spec: goTemplate: true goTemplateOptions: ["missingkey=error"] generators: - list: elements: - env: develop namespace: dev-my-service sync: true - env: prod namespace: my-service sync: false template: metadata: name: 'my-service-{{.env}}' spec: project: default source: repoURL: 'https://example.com/my-repo.git' targetRevision: develop path: 'my-service' helm: valueFiles: - 'values-{{.env}}.yaml' destination: name: in-cluster namespace: '{{.namespace}}' syncPolicy: syncOptions: - CreateNamespace=true {{ if .sync }} automated: prune: true selfHeal: true {{ end }}
错误原因解析
你原配置中使用了{{- if .sync }},其中的{{-会删除模板标签左侧的所有空白字符(包括换行),导致渲染后的YAML出现语法错误:
syncOptions: - CreateNamespace=trueautomated: prune: true selfHeal: true
这种拼接后的内容不符合YAML语法规范,因此触发了could not find expected ':'的错误。
关键修改点
将{{- if .sync }}和{{- end }}中的减号(-)移除,保留模板标签前后的换行与缩进,确保渲染后的YAML结构正确:
- 当
sync: true时,automated字段会被正确添加到syncPolicy下,启用自动同步 - 当
sync: false时,automated字段不会被渲染,自动同步保持禁用状态
额外优化(可选)
如果希望避免渲染后出现多余空行,可以仅在标签右侧使用减号来trim后续空白:
syncPolicy: syncOptions: - CreateNamespace=true {{ if .sync -}} automated: prune: true selfHeal: true {{- end }}
这种写法会保留automated字段的正确缩进,同时去掉条件块前后的多余空行。
内容的提问来源于stack exchange,提问作者Sergey Bizinow
相关产品推荐
相关产品推荐

