Search code examples
pythonkubernetesopenshiftrest-clientkubeconfig

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


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.


Solution

  • 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.