Adding ability to pass kube_config as a dict.
@@ -706,13 +706,34 @@ def _get_kube_config_loader_for_yaml_file(
config_base_path=None,
**kwargs)
+def _get_kube_config_loader(
+ filename=None,config_dict=None, persist_config=False, **kwargs):
+
+ if (config_dict is None):
+ kcfg = KubeConfigMerger(filename)
+ if persist_config and 'config_persister' not in kwargs:
+ kwargs['config_persister'] = kcfg.save_changes
+
+ if kcfg.config is None:
+ raise ConfigException(
+ 'Invalid kube-config file. '
+ 'No configuration found.')
+ return KubeConfigLoader(
+ config_dict=kcfg.config,
+ config_base_path=None,
+ **kwargs)
+ else:
+ return KubeConfigLoader(
+ config_dict=config_dict,
+ config_base_path=None,
+ **kwargs)
def list_kube_config_contexts(config_file=None):
if config_file is None:
config_file = KUBE_CONFIG_DEFAULT_LOCATION
- loader = _get_kube_config_loader_for_yaml_file(config_file)
+ loader = _get_kube_config_loader(filename=config_file)
return loader.list_contexts(), loader.current_context
@@ -734,8 +755,8 @@ def load_kube_config(config_file=None, context=None,
if config_file is None:
config_file = KUBE_CONFIG_DEFAULT_LOCATION
- loader = _get_kube_config_loader_for_yaml_file(
- config_file, active_context=context,
+ loader = _get_kube_config_loader(
+ filename=config_file, active_context=context,
persist_config=persist_config)
if client_configuration is None:
@@ -745,6 +766,36 @@ def load_kube_config(config_file=None, context=None,
else:
loader.load_and_set(client_configuration)
+def load_kube_config_from_dict(config_dict, context=None,
+ client_configuration=None,
+ persist_config=True):
+ """Loads authentication and cluster information from kube-config file
+ and stores them in kubernetes.client.configuration.
+
+ :param config_dict: Takes the config file as a dict.
+ :param context: set the active context. If is set to None, current_context
+ from config file will be used.
+ :param client_configuration: The kubernetes.client.Configuration to
+ set configs to.
+ :param persist_config: If True, config file will be updated when changed
+ (e.g GCP token refresh).
+ """
+
+ if config_dict is None:
+ raise ConfigException(
+ 'Invalid kube-config dict. '
+ 'No configuration found.')
+
+ loader = _get_kube_config_loader(
+ config_dict=config_dict, active_context=context,
+ persist_config=persist_config)
+
+ if client_configuration is None:
+ config = type.__call__(Configuration)
+ loader.load_and_set(config)
+ Configuration.set_default(config)
+ else:
+ loader.load_and_set(client_configuration)
def new_client_from_config(
config_file=None,
