<p>With command, I can add label as below</p>

<pre><code>kubectl label pod POD_NAME KEY1=VALUE1
</code></pre>

<p>How could I do that from kubernetes API?</p>

<p>I guess it can be done by <code>PATCH /api/v1/namespaces/{namespace}/pods/{name}</code></p>

<p>Here is pod.json</p>

<pre><code>{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        "labels": {
            "key1": "value1"
        }
    }
}
</code></pre>

<p>I tried with following command</p>

<pre><code>KUBE_TOKEN=$(&lt;/var/run/secrets/kubernetes.io/serviceaccount/token)
curl --request PATCH --insecure \
      --header "Authorization: Bearer $KUBE_TOKEN"  \
      --data "$(cat pod.json)" \
      https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/$POD_NAMESPACE/pods/$POD_NAME
</code></pre>

<p>And it returns </p>

<pre><code>{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {},
  "status": "Failure",
  "message": "the server responded with the status code 415 but did not return more information",
  "details": {},
  "code": 415
}
</code></pre>

<p>Set content-type to <code>application/json-patch+json</code> and specify the patch in <a href="http://jsonpatch.org" rel="noreferrer">http://jsonpatch.org</a> format. </p>

<pre><code>$ cat &gt; patch.json &lt;&lt;EOF
[ 
 { 
 "op": "add", "path": "/metadata/labels/hello", "value": "world" 
 } 
]
EOF
$ curl --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  
</code></pre>
