|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
| 15 | +import os |
| 16 | + |
15 | 17 | from .config_exception import ConfigException
|
16 | 18 | from .incluster_config import load_incluster_config
|
17 |
| -from .kube_config import (list_kube_config_contexts, load_kube_config, |
| 19 | +from .kube_config import (KUBE_CONFIG_DEFAULT_LOCATION, |
| 20 | + list_kube_config_contexts, load_kube_config, |
18 | 21 | load_kube_config_from_dict, new_client_from_config)
|
| 22 | + |
| 23 | + |
| 24 | +def load_config(**kwargs): |
| 25 | + """ |
| 26 | + Wrapper function to load the kube_config. |
| 27 | + It will initially try to load_kube_config from provided path, |
| 28 | + then check if the KUBE_CONFIG_DEFAULT_LOCATION exists |
| 29 | + If neither exists, it will fall back to load_incluster_config |
| 30 | + and inform the user accordingly. |
| 31 | +
|
| 32 | + :param kwargs: A combination of all possible kwargs that |
| 33 | + can be passed to either load_kube_config or |
| 34 | + load_incluster_config functions. |
| 35 | + """ |
| 36 | + if "kube_config_path" in kwargs.keys() or os.path.exists( |
| 37 | + KUBE_CONFIG_DEFAULT_LOCATION): |
| 38 | + load_kube_config(**kwargs) |
| 39 | + else: |
| 40 | + print( |
| 41 | + "kube_config_path not provided and " |
| 42 | + "default location ({0}) does not exist. " |
| 43 | + "Using inCluster Config. " |
| 44 | + "This might not work.".format(KUBE_CONFIG_DEFAULT_LOCATION)) |
| 45 | + load_incluster_config(**kwargs) |
0 commit comments