How to fix 'No such file or directory: '/home/jenkins/.kube/config'' when using load_incluster_config in python openshift rest client

4/16/2019

I wrote a script that checks some secrets within an OpenShift cluster. I used the python rest-client library for Openshift and the script is executed within the cluster. But i always get IOError: [Errno 2] No such file or directory: '/home/jenkins/.kube/config'

I know that I don't have a kube config in the pod and therefore I tried to use the kubernetes.config.load_incluster_config() method to enable the in cluster config.

from kubernetes import client, config
from openshift.dynamic import DynamicClient

config.load_incluster_config()

k8s_client = config.new_client_from_config()
dyn_client = DynamicClient(k8s_client)

I would assume that it is no longer necessary to provide a kube config with the load_incluster_config call. Did someone solve the problem with the rest client and openshift in cluster execution with a service account?

I appreciate any help, thanks.

-- MartinD
kubeconfig
kubernetes
openshift
python
rest-client

2 Answers

4/16/2019

I mean, you've probably already checked this, but are sure that you're in the right directory? Because running a file from the wrong directory can cause the error of "No such file or directory".

-- JZkea
Source: StackOverflow

4/16/2019

I solved it with the following:

if os.getenv('KUBERNETES_SERVICE_HOST'):
    config.load_incluster_config()
else:
    config.load_kube_config()

dyn_client = DynamicClient(ApiClient())

ApiClient is using the default configuration.

-- MartinD
Source: StackOverflow