问题
What happened
I am trying to create custom object in kubernetes using kubernetes python client, but i am unable to do so, it would be helpful if someone can explain what i am doing wrong here
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
What you expected to happen: Custom Object found
How to reproduce it (as minimally and precisely as possible):
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()
Anything else we need to know?: I am able to use all other api including create_namespaced_custom_object() I can view this custom object using "kubectl get backup -n velero"
CRD https://gist.githubusercontent.com/talhalatifkhan/c6eba420e327cf5ef8da1087c326e0a1/raw/d030950775b87a60ecfdb4370fbd518169118d26/gistfile1.txt
CUSTOM OBJECT
https://gist.githubusercontent.com/talhalatifkhan/0537695a1f08b235cbe87d83a3f83296/raw/030cf503a33a9162251a61380105c719037b90ad/gistfile1.txt
Environment:
- Kubernetes version (v1.18.6)
- OS (Linux 20.04):
- Python version (3.8.2)
- Python client version (12.0.0a1)
回答1:
Used python 3.6 and it no longer returns status.code 404.
来源:https://stackoverflow.com/questions/63772649/404-when-trying-to-get-crd-using-python-client