简体   繁体   中英

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. Therefore, kubeapiserver checks body of the request, no need to have custom removing taint in Python client library.

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.

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

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?):

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM