Kubernetes API:为pod添加标签

时间:2016-03-22 05:56:14

标签: kubernetes

使用命令,我可以添加标签,如下所示

kubectl label pod POD_NAME KEY1=VALUE1 我怎么能从kubernetes API那里做到这一点?

我想这可以通过PATCH /api/v1/namespaces/{namespace}/pods/{name}

来完成

这是pod.json

{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        "labels": {
            "key1": "value1"
        }
    }
}

我尝试了以下命令

KUBE_TOKEN=$(</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

它返回

{
  "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
}

1 个答案:

答案 0 :(得分:6)

将内容类型设置为application/json-patch+json,并以http://jsonpatch.org格式指定修补程序。

$ cat > patch.json <<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