Skip to content

Commit ab51510

Browse files
committed
Adding ability to pass kube_config as a dict.
1 parent 49ec060 commit ab51510

1 file changed

Lines changed: 54 additions & 3 deletions

File tree

config/kube_config.py

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,13 +706,34 @@ def _get_kube_config_loader_for_yaml_file(
706706
config_base_path=None,
707707
**kwargs)
708708

709+
def _get_kube_config_loader(
710+
filename=None,config_dict=None, persist_config=False, **kwargs):
711+
712+
if (config_dict is None):
713+
kcfg = KubeConfigMerger(filename)
714+
if persist_config and 'config_persister' not in kwargs:
715+
kwargs['config_persister'] = kcfg.save_changes
716+
717+
if kcfg.config is None:
718+
raise ConfigException(
719+
'Invalid kube-config file. '
720+
'No configuration found.')
721+
return KubeConfigLoader(
722+
config_dict=kcfg.config,
723+
config_base_path=None,
724+
**kwargs)
725+
else:
726+
return KubeConfigLoader(
727+
config_dict=config_dict,
728+
config_base_path=None,
729+
**kwargs)
709730

710731
def list_kube_config_contexts(config_file=None):
711732

712733
if config_file is None:
713734
config_file = KUBE_CONFIG_DEFAULT_LOCATION
714735

715-
loader = _get_kube_config_loader_for_yaml_file(config_file)
736+
loader = _get_kube_config_loader(filename=config_file)
716737
return loader.list_contexts(), loader.current_context
717738

718739

@@ -734,8 +755,8 @@ def load_kube_config(config_file=None, context=None,
734755
if config_file is None:
735756
config_file = KUBE_CONFIG_DEFAULT_LOCATION
736757

737-
loader = _get_kube_config_loader_for_yaml_file(
738-
config_file, active_context=context,
758+
loader = _get_kube_config_loader(
759+
filename=config_file, active_context=context,
739760
persist_config=persist_config)
740761

741762
if client_configuration is None:
@@ -745,6 +766,36 @@ def load_kube_config(config_file=None, context=None,
745766
else:
746767
loader.load_and_set(client_configuration)
747768

769+
def load_kube_config_from_dict(config_dict, context=None,
770+
client_configuration=None,
771+
persist_config=True):
772+
"""Loads authentication and cluster information from kube-config file
773+
and stores them in kubernetes.client.configuration.
774+
775+
:param config_dict: Takes the config file as a dict.
776+
:param context: set the active context. If is set to None, current_context
777+
from config file will be used.
778+
:param client_configuration: The kubernetes.client.Configuration to
779+
set configs to.
780+
:param persist_config: If True, config file will be updated when changed
781+
(e.g GCP token refresh).
782+
"""
783+
784+
if config_dict is None:
785+
raise ConfigException(
786+
'Invalid kube-config dict. '
787+
'No configuration found.')
788+
789+
loader = _get_kube_config_loader(
790+
config_dict=config_dict, active_context=context,
791+
persist_config=persist_config)
792+
793+
if client_configuration is None:
794+
config = type.__call__(Configuration)
795+
loader.load_and_set(config)
796+
Configuration.set_default(config)
797+
else:
798+
loader.load_and_set(client_configuration)
748799

749800
def new_client_from_config(
750801
config_file=None,

0 commit comments

Comments
 (0)