Kustomize: patchesJson6902 add annotation

Created on 10 Aug 2019  路  3Comments  路  Source: kubernetes-sigs/kustomize

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?

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:

- 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.

All 3 comments

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 ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Liujingfang1 picture Liujingfang1  路  4Comments

nicks picture nicks  路  3Comments

mgoodness picture mgoodness  路  4Comments

davidknezic picture davidknezic  路  3Comments

yujunz picture yujunz  路  5Comments