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

Commit

Permalink
Adding ability to pass kube_config as a dict.
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnu667 committed May 14, 2020
1 parent 49ec060 commit ab51510
Showing 1 changed file with 54 additions and 3 deletions.
57 changes: 54 additions & 3 deletions config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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:
Expand All @@ -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,
Expand Down

0 comments on commit ab51510

Please # to comment.