Merge pull request #208 from ryphon/207-config-as-string
Support file-ish objects in config loading
This commit is contained in:
commit
fd322f70aa
@ -667,8 +667,27 @@ class KubeConfigMerger:
|
||||
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)
|
||||
|
||||
for path in paths.split(ENV_KUBECONFIG_PATH_SEPARATOR):
|
||||
@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())
|
||||
|
||||
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 @@ class KubeConfigMerger:
|
||||
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)
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
|
||||
import base64
|
||||
import datetime
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
@ -1247,7 +1248,7 @@ class TestKubeConfigLoader(BaseTestCase):
|
||||
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 @@ class TestKubeConfigLoader(BaseTestCase):
|
||||
client_configuration=actual)
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
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",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user