Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit 0d4f822

Browse files
authored
Merge pull request #221 from MoShitrit/m3e-issue-1005
Add load_config method which allows a more generic way to load the kubeconfig
2 parents d25434b + 6d1c8d3 commit 0d4f822

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

config/__init__.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,34 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
16+
1517
from .config_exception import ConfigException
1618
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,
1821
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

Comments
 (0)