openshift/openshift-restclient-python

kubernetes.client.rest.ApiException: (401) Reason: Unauthorized

zhangzhidao opened this issue · 4 comments

I refer to the documentation ,but the tip kubernetes.client.rest.ApiException: (401) Reason: Unauthorized,why?
tip: k8s.config = /root/.kube/config
`
from kubernetes import client, config
from openshift.dynamic import DynamicClient

k8s_client = config.new_client_from_config('k8s.config')
dyn_client = DynamicClient(k8s_client)
Traceback (most recent call last):
File "", line 1, in
File "openshift/dynamic/client.py", line 112, in init
self.__discoverer = discoverer(self, cache_file)
File "openshift/dynamic/client.py", line 778, in init
Discoverer.init(self, client, cache_file)
File "openshift/dynamic/client.py", line 609, in init
self.__init_cache()
File "openshift/dynamic/client.py", line 624, in __init_cache
self._load_server_info()
File "openshift/dynamic/client.py", line 696, in _load_server_info
self._cache['version'] = {'kubernetes': load_json(self.client.request('get', '/version'))}
File "openshift/dynamic/client.py", line 285, in request
_return_http_data_only=params.get('_return_http_data_only', True)
File "/usr/local/lib/python2.7/site-packages/kubernetes/client/api_client.py", line 334, in call_api
_return_http_data_only, collection_formats, _preload_content, _request_timeout)
File "/usr/local/lib/python2.7/site-packages/kubernetes/client/api_client.py", line 168, in __call_api
_request_timeout=_request_timeout)
File "/usr/local/lib/python2.7/site-packages/kubernetes/client/api_client.py", line 355, in request
headers=headers)
File "/usr/local/lib/python2.7/site-packages/kubernetes/client/rest.py", line 231, in GET
query_params=query_params)
File "/usr/local/lib/python2.7/site-packages/kubernetes/client/rest.py", line 222, in request
raise ApiException(http_resp=r)
kubernetes.client.rest.ApiException: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({'Date': 'Fri, 02 Aug 2019 02:17:46 GMT', 'Content-Length': '129', 'Content-Type': 'application/json', 'Cache-Control': 'no-store'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Unauthorized","reason":"Unauthorized","code":401}
`

My OKD version is 3.11, thanks

k8s_client = config.new_client_from_config('k8s.config')

did you mean to quote k8s.config as you have shown here? I believe that would make it attempt to create a DynamicClient using the KubeConfig at path 'k8s.config'.

Thanks, my friend, I has resolve it, I have two master node . The other master /root/.kube/config can

I refer to the documentation ,but the tip kubernetes.client.rest.ApiException: (401) Reason: Unauthorized,why?
tip: k8s.config = /root/.kube/config
`
from kubernetes import client, config
from openshift.dynamic import DynamicClient

k8s_client = config.new_client_from_config('k8s.config')
dyn_client = DynamicClient(k8s_client)
Traceback (most recent call last):
File "", line 1, in
File "openshift/dynamic/client.py", line 112, in init
self.__discoverer = discoverer(self, cache_file)
File "openshift/dynamic/client.py", line 778, in init
Discoverer.init(self, client, cache_file)
File "openshift/dynamic/client.py", line 609, in init
self.__init_cache()
File "openshift/dynamic/client.py", line 624, in __init_cache
self._load_server_info()
File "openshift/dynamic/client.py", line 696, in _load_server_info
self._cache['version'] = {'kubernetes': load_json(self.client.request('get', '/version'))}
File "openshift/dynamic/client.py", line 285, in request
_return_http_data_only=params.get('_return_http_data_only', True)
File "/usr/local/lib/python2.7/site-packages/kubernetes/client/api_client.py", line 334, in call_api
_return_http_data_only, collection_formats, _preload_content, _request_timeout)
File "/usr/local/lib/python2.7/site-packages/kubernetes/client/api_client.py", line 168, in __call_api
_request_timeout=_request_timeout)
File "/usr/local/lib/python2.7/site-packages/kubernetes/client/api_client.py", line 355, in request
headers=headers)
File "/usr/local/lib/python2.7/site-packages/kubernetes/client/rest.py", line 231, in GET
query_params=query_params)
File "/usr/local/lib/python2.7/site-packages/kubernetes/client/rest.py", line 222, in request
raise ApiException(http_resp=r)
kubernetes.client.rest.ApiException: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({'Date': 'Fri, 02 Aug 2019 02:17:46 GMT', 'Content-Length': '129', 'Content-Type': 'application/json', 'Cache-Control': 'no-store'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Unauthorized","reason":"Unauthorized","code":401}
`

It could be your Kubernetes version... Just update your kubernetes version on your computer.