I would like to add an annotation using json patch to my ingress. I'm trying it like this right now:
- op: add
path: "/metadata/annotations/-"
value:
nginx.ingress.kubernetes.io/auth-type: basic
The result is:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
'-':
nginx.ingress.kubernetes.io/auth-type: basic
How can this be patched correctly?
The /- syntax is used for inserting a value at the end of a list. The annotations field is a map so this syntax doesn't work in the same way.
If you want to completely replace the set of annotations in an object or if no annotations are currently set you can use:
- op: add
path: "/metadata/annotations"
value:
nginx.ingress.kubernetes.io/auth-type: basic
If there are existing annotations and you don't want to override them:
- op: add
path: "/metadata/annotations/nginx.ingress.kubernetes.io~1auth-type"
value: basic
However if you want to be able to support patching an object that may or may not have existing annotations and also not clobber other annotations you're kind of out of luck with json patches. In that case you'll need to use a strategic merge patch instead.
Thanks for your answer! Yes I would like to add annotations to an exiting map of annotations. As far as I know, the strategic merge patch does not work with an Ingress resource, that's why I used json patching.
Did you resolve it ?
Most helpful comment
The
/-syntax is used for inserting a value at the end of a list. The annotations field is a map so this syntax doesn't work in the same way.If you want to completely replace the set of annotations in an object or if no annotations are currently set you can use:
If there are existing annotations and you don't want to override them:
However if you want to be able to support patching an object that may or may not have existing annotations and also not clobber other annotations you're kind of out of luck with json patches. In that case you'll need to use a strategic merge patch instead.