From f65f06b1ed4388a1ab030215deb4381ec438f318 Mon Sep 17 00:00:00 2001 From: Dylan Shepard Date: Wed, 19 Aug 2020 12:36:32 -0700 Subject: [PATCH 1/7] commiting changes to branch --- config/kube_config.py | 29 +++++++++++++++++++++-------- config/kube_config_test.py | 15 +++++++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/config/kube_config.py b/config/kube_config.py index 68910841f..a1fc59c98 100644 --- a/config/kube_config.py +++ b/config/kube_config.py @@ -19,6 +19,7 @@ import datetime import json import logging import os +import io import platform import subprocess import tempfile @@ -667,19 +668,31 @@ class KubeConfigMerger: self.paths = [] self.config_files = {} self.config_merged = None - - for path in paths.split(ENV_KUBECONFIG_PATH_SEPARATOR): - if path: - path = os.path.expanduser(path) - if os.path.exists(path): - self.paths.append(path) - self.load_config(path) - self.config_saved = copy.deepcopy(self.config_files) + if hasattr(paths, 'read'): + self.load_config_from_fileish(paths) + else: + for path in paths.split(ENV_KUBECONFIG_PATH_SEPARATOR): + if path: + path = os.path.expanduser(path) + if os.path.exists(path): + self.paths.append(path) + self.load_config(path) + self.config_saved = copy.deepcopy(self.config_files) @property def config(self): return self.config_merged + def load_config_from_fileish(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(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 4b406b34f..a666cff2f 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -16,6 +16,7 @@ import base64 import datetime import json import os +import io import shutil import tempfile import unittest @@ -1257,6 +1258,14 @@ class TestKubeConfigLoader(BaseTestCase): client_configuration=actual) self.assertEqual(expected, actual) + def test_load_kube_config_from_stringio(self): + expected = FakeConfig(host=TEST_HOST, + token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64) + kubeconfig = self._create_stringio_config() + actual = FakeConfig() + load_kube_config(config_file=kubeconfig, 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) @@ -1633,6 +1642,11 @@ class TestKubeConfigMerger(BaseTestCase): files.append(self._create_temp_file(yaml.safe_dump(part))) return ENV_KUBECONFIG_PATH_SEPARATOR.join(files) + def _create_stringio_config(self): + obj = io.StringIO() + obj.write(self.TEST_KUBE_CONFIG_PART1) + return obj + def test_list_kube_config_contexts(self): kubeconfigs = self._create_multi_config() expected_contexts = [ @@ -1660,6 +1674,7 @@ class TestKubeConfigMerger(BaseTestCase): self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64, client.configuration.api_key['authorization']) + def test_save_changes(self): kubeconfigs = self._create_multi_config() From aac4e35ca9f14aaaa741f200283f3cfe0a85f1d9 Mon Sep 17 00:00:00 2001 From: Dylan Shepard Date: Wed, 19 Aug 2020 12:49:33 -0700 Subject: [PATCH 2/7] correcting tests --- config/kube_config_test.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/config/kube_config_test.py b/config/kube_config_test.py index a666cff2f..e53c57675 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -1258,12 +1258,13 @@ class TestKubeConfigLoader(BaseTestCase): client_configuration=actual) self.assertEqual(expected, actual) - def test_load_kube_config_from_stringio(self): + def test_load_kube_config_from_fileish(self): expected = FakeConfig(host=TEST_HOST, token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64) - kubeconfig = self._create_stringio_config() + config_fileish = io.StringIO() + config_fileish.write(yaml.safe_dump(self.TEST_KUBE_CONFIG)) actual = FakeConfig() - load_kube_config(config_file=kubeconfig, context="simple_token", client_configuration=actual) + load_kube_config(config_file=config_fileish, context="simple_token", client_configuration=actual) self.assertEqual(expected, actual) def test_load_kube_config_from_dict(self): @@ -1642,11 +1643,6 @@ class TestKubeConfigMerger(BaseTestCase): files.append(self._create_temp_file(yaml.safe_dump(part))) return ENV_KUBECONFIG_PATH_SEPARATOR.join(files) - def _create_stringio_config(self): - obj = io.StringIO() - obj.write(self.TEST_KUBE_CONFIG_PART1) - return obj - def test_list_kube_config_contexts(self): kubeconfigs = self._create_multi_config() expected_contexts = [ From f85a41fa31d47c7a5b153bdc2ca4fb0b1c60a710 Mon Sep 17 00:00:00 2001 From: Dylan Shepard Date: Mon, 31 Aug 2020 12:01:11 -0700 Subject: [PATCH 3/7] renaming functions and setting to internal --- config/kube_config.py | 21 ++++++++++++--------- config/kube_config_test.py | 12 ++++++------ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/config/kube_config.py b/config/kube_config.py index a1fc59c98..14fd71a68 100644 --- a/config/kube_config.py +++ b/config/kube_config.py @@ -669,21 +669,15 @@ class KubeConfigMerger: self.config_files = {} self.config_merged = None if hasattr(paths, 'read'): - self.load_config_from_fileish(paths) + self._load_config_from_file_like_object(paths) else: - for path in paths.split(ENV_KUBECONFIG_PATH_SEPARATOR): - if path: - path = os.path.expanduser(path) - if os.path.exists(path): - self.paths.append(path) - self.load_config(path) - self.config_saved = copy.deepcopy(self.config_files) + self._load_config_from_file_path(paths) @property def config(self): return self.config_merged - def load_config_from_fileish(self, string): + def _load_config_from_file_like_object(self, string): if hasattr(string, 'getvalue'): config = yaml.safe_load(string.getvalue()) else: @@ -693,6 +687,15 @@ class KubeConfigMerger: 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): + self.paths.append(path) + self.load_config(path) + self.config_saved = copy.deepcopy(self.config_files) + 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 e53c57675..1f74d3452 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -1248,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( @@ -1258,19 +1258,19 @@ class TestKubeConfigLoader(BaseTestCase): client_configuration=actual) self.assertEqual(expected, actual) - def test_load_kube_config_from_fileish(self): + def test_load_kube_config_from_file_like_object(self): expected = FakeConfig(host=TEST_HOST, token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64) - config_fileish = io.StringIO() - config_fileish.write(yaml.safe_dump(self.TEST_KUBE_CONFIG)) + config_file_like_object = io.StringIO() + config_file_like_object.write(yaml.safe_dump(self.TEST_KUBE_CONFIG)) actual = FakeConfig() - load_kube_config(config_file=config_fileish, context="simple_token", client_configuration=actual) + 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", From bfa968140cb6e7554ecb87e034f519ed2724ba8d Mon Sep 17 00:00:00 2001 From: Dylan Shepard Date: Wed, 9 Sep 2020 07:03:45 -0700 Subject: [PATCH 4/7] supporting 2.7, reading works fine, writing reqs unicode --- config/kube_config_test.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/config/kube_config_test.py b/config/kube_config_test.py index 1f74d3452..b2b90ce98 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -1262,7 +1262,12 @@ class TestKubeConfigLoader(BaseTestCase): expected = FakeConfig(host=TEST_HOST, token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64) config_file_like_object = io.StringIO() - config_file_like_object.write(yaml.safe_dump(self.TEST_KUBE_CONFIG)) + #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) From 49cbb1de99ec4bd3213a1f66c8fcd00a55ff761f Mon Sep 17 00:00:00 2001 From: Dylan Shepard Date: Wed, 9 Sep 2020 07:07:13 -0700 Subject: [PATCH 5/7] replace inside the parens --- config/kube_config_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/kube_config_test.py b/config/kube_config_test.py index b2b90ce98..8fcfcc5dd 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -1265,7 +1265,7 @@ class TestKubeConfigLoader(BaseTestCase): #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') + 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() From 9f4775f43f8d0d205941a3ae6e1f885d517410aa Mon Sep 17 00:00:00 2001 From: Dylan Shepard Date: Wed, 9 Sep 2020 07:22:04 -0700 Subject: [PATCH 6/7] trying to fix pycodestyle problems --- config/kube_config_test.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/config/kube_config_test.py b/config/kube_config_test.py index 8fcfcc5dd..f12a0b3ea 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -1262,15 +1262,23 @@ class TestKubeConfigLoader(BaseTestCase): 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) + # 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')) + 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)) + 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) + 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): @@ -1675,7 +1683,6 @@ class TestKubeConfigMerger(BaseTestCase): self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64, client.configuration.api_key['authorization']) - def test_save_changes(self): kubeconfigs = self._create_multi_config() From 0559445cb4a61548b34c68698e37219d837033c9 Mon Sep 17 00:00:00 2001 From: Dylan Shepard Date: Wed, 9 Sep 2020 07:28:51 -0700 Subject: [PATCH 7/7] unused io import, pre-setting pycodestyle --- config/kube_config.py | 1 - config/kube_config_test.py | 12 ++++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/config/kube_config.py b/config/kube_config.py index 14fd71a68..0ed5a71cf 100644 --- a/config/kube_config.py +++ b/config/kube_config.py @@ -19,7 +19,6 @@ import datetime import json import logging import os -import io import platform import subprocess import tempfile diff --git a/config/kube_config_test.py b/config/kube_config_test.py index f12a0b3ea..de1dcc1b7 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -14,9 +14,9 @@ import base64 import datetime +import io import json import os -import io import shutil import tempfile import unittest @@ -1272,13 +1272,13 @@ class TestKubeConfigLoader(BaseTestCase): errors='replace')) except NameError: config_file_like_object.write( - yaml.safe_dump( - self.TEST_KUBE_CONFIG)) + yaml.safe_dump( + self.TEST_KUBE_CONFIG)) actual = FakeConfig() load_kube_config( - config_file=config_file_like_object, - context="simple_token", - client_configuration=actual) + config_file=config_file_like_object, + context="simple_token", + client_configuration=actual) self.assertEqual(expected, actual) def test_load_kube_config_from_dict(self):