diff --git a/config/kube_config.py b/config/kube_config.py index 68910841..0ed5a71c 100644 --- a/config/kube_config.py +++ b/config/kube_config.py @@ -667,8 +667,27 @@ def __init__(self, paths): self.paths = [] self.config_files = {} self.config_merged = None + if hasattr(paths, 'read'): + self._load_config_from_file_like_object(paths) + else: + self._load_config_from_file_path(paths) + + @property + def config(self): + return self.config_merged + + def _load_config_from_file_like_object(self, string): + if hasattr(string, 'getvalue'): + config = yaml.safe_load(string.getvalue()) + else: + config = yaml.safe_load(string.read()) - for path in paths.split(ENV_KUBECONFIG_PATH_SEPARATOR): + if self.config_merged is None: + self.config_merged = copy.deepcopy(config) + # doesn't need to do any further merging + + def _load_config_from_file_path(self, string): + for path in string.split(ENV_KUBECONFIG_PATH_SEPARATOR): if path: path = os.path.expanduser(path) if os.path.exists(path): @@ -676,10 +695,6 @@ def __init__(self, paths): self.load_config(path) self.config_saved = copy.deepcopy(self.config_files) - @property - def config(self): - return self.config_merged - def load_config(self, path): with open(path) as f: config = yaml.safe_load(f) diff --git a/config/kube_config_test.py b/config/kube_config_test.py index 4b406b34..de1dcc1b 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -14,6 +14,7 @@ import base64 import datetime +import io import json import os import shutil @@ -1247,7 +1248,7 @@ def test_ssl_with_relative_ssl_files(self): finally: shutil.rmtree(temp_dir) - def test_load_kube_config(self): + def test_load_kube_config_from_file_path(self): expected = FakeConfig(host=TEST_HOST, token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64) config_file = self._create_temp_file( @@ -1257,10 +1258,32 @@ def test_load_kube_config(self): client_configuration=actual) self.assertEqual(expected, actual) - def test_load_kube_config_from_dict(self): + def test_load_kube_config_from_file_like_object(self): expected = FakeConfig(host=TEST_HOST, token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64) + config_file_like_object = io.StringIO() + # py3 (won't have unicode) vs py2 (requires it) + try: + unicode('') + config_file_like_object.write( + unicode( + yaml.safe_dump( + self.TEST_KUBE_CONFIG), + errors='replace')) + except NameError: + config_file_like_object.write( + yaml.safe_dump( + self.TEST_KUBE_CONFIG)) + actual = FakeConfig() + load_kube_config( + config_file=config_file_like_object, + context="simple_token", + 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",