Skip to content

Commit fd322f7

Browse files
authored
Merge pull request kubernetes-client#208 from ryphon/207-config-as-string
Support file-ish objects in config loading
2 parents 3dc7fe0 + 0559445 commit fd322f7

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

config/kube_config.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -667,19 +667,34 @@ def __init__(self, paths):
667667
self.paths = []
668668
self.config_files = {}
669669
self.config_merged = None
670+
if hasattr(paths, 'read'):
671+
self._load_config_from_file_like_object(paths)
672+
else:
673+
self._load_config_from_file_path(paths)
674+
675+
@property
676+
def config(self):
677+
return self.config_merged
678+
679+
def _load_config_from_file_like_object(self, string):
680+
if hasattr(string, 'getvalue'):
681+
config = yaml.safe_load(string.getvalue())
682+
else:
683+
config = yaml.safe_load(string.read())
670684

671-
for path in paths.split(ENV_KUBECONFIG_PATH_SEPARATOR):
685+
if self.config_merged is None:
686+
self.config_merged = copy.deepcopy(config)
687+
# doesn't need to do any further merging
688+
689+
def _load_config_from_file_path(self, string):
690+
for path in string.split(ENV_KUBECONFIG_PATH_SEPARATOR):
672691
if path:
673692
path = os.path.expanduser(path)
674693
if os.path.exists(path):
675694
self.paths.append(path)
676695
self.load_config(path)
677696
self.config_saved = copy.deepcopy(self.config_files)
678697

679-
@property
680-
def config(self):
681-
return self.config_merged
682-
683698
def load_config(self, path):
684699
with open(path) as f:
685700
config = yaml.safe_load(f)

config/kube_config_test.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import base64
1616
import datetime
17+
import io
1718
import json
1819
import os
1920
import shutil
@@ -1247,7 +1248,7 @@ def test_ssl_with_relative_ssl_files(self):
12471248
finally:
12481249
shutil.rmtree(temp_dir)
12491250

1250-
def test_load_kube_config(self):
1251+
def test_load_kube_config_from_file_path(self):
12511252
expected = FakeConfig(host=TEST_HOST,
12521253
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
12531254
config_file = self._create_temp_file(
@@ -1257,10 +1258,32 @@ def test_load_kube_config(self):
12571258
client_configuration=actual)
12581259
self.assertEqual(expected, actual)
12591260

1260-
def test_load_kube_config_from_dict(self):
1261+
def test_load_kube_config_from_file_like_object(self):
12611262
expected = FakeConfig(host=TEST_HOST,
12621263
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
1264+
config_file_like_object = io.StringIO()
1265+
# py3 (won't have unicode) vs py2 (requires it)
1266+
try:
1267+
unicode('')
1268+
config_file_like_object.write(
1269+
unicode(
1270+
yaml.safe_dump(
1271+
self.TEST_KUBE_CONFIG),
1272+
errors='replace'))
1273+
except NameError:
1274+
config_file_like_object.write(
1275+
yaml.safe_dump(
1276+
self.TEST_KUBE_CONFIG))
1277+
actual = FakeConfig()
1278+
load_kube_config(
1279+
config_file=config_file_like_object,
1280+
context="simple_token",
1281+
client_configuration=actual)
1282+
self.assertEqual(expected, actual)
12631283

1284+
def test_load_kube_config_from_dict(self):
1285+
expected = FakeConfig(host=TEST_HOST,
1286+
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
12641287
actual = FakeConfig()
12651288
load_kube_config_from_dict(config_dict=self.TEST_KUBE_CONFIG,
12661289
context="simple_token",

0 commit comments

Comments
 (0)