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

Commit

Permalink
Merge pull request #195 from vishnu667/kubeConfig_from_dict
Browse files Browse the repository at this point in the history
Adding ability to set kube config from a dict.
  • Loading branch information
k8s-ci-robot committed Jun 19, 2020
2 parents 49ec060 + 9181235 commit a6a6273
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 19 deletions.
2 changes: 1 addition & 1 deletion config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
from .config_exception import ConfigException
from .incluster_config import load_incluster_config
from .kube_config import (list_kube_config_contexts, load_kube_config,
new_client_from_config)
new_client_from_config, load_kube_config_from_dict)
76 changes: 59 additions & 17 deletions config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,31 +688,43 @@ def save_config(self, path):
yaml.safe_dump(self.config_files[path], f,
default_flow_style=False)


def _get_kube_config_loader_for_yaml_file(
filename, persist_config=False, **kwargs):

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,
return _get_kube_config_loader(
filename=filename,
persist_config=persist_config,
**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 +746,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 +757,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 config_dict 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
37 changes: 36 additions & 1 deletion config/kube_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
ConfigNode, FileOrData, KubeConfigLoader,
KubeConfigMerger, _cleanup_temp_files,
_create_temp_file_with_content,
_get_kube_config_loader,
_get_kube_config_loader_for_yaml_file,
list_kube_config_contexts, load_kube_config,
new_client_from_config)
load_kube_config_from_dict, new_client_from_config)

BEARER_TOKEN_FORMAT = "Bearer %s"

Expand Down Expand Up @@ -1229,6 +1230,16 @@ def test_load_kube_config(self):
client_configuration=actual)
self.assertEqual(expected, actual)

def test_load_kube_config_from_dict(self):
expected = FakeConfig(host=TEST_HOST,
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)

actual = FakeConfig()
load_kube_config_from_dict(config_dict=self.TEST_KUBE_CONFIG,
context="simple_token",
client_configuration=actual)
self.assertEqual(expected, actual)

def test_list_kube_config_contexts(self):
config_file = self._create_temp_file(
yaml.safe_dump(self.TEST_KUBE_CONFIG))
Expand Down Expand Up @@ -1344,6 +1355,30 @@ def test__get_kube_config_loader_for_yaml_file_persist(self):
self.assertTrue(callable(actual._config_persister))
self.assertEquals(actual._config_persister.__name__, "save_changes")

def test__get_kube_config_loader_file_no_persist(self):
expected = FakeConfig(host=TEST_HOST,
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
config_file = self._create_temp_file(
yaml.safe_dump(self.TEST_KUBE_CONFIG))
actual = _get_kube_config_loader(filename=config_file)
self.assertIsNone(actual._config_persister)

def test__get_kube_config_loader_file_persist(self):
expected = FakeConfig(host=TEST_HOST,
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
config_file = self._create_temp_file(
yaml.safe_dump(self.TEST_KUBE_CONFIG))
actual = _get_kube_config_loader(filename=config_file,
persist_config=True)
self.assertTrue(callable(actual._config_persister))
self.assertEquals(actual._config_persister.__name__, "save_changes")

def test__get_kube_config_loader_dict_no_persist(self):
expected = FakeConfig(host=TEST_HOST,
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
actual = _get_kube_config_loader(
config_dict=self.TEST_KUBE_CONFIG)
self.assertIsNone(actual._config_persister)

class TestKubernetesClientConfiguration(BaseTestCase):
# Verifies properties of kubernetes.client.Configuration.
Expand Down

0 comments on commit a6a6273

Please sign in to comment.