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

Commit a6a6273

Browse files
authored
Merge pull request #195 from vishnu667/kubeConfig_from_dict
Adding ability to set kube config from a dict.
2 parents 49ec060 + 9181235 commit a6a6273

File tree

3 files changed

+96
-19
lines changed

3 files changed

+96
-19
lines changed

config/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
from .config_exception import ConfigException
1616
from .incluster_config import load_incluster_config
1717
from .kube_config import (list_kube_config_contexts, load_kube_config,
18-
new_client_from_config)
18+
new_client_from_config, load_kube_config_from_dict)

config/kube_config.py

+59-17
Original file line numberDiff line numberDiff line change
@@ -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-
692691
def _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

710722
def 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

749791
def new_client_from_config(
750792
config_file=None,

config/kube_config_test.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333
ConfigNode, FileOrData, KubeConfigLoader,
3434
KubeConfigMerger, _cleanup_temp_files,
3535
_create_temp_file_with_content,
36+
_get_kube_config_loader,
3637
_get_kube_config_loader_for_yaml_file,
3738
list_kube_config_contexts, load_kube_config,
38-
new_client_from_config)
39+
load_kube_config_from_dict, new_client_from_config)
3940

4041
BEARER_TOKEN_FORMAT = "Bearer %s"
4142

@@ -1229,6 +1230,16 @@ def test_load_kube_config(self):
12291230
client_configuration=actual)
12301231
self.assertEqual(expected, actual)
12311232

1233+
def test_load_kube_config_from_dict(self):
1234+
expected = FakeConfig(host=TEST_HOST,
1235+
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
1236+
1237+
actual = FakeConfig()
1238+
load_kube_config_from_dict(config_dict=self.TEST_KUBE_CONFIG,
1239+
context="simple_token",
1240+
client_configuration=actual)
1241+
self.assertEqual(expected, actual)
1242+
12321243
def test_list_kube_config_contexts(self):
12331244
config_file = self._create_temp_file(
12341245
yaml.safe_dump(self.TEST_KUBE_CONFIG))
@@ -1344,6 +1355,30 @@ def test__get_kube_config_loader_for_yaml_file_persist(self):
13441355
self.assertTrue(callable(actual._config_persister))
13451356
self.assertEquals(actual._config_persister.__name__, "save_changes")
13461357

1358+
def test__get_kube_config_loader_file_no_persist(self):
1359+
expected = FakeConfig(host=TEST_HOST,
1360+
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
1361+
config_file = self._create_temp_file(
1362+
yaml.safe_dump(self.TEST_KUBE_CONFIG))
1363+
actual = _get_kube_config_loader(filename=config_file)
1364+
self.assertIsNone(actual._config_persister)
1365+
1366+
def test__get_kube_config_loader_file_persist(self):
1367+
expected = FakeConfig(host=TEST_HOST,
1368+
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
1369+
config_file = self._create_temp_file(
1370+
yaml.safe_dump(self.TEST_KUBE_CONFIG))
1371+
actual = _get_kube_config_loader(filename=config_file,
1372+
persist_config=True)
1373+
self.assertTrue(callable(actual._config_persister))
1374+
self.assertEquals(actual._config_persister.__name__, "save_changes")
1375+
1376+
def test__get_kube_config_loader_dict_no_persist(self):
1377+
expected = FakeConfig(host=TEST_HOST,
1378+
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
1379+
actual = _get_kube_config_loader(
1380+
config_dict=self.TEST_KUBE_CONFIG)
1381+
self.assertIsNone(actual._config_persister)
13471382

13481383
class TestKubernetesClientConfiguration(BaseTestCase):
13491384
# Verifies properties of kubernetes.client.Configuration.

0 commit comments

Comments
 (0)