mnubo / kubernetes-py

A python module for Kubernetes.
Apache License 2.0
123 stars 46 forks source link

Support for multiple config files #146

Open varac opened 5 years ago

varac commented 5 years ago

I need to deal with multiple clusters. Instead of merging all cluster configs into one file I maintain them in seperate config files, and export my KUBECOFIG env variable like this:

 ~ $ env | grep KUBE
KUBECONFIG=/home/varac/.kube/admin@cluster1.example.org.yml:/home/varac/.kube/admin@cluster2.example.org.yml

This is one official way of organizing your cluster configs with kubectl and works with kubectl really fine for me, i.e.

 ~ $ kubectl config get-clusters
NAME
cluster1
cluster2

However, kubernets-py fails to deal with multiple config files referenced in KUBECONFIG env var:

In [1]: from kubernetes_py import K8sConfig

In [2]: cfg_default = K8sConfig()
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-2-4b42c288a7cf> in <module>()
----> 1 cfg_default = K8sConfig()

/home/varac/.local/lib/python3.6/site-packages/kubernetes_py/K8sConfig.py in __init__(self, kubeconfig, api_host, auth, cert, namespace, pull_secret, token, version)
     63         self.version = None
     64 
---> 65         self._init_with_defaults()
     66 
     67         if kubeconfig is None:

/home/varac/.local/lib/python3.6/site-packages/kubernetes_py/K8sConfig.py in _init_with_defaults(self)
    138         kubeconfig = os.getenv(KUBECONFIG_ENV_VAR, None)
    139         if kubeconfig is not None:
--> 140             self._read_config(filename=kubeconfig)
    141             return
    142 

/home/varac/.local/lib/python3.6/site-packages/kubernetes_py/K8sConfig.py in _read_config(self, filename)
    171 
    172         if not isfile(filename):
--> 173             raise IOError('K8sConfig: kubeconfig: [ {0} ] doesn\'t exist.'.format(filename))
    174         try:
    175             with open(filename, 'r') as stream:

OSError: K8sConfig: kubeconfig: [ 
/home/varac/.kube/admin@cluster1.example.org.yml:/home/varac/.kube/admin@cluster2.example.org.yml ] doesn't exist.
sebastienc commented 5 years ago

I'll have a look at it. It could be pretty useful to support this.

varac commented 5 years ago

See https://github.com/kubernetes-client/python/issues/723 for the same issue.