1

When adding a kustomize patch to a kustomization.yaml the double quotes are replaced with single quotes that lead to error

I am using the following:

kustomize edit add patch --patch "- op: add\n  path: /metadata/annotations/argocd.argoproj.io~1sync-wave\n. value: 1" --kind Deployment

is converted to

- patch: '- op: add\n  path: /metadata/annotations/argocd.argoproj.io~1sync-wave\n value: 1'
  target:
    kind: Deployment

in the kustomization.yaml

This leads to the following error when you do kustomize build

Error: trouble configuring builtin PatchTransformer with config: `
patch: ‘- op: add\n path: /metadata/annotations/argocd.argoproj.io~1sync-wave\n  value:
  1’
target:
  kind: Deployment
`: unable to parse SM or JSON patch from [- op: add\n path: /metadata/annotations/argocd.argoproj.io~1sync-wave\n  value: 1]

How do I make sure that the patch in kustomization.yaml has double quotes instead?

mbbce
  • 2,245
  • 1
  • 19
  • 31
  • 1
    why does it matter? – The Fool Feb 28 '22 at 19:05
  • I have added the error. I have worked around the problem now. – mbbce Feb 28 '22 at 19:59
  • I suspect if you were to specify the patch as a JSON string, which doesn't require embedded newlines, it would be easier to pass on the command line (I wouldn't do either: I would just edit `kustomization.yaml` myself rather than relying on `kustomize edit add`). – larsks Feb 28 '22 at 22:30
  • I just ended up using `kustomize edit add annotations` instead. There 100s of kustomization files so adding them manually was a bit of a pain that I was trying to avoid. – mbbce Mar 02 '22 at 12:18
  • @mbbce You can post this as an answer so it'll be seen by others. – moonkotte Mar 02 '22 at 17:37

2 Answers2

1

Since I had hundreds of kustomization files that needed to be updated with the annotations for ArgoCD sync-waves, I worked around the problem by using commonAnnotations instead (I believe this is the right way of doing it as well). So instead of adding a patch, I did the following:

kustomize edit add annotation argocd.argoproj.io/sync-wave:$wave --force

This will add the annotation to all objects. Where $wave was the wave number and --force overwrites the annotation if it already exists in the file.

mbbce
  • 2,245
  • 1
  • 19
  • 31
0

In my case annotations were not an option, so I had to provide the patch definition serialized as an array of ops:

kustomize edit add patch --patch '[{"op": "replace", "path": "/spec/template/spec/containers/0/ports/0/containerPort", "value": 8080}]' --kind Deployment
Demmostenes
  • 101
  • 1
  • 5