Kubernetes Python客户端API create_namespaced_binding()方法显示target.name:必需的值错误

问题描述 投票:0回答:1

我正在使用Kubernetes Python Client to implement来实现自定义调度程序。这是调度程序的代码。

from kubernetes import client, config, watch
from kubernetes.client.rest import ApiException
config.load_kube_config()
v1 = client.CoreV1Api()

scheduler_name = 'custom-scheduler-test'

def nodes_available():
    ready_nodes = []
    for n in v1.list_node().items:
        for status in n.status.conditions:
            if status.status == 'True' and status.type == 'Ready':
                ready_nodes.append(n.metadata.name)
    return ready_nodes

def scheduler(name, node, namespace='default'):
    body = client.V1ConfigMap()
    target = client.V1ObjectReference()
    target.kind = 'Node'
    target.apiVersion = 'v1'
    target.name = node
    meta = client.V1ObjectMeta()
    meta.name = name
    body.target = target
    body.metadata = meta
    return v1.create_namespaced_binding(namespace, body)

def main():
    w = watch.Watch()
    for event in w.stream(v1.list_namespaced_pod, 'default'):
        if event['object'].status.phase == 'Pending' and event['object'
                ].spec.scheduler_name == scheduler_name:
            try:
                res = scheduler(event['object'].metadata.name,random.choice(nodes_available()))
            except ApiException as e:
                print ("Exception when calling CoreV1Api->create_namespaced_binding: %s\n" % e)

if __name__ == '__main__':
    main()

此代码以前适用于Kubernetes 1.6,但现在我有1.7,它显示target.name: Required value。有任何纠正错误的线索吗?

错误消息

Exception when calling CoreV1Api->create_namespaced_binding: (500)
Reason: Internal Server Error
HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json', 'Date': 'Wed, 06 Jun 2018 20:55:04 GMT', 'Content-Length': '120'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"target.name: Required value","code":500} 

更新于2018-06-08

如果我使用body = client.V1Binding()而不是body = client.V1ConfigMap(),则会显示以下错误消息

 File "/usr/lib/python2.7/site-packages/kubernetes/client/models/v1_binding.py", line 64, in __init__
    self.target = target
  File "/usr/lib/python2.7/site-packages/kubernetes/client/models/v1_binding.py", line 156, in target
    raise ValueError("Invalid value for `target`, must not be `None`")
ValueError: Invalid value for `target`, must not be `None`
python api kubernetes scheduler
1个回答
0
投票

我在使用python3时遇到了同样的问题,我只是通过键入以下内容来卸载了kubernetes库

 pip3 uninstall kubernetes 

然后我通过键入以下命令安装了版本2.0.0

 pip install kubernetes==2.0.0

验证已成功安装:

 pip3 freeze | grep kubernetes
© www.soinside.com 2019 - 2024. All rights reserved.