简体   繁体   English

如何使用 Python 的 Kubernetes 库删除节点污点

[英]How to delete a node taint using Python's Kubernetes library

Set the stain like this:像这样设置污点:

    v3.patch_node('nodename',
                  {"spec": {"taints": [{"effect": "NoSchedule", "key": "test", "value": "1",'tolerationSeconds': '300'}]}}```

however.
How to remove tains?

Node 对象上没有什么特别的、标准的更新或补丁调用。

Client libraries are used to interact with kubeapiserver.客户端库用于与 kubeapiserver 交互。 Therefore, kubeapiserver checks body of the request, no need to have custom removing taint in Python client library.因此,kubeapiserver 检查请求的主体,无需在 Python 客户端库中自定义删除污点。

I think you can do it by calling我想你可以通过打电话来做到

v3.patch_node('cn-shanghai.10.10.10.249',
                  {"spec": {"taints": [{"effect": "NoSchedule-", "key": "test", "value": "1","tolerationSeconds": "300"}]}}

An example can be found in python-client examples repository.可以在 python-client 示例存储库中找到一个示例。

from kubernetes import client, config


def main():
    config.load_kube_config()

    api_instance = client.CoreV1Api()

    body = {
        "metadata": {
            "labels": {
                "foo": "bar",
                "baz": None}
        }
    }

    # Listing the cluster nodes
    node_list = api_instance.list_node()

    print("%s\t\t%s" % ("NAME", "LABELS"))
    # Patching the node labels
    for node in node_list.items:
        api_response = api_instance.patch_node(node.metadata.name, body)
        print("%s\t%s" % (node.metadata.name, node.metadata.labels))


if __name__ == '__main__':
    main()

Reference: https://github.com/kubernetes-client/python/blob/c3f1a1c61efc608a4fe7f103ed103582c77bc30a/examples/node_labels.py参考: https : //github.com/kubernetes-client/python/blob/c3f1a1c61efc608a4fe7f103ed103582c77bc30a/examples/node_labels.py

This was pretty non-intuitive to me, but here's how I accomplished this.这对我来说非常不直观,但这是我如何完成的。

def taint_node(context, node_name):
    kube_client = setup_kube_client(context)
    taint_patch = {"spec": {"taints": [{"effect": "NoSchedule", "key": "test", "value": "True"}]}}
    return kube_client.patch_node(node_name, taint_patch)

def untaint_node(context, node_name):
    kube_client = setup_kube_client(context)
    remove_taint_patch = {"spec": {"taints": []}}
    return kube_client.patch_node(node_name, remove_taint_patch)

That worked for me, but it removes ALL taints, which is maybe not what you want to do.这对我有用,但它消除了所有污点,这可能不是您想要做的。

I tried the following:我尝试了以下方法:

def untaint_node(context, node_name):
    kube_client = setup_kube_client(context)
    remove_taint_patch = {"spec": {"taints": [{"effect": "NoSchedule-", "key": "test", "value": "True"}]}}
    return kube_client.patch_node(node_name, remove_taint_patch)

but encountered server side validation preventing it (because the effect isn't in the collection of supported values):但遇到服务器端验证阻止它(因为效果不在支持值的集合中):

kubernetes.client.exceptions.ApiException: (422)
Reason: Unprocessable Entity
HTTP response headers: HTTPHeaderDict({'Audit-Id': 'bfbad6e1-f37c-4090-898b-b2b9c5500425', 'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'X-Kubernetes-Pf-Flowschema-Uid': '7c028f53-f0a4-46bd-b400-68641158da78', 'X-Kubernetes-Pf-Prioritylevel-Uid': 'ef92e801-ce03-4abb-a607-20921bf82547', 'Date': 'Sat, 18 Sep 2021 02:45:37 GMT', 'Content-Length': '759'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Node \"aks-agentpool-33938206-vmss000000\" is invalid: metadata.taints[0].effect: Unsupported value: \"NoSchedule-\": supported values: \"NoSchedule\", \"PreferNoSchedule\", \"NoExecute\"","reason":"Invalid","details":{"name":"aks-agentpool-33938206-vmss000000","kind":"Node","causes":[{"reason":"FieldValueNotSupported","message":"Unsupported value: \"NoSchedule-\": supported values: \"NoSchedule\", \"PreferNoSchedule\", \"NoExecute\"","field":"metadata.taints[0].effect"},{"reason":"FieldValueNotSupported","message":"Unsupported value: \"NoSchedule-\": supported values: \"NoSchedule\", \"PreferNoSchedule\", \"NoExecute\"","field":"metadata.taints[0].effect"}]},"code":422}

Finally, if you need to remove a specific taint, you can always shell out to kubectl (though that's kinda cheating, huh?):最后,如果您需要删除特定的污点,您可以随时使用 kubectl(尽管这有点作弊,是吧?):

def untaint_node_with_cmd(context, node_name):
    cmd_env = os.environ.copy()
    child = subprocess.Popen(['kubectl', 'taint', 'nodes', node_name, 'test=True:NoSchedule-', '--context', context], env=cmd_env)
    exit_code = child.wait()
    return exit_code

Sadly, it doesn't look like this issue has gotten much love in the k8s python client repo.可悲的是,这个问题在 k8s python 客户端存储库中看起来并没有受到很多人的喜爱。 https://github.com/kubernetes-client/python/issues/161 https://github.com/kubernetes-client/python/issues/161

One more better way to untainted a particular taint.清除特定污点的另一种更好的方法。 By doing this way other taints will not get removed.only a particular taint will ve untainted.通过这种方式,其他污点不会被移除。只有特定的污点不会被污染。

def untaint_node(context, node_name, taint_key):
    Kube_client = setup_kube_client(context)
    node = Kube_client.list_nodes(field_selector={"metadata.name" : node_name}).items[0]
    taints = node.spec.taints
    filtered_taints = list(filter(lambda x: x.key != taint_key, taints))
    body = {"spec": {"taints": filtered_taints}}
    return kube_client.patch_node(node_name, body)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 python kubernetes-client 库修补 Kubernetes 节点 - Cannot patch Kubernetes node using python kubernetes-client library 如何使用 python 删除 GKE(Google Kubernetes 引擎)集群? - How to delete GKE (Google Kubernetes Engine) cluster using python? 使用python dpath库删除嵌套对象 - Delete nested objects using python dpath library 如何使用 Python 的 Pygame 库更改 class 中的参数值 - How to change the value of parameters in a class using Python's Pygame library 使用 Python 的 docx 库时如何配置页眉(和页脚)的“顶部页眉”值? - How to configure header's (and footer's) “Header from Top” value when using Python's docx library? 在 Python 的 Socket 库中使用 ACK 方法 - Using the ACK method with Python's Socket library 如何使用next删除列表的最后一个节点 - How to delete last Node of the list using next 如何在python中使用spotify的annoy库? - How to use the spotify's annoy library in python? 使用 Python 的 Requests 库访问 Airtable 的分页 - Accessing Airtable's Pagination Using Python's Requests Library 如何使用Python 3的标准库测试不像字符串的序列 - How to test for sequences that are not string-like using Python 3's standard library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM