Skip to content

Commit 0c662bb

Browse files
committed
Adding load_config wrapper method to have a more generic way of initializing the client config
1 parent 7199c14 commit 0c662bb

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

config/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,22 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
1516
from .config_exception import ConfigException
1617
from .incluster_config import load_incluster_config
1718
from .kube_config import (list_kube_config_contexts, load_kube_config,
18-
load_kube_config_from_dict, new_client_from_config)
19+
load_kube_config_from_dict, new_client_from_config, KUBE_CONFIG_DEFAULT_LOCATION)
20+
21+
22+
def load_config(**kwargs):
23+
"""
24+
Wrapper function to load the kube_config.
25+
It will initially try to load_kube_config from provided path, then check if the KUBE_CONFIG_DEFAULT_LOCATION exists
26+
If neither exists- it will fall back to load_incluster_config and inform the user accordingly.
27+
"""
28+
if "kube_config_path" in kwargs.keys() or os.path.exists(KUBE_CONFIG_DEFAULT_LOCATION):
29+
load_kube_config(**kwargs)
30+
else:
31+
print(f"kube_config_path not provided and default location ({KUBE_CONFIG_DEFAULT_LOCATION}) does not exist. "
32+
"Using inCluster Config. This might not work.")
33+
load_incluster_config(**kwargs)

0 commit comments

Comments
 (0)