Add pod annotation through Kubernetes REST API

4/25/2018

I can add labels to a pod as described here

But no luck to create annotations likewise

 $ KUBE_TOKEN=$(</var/run/secrets/kubernetes.io/serviceaccount/token) 

 $ cat > patch.json <<EOF
        ]
         {
         "op": "add", "path": "/metadata/annotations/test", "value": "world"
         }
        ]
        EOF

$  curl -sSk -H "Authorization: Bearer $KUBE_TOKEN" --request PATCH --data "$(cat patch.json)" -H "Content-Type:application/json-patch+json" https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/$POD_NAMESPACE/pods/$POD_NAME

the response is:

    {
      "kind": "Status",
      "apiVersion": "v1",
      "metadata": {

      },
      "status": "Failure",
      "message": "jsonpatch add operation does not apply: doc is missing path: /metadata/annotations/test",
      "code": 500
    }
-- mosquitos
kubernetes

2 Answers

5/20/2019

The error is related to non-existing path.

    [
     {
     "op": "add", "path": "/metadata/annotations/", "value": { "test" : "world" }
     }
    ]

Check out https://tools.ietf.org/html/rfc6902#section-4.1

-- user3337548
Source: StackOverflow

4/25/2018

AFAIR, you cannot add new annotations on existing resources. You can only update existing ones.

-- iocanel
Source: StackOverflow