使用命令,我可以添加标签如下
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
}
Content-Type: application/merge-patch+json
对我有效。 - Mr.Wang from Next Door~
或/
的键,则必须分别将它们替换为~0
和~1
。因此,对于您的示例"path":"/metadata/labels/domain.com~1hello"
。 - Merlijn Sebrechts