@@ -688,31 +688,43 @@ def save_config(self, path):
688688 yaml .safe_dump (self .config_files [path ], f ,
689689 default_flow_style = False )
690690
691-
692691def _get_kube_config_loader_for_yaml_file (
693692 filename , persist_config = False , ** kwargs ):
694-
695- kcfg = KubeConfigMerger (filename )
696- if persist_config and 'config_persister' not in kwargs :
697- kwargs ['config_persister' ] = kcfg .save_changes
698-
699- if kcfg .config is None :
700- raise ConfigException (
701- 'Invalid kube-config file. '
702- 'No configuration found.' )
703-
704- return KubeConfigLoader (
705- config_dict = kcfg .config ,
706- config_base_path = None ,
693+ return _get_kube_config_loader (
694+ filename = filename ,
695+ persist_config = persist_config ,
707696 ** kwargs )
708697
698+ def _get_kube_config_loader (
699+ filename = None ,
700+ config_dict = None ,
701+ persist_config = False ,
702+ ** kwargs ):
703+ if config_dict is None :
704+ kcfg = KubeConfigMerger (filename )
705+ if persist_config and 'config_persister' not in kwargs :
706+ kwargs ['config_persister' ] = kcfg .save_changes
707+
708+ if kcfg .config is None :
709+ raise ConfigException (
710+ 'Invalid kube-config file. '
711+ 'No configuration found.' )
712+ return KubeConfigLoader (
713+ config_dict = kcfg .config ,
714+ config_base_path = None ,
715+ ** kwargs )
716+ else :
717+ return KubeConfigLoader (
718+ config_dict = config_dict ,
719+ config_base_path = None ,
720+ ** kwargs )
709721
710722def list_kube_config_contexts (config_file = None ):
711723
712724 if config_file is None :
713725 config_file = KUBE_CONFIG_DEFAULT_LOCATION
714726
715- loader = _get_kube_config_loader_for_yaml_file ( config_file )
727+ loader = _get_kube_config_loader ( filename = config_file )
716728 return loader .list_contexts (), loader .current_context
717729
718730
@@ -734,8 +746,8 @@ def load_kube_config(config_file=None, context=None,
734746 if config_file is None :
735747 config_file = KUBE_CONFIG_DEFAULT_LOCATION
736748
737- loader = _get_kube_config_loader_for_yaml_file (
738- config_file , active_context = context ,
749+ loader = _get_kube_config_loader (
750+ filename = config_file , active_context = context ,
739751 persist_config = persist_config )
740752
741753 if client_configuration is None :
@@ -745,6 +757,36 @@ def load_kube_config(config_file=None, context=None,
745757 else :
746758 loader .load_and_set (client_configuration )
747759
760+ def load_kube_config_from_dict (config_dict , context = None ,
761+ client_configuration = None ,
762+ persist_config = True ):
763+ """Loads authentication and cluster information from config_dict file
764+ and stores them in kubernetes.client.configuration.
765+
766+ :param config_dict: Takes the config file as a dict.
767+ :param context: set the active context. If is set to None, current_context
768+ from config file will be used.
769+ :param client_configuration: The kubernetes.client.Configuration to
770+ set configs to.
771+ :param persist_config: If True, config file will be updated when changed
772+ (e.g GCP token refresh).
773+ """
774+
775+ if config_dict is None :
776+ raise ConfigException (
777+ 'Invalid kube-config dict. '
778+ 'No configuration found.' )
779+
780+ loader = _get_kube_config_loader (
781+ config_dict = config_dict , active_context = context ,
782+ persist_config = persist_config )
783+
784+ if client_configuration is None :
785+ config = type .__call__ (Configuration )
786+ loader .load_and_set (config )
787+ Configuration .set_default (config )
788+ else :
789+ loader .load_and_set (client_configuration )
748790
749791def new_client_from_config (
750792 config_file = None ,
0 commit comments