Kubernetes API:向Pod添加标签 [英] Kubernetes API : add label to pod

查看:669
本文介绍了Kubernetes API:向Pod添加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过命令,我可以如下添加标签

With command, I can add label as below

kubectl label pod POD_NAME KEY1=VALUE1

如何从kubernetes API中做到这一点?

How could I do that from kubernetes API?

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

这是pod.json

Here is 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/json-patch+json并在 http中指定补丁://jsonpatch.org 格式.

Set content-type to application/json-patch+json and specify the patch in http://jsonpatch.org format.

$ 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  

这篇关于Kubernetes API:向Pod添加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆