Skip to content

Commit f65f06b

Browse files
committed
commiting changes to branch
1 parent 54d188f commit f65f06b

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

config/kube_config.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import json
2020
import logging
2121
import os
22+
import io
2223
import platform
2324
import subprocess
2425
import tempfile
@@ -667,19 +668,31 @@ def __init__(self, paths):
667668
self.paths = []
668669
self.config_files = {}
669670
self.config_merged = None
670-
671-
for path in paths.split(ENV_KUBECONFIG_PATH_SEPARATOR):
672-
if path:
673-
path = os.path.expanduser(path)
674-
if os.path.exists(path):
675-
self.paths.append(path)
676-
self.load_config(path)
677-
self.config_saved = copy.deepcopy(self.config_files)
671+
if hasattr(paths, 'read'):
672+
self.load_config_from_fileish(paths)
673+
else:
674+
for path in paths.split(ENV_KUBECONFIG_PATH_SEPARATOR):
675+
if path:
676+
path = os.path.expanduser(path)
677+
if os.path.exists(path):
678+
self.paths.append(path)
679+
self.load_config(path)
680+
self.config_saved = copy.deepcopy(self.config_files)
678681

679682
@property
680683
def config(self):
681684
return self.config_merged
682685

686+
def load_config_from_fileish(self, string):
687+
if hasattr(string, 'getvalue'):
688+
config = yaml.safe_load(string.getvalue())
689+
else:
690+
config = yaml.safe_load(string.read())
691+
692+
if self.config_merged is None:
693+
self.config_merged = copy.deepcopy(config)
694+
# doesn't need to do any further merging
695+
683696
def load_config(self, path):
684697
with open(path) as f:
685698
config = yaml.safe_load(f)

config/kube_config_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import datetime
1717
import json
1818
import os
19+
import io
1920
import shutil
2021
import tempfile
2122
import unittest
@@ -1257,6 +1258,14 @@ def test_load_kube_config(self):
12571258
client_configuration=actual)
12581259
self.assertEqual(expected, actual)
12591260

1261+
def test_load_kube_config_from_stringio(self):
1262+
expected = FakeConfig(host=TEST_HOST,
1263+
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
1264+
kubeconfig = self._create_stringio_config()
1265+
actual = FakeConfig()
1266+
load_kube_config(config_file=kubeconfig, context="simple_token", client_configuration=actual)
1267+
self.assertEqual(expected, actual)
1268+
12601269
def test_load_kube_config_from_dict(self):
12611270
expected = FakeConfig(host=TEST_HOST,
12621271
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
@@ -1633,6 +1642,11 @@ def _create_multi_config(self):
16331642
files.append(self._create_temp_file(yaml.safe_dump(part)))
16341643
return ENV_KUBECONFIG_PATH_SEPARATOR.join(files)
16351644

1645+
def _create_stringio_config(self):
1646+
obj = io.StringIO()
1647+
obj.write(self.TEST_KUBE_CONFIG_PART1)
1648+
return obj
1649+
16361650
def test_list_kube_config_contexts(self):
16371651
kubeconfigs = self._create_multi_config()
16381652
expected_contexts = [
@@ -1660,6 +1674,7 @@ def test_new_client_from_config(self):
16601674
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
16611675
client.configuration.api_key['authorization'])
16621676

1677+
16631678
def test_save_changes(self):
16641679
kubeconfigs = self._create_multi_config()
16651680

0 commit comments

Comments
 (0)