发生了什么
我正在尝试使用 kubernetes python 客户端在 kubernetes 中创建自定义对象,但我无法这样做,如果有人可以在这里解释我做错了什么会很有帮助
Traceback (most recent call last):
File "/home/talha/PycharmProjects/doosra/tasks/cluster_tasks.py", line 585, in <module>
main()
File "/home/talha/PycharmProjects/doosra/tasks/cluster_tasks.py", line 574, in main
resource = api.get_namespaced_custom_object(
File "/home/talha/PycharmProjects/venv/lib/python3.8/site-packages/kubernetes/client/api/custom_objects_api.py", line 1484, in get_namespaced_custom_object
return self.get_namespaced_custom_object_with_http_info(group, version, namespace, plural, name, **kwargs) # noqa: E501
File "/home/talha/PycharmProjects/venv/lib/python3.8/site-packages/kubernetes/client/api/custom_objects_api.py", line 1591, in get_namespaced_custom_object_with_http_info
return self.api_client.call_api(
File "/home/talha/PycharmProjects/venv/lib/python3.8/site-packages/kubernetes/client/api_client.py", line 348, in call_api
return self.__call_api(resource_path, method,
File "/home/talha/PycharmProjects/venv/lib/python3.8/site-packages/kubernetes/client/api_client.py", line 180, in __call_api
response_data = self.request(
File "/home/talha/PycharmProjects/venv/lib/python3.8/site-packages/kubernetes/client/api_client.py", line 373, in request
return self.rest_client.GET(url,
File "/home/talha/PycharmProjects/venv/lib/python3.8/site-packages/kubernetes/client/rest.py", line 239, in GET
return self.request("GET", url,
File "/home/talha/PycharmProjects/venv/lib/python3.8/site-packages/kubernetes/client/rest.py", line 233, in request
raise ApiException(http_resp=r)
kubernetes.client.exceptions.ApiException: (404)
Reason: Not Found
HTTP response headers: HTTPHeaderDict({'Cache-Control': 'no-cache, private', 'Content-Type': 'text/plain; charset=utf-8', 'X-Content-Type-Options': 'nosniff', 'Date': 'Mon, 07 Sep 2020 00:41:25 GMT', 'Content-Length': '19'})
HTTP response body: 404 page not found
您期望发生的事情:找到自定义对象
如何重现它(尽可能最小和精确):
from kubernetes import client
from kubernetes.client import Configuration
from kubernetes.config import kube_config
class K8s(object):
def __init__(self, configuration_json):
self.configuration_json = configuration_json
# self._configuration_json = None
@property
def client(self):
k8_loader = kube_config.KubeConfigLoader(self.configuration_json)
call_config = type.__call__(Configuration)
k8_loader.load_and_set(call_config)
Configuration.set_default(call_config)
return client
def main():
cls = {
"kind": "Config",
"users": [
{
"name": "xyx",
"user": {
"client-key-data": "daksjdkasjdklj==",
"client-certificate-data": "skadlkasldk"
}
}
],
"clusters": [
{
"name": "cluster1",
"cluster": {
"server": "https://cyx.cloud.ibm.com",
"certificate-authority-data": "sldklaksdl="
}
}
],
"contexts": [
{
"name": "cluster1/admin",
"context": {
"user": "admin",
"cluster": "cluster1",
"namespace": "default"
}
}
],
"apiVersion": "v1",
"preferences": {},
"current-context": "cluster1/admin"
}
config_trgt = K8s(configuration_json=cls).client
api = config_trgt.CustomObjectsApi()
resource = api.get_namespaced_custom_object(
group="velero.io",
version="v1",
namespace="velero",
plural="backups",
name="apple"
)
print(resource)
if __name__ == "__main__":
main()
还有什么我们需要知道的吗?:我可以使用所有其他 api,包括 create_namespaced_custom_object() 我可以使用“kubectl get backup -n velero”查看这个自定义对象
自定义对象
环境:
- Kubernetes 版本(v1.18.6)
- 操作系统(Linux 20.04):
- Python 版本 (3.8.2)
- Python 客户端版本 (12.0.0a1)