From 630d6e400b55fae4125cf402aa31afe50313398f Mon Sep 17 00:00:00 2001 From: JackYoon Date: Tue, 13 Apr 2021 15:56:55 +0800 Subject: [PATCH 01/11] add params --- config/kube_config.py | 44 ++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/config/kube_config.py b/config/kube_config.py index 61a261f..fa8195d 100644 --- a/config/kube_config.py +++ b/config/kube_config.py @@ -60,7 +60,7 @@ def _cleanup_temp_files(): _temp_files = {} -def _create_temp_file_with_content(content): +def _create_temp_file_with_content(content, temp_file_path=None): if len(_temp_files) == 0: atexit.register(_cleanup_temp_files) # Because we may change context several times, try to remember files we @@ -68,7 +68,9 @@ def _create_temp_file_with_content(content): content_key = str(content) if content_key in _temp_files: return _temp_files[content_key] - _, name = tempfile.mkstemp() + if temp_file_path and not os.path.isdir(temp_file_path): + os.makedirs(name=temp_file_path, exist_ok=True) + _, name = tempfile.mkstemp(dir=temp_file_path) _temp_files[content_key] = name with open(name, 'wb') as fd: fd.write(content.encode() if isinstance(content, str) else content) @@ -91,12 +93,14 @@ class FileOrData(object): result in base64 encode of the file content after read.""" def __init__(self, obj, file_key_name, data_key_name=None, - file_base_path="", base64_file_content=True): + file_base_path="", base64_file_content=True, + temp_file_path=None): if not data_key_name: data_key_name = file_key_name + "-data" self._file = None self._data = None self._base64_file_content = base64_file_content + self._temp_file_path = temp_file_path if not obj: return if data_key_name in obj: @@ -116,9 +120,9 @@ def as_file(self): else: content = self._data self._file = _create_temp_file_with_content( - base64.standard_b64decode(content)) + base64.standard_b64decode(content), self._temp_file_path) else: - self._file = _create_temp_file_with_content(self._data) + self._file = _create_temp_file_with_content(self._data, self._temp_file_path) if self._file and not os.path.isfile(self._file): raise ConfigException("File does not exist: %s" % self._file) return self._file @@ -182,7 +186,8 @@ class KubeConfigLoader(object): def __init__(self, config_dict, active_context=None, get_google_credentials=None, config_base_path="", - config_persister=None): + config_persister=None, + temp_file_path=None): if config_dict is None: raise ConfigException( @@ -199,6 +204,7 @@ def __init__(self, config_dict, active_context=None, self.set_active_context(active_context) self._config_base_path = config_base_path self._config_persister = config_persister + self._temp_file_path = temp_file_path def _refresh_credentials_with_cmd_path(): config = self._user['auth-provider']['config'] @@ -489,12 +495,14 @@ def _load_from_exec_plugin(self): status, None, data_key_name='clientCertificateData', file_base_path=base_path, - base64_file_content=False).as_file() + base64_file_content=False, + temp_file_path=self._temp_file_path).as_file() self.key_file = FileOrData( status, None, data_key_name='clientKeyData', file_base_path=base_path, - base64_file_content=False).as_file() + base64_file_content=False, + temp_file_path=self._temp_file_path).as_file() return True logging.error('exec: missing token or clientCertificateData field ' 'in plugin output') @@ -507,7 +515,8 @@ def _load_user_token(self): token = FileOrData( self._user, 'tokenFile', 'token', file_base_path=base_path, - base64_file_content=False).as_data() + base64_file_content=False, + temp_file_path=self._temp_file_path).as_data() if token: self.token = "Bearer %s" % token return True @@ -533,17 +542,20 @@ def _load_cluster_info(self): base_path = self._get_base_path(self._cluster.path) self.ssl_ca_cert = FileOrData( self._cluster, 'certificate-authority', - file_base_path=base_path).as_file() + file_base_path=base_path, + temp_file_path=self._temp_file_path).as_file() if 'cert_file' not in self.__dict__: # cert_file could have been provided by # _load_from_exec_plugin; only load from the _user # section if we need it. self.cert_file = FileOrData( self._user, 'client-certificate', - file_base_path=base_path).as_file() + file_base_path=base_path, + temp_file_path=self._temp_file_path).as_file() self.key_file = FileOrData( self._user, 'client-key', - file_base_path=base_path).as_file() + file_base_path=base_path, + temp_file_path=self._temp_file_path).as_file() if 'insecure-skip-tls-verify' in self._cluster: self.verify_ssl = not self._cluster['insecure-skip-tls-verify'] @@ -811,7 +823,8 @@ def load_kube_config(config_file=None, context=None, def load_kube_config_from_dict(config_dict, context=None, client_configuration=None, - persist_config=True): + persist_config=True, + temp_file_path=None): """Loads authentication and cluster information from config_dict file and stores them in kubernetes.client.configuration. @@ -822,8 +835,8 @@ def load_kube_config_from_dict(config_dict, context=None, set configs to. :param persist_config: If True, config file will be updated when changed (e.g GCP token refresh). + :param temp_file_path: store temp files path. """ - if config_dict is None: raise ConfigException( 'Invalid kube-config dict. ' @@ -831,7 +844,8 @@ def load_kube_config_from_dict(config_dict, context=None, loader = _get_kube_config_loader( config_dict=config_dict, active_context=context, - persist_config=persist_config) + persist_config=persist_config, + temp_file_path=temp_file_path) if client_configuration is None: config = type.__call__(Configuration) From 95e88c424c1fc8daf56736797fd8186f0e6178da Mon Sep 17 00:00:00 2001 From: JackYoon Date: Tue, 13 Apr 2021 16:53:24 +0800 Subject: [PATCH 02/11] add tests --- config/kube_config.py | 3 ++- config/kube_config_test.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/config/kube_config.py b/config/kube_config.py index fa8195d..6b8ac73 100644 --- a/config/kube_config.py +++ b/config/kube_config.py @@ -122,7 +122,8 @@ def as_file(self): self._file = _create_temp_file_with_content( base64.standard_b64decode(content), self._temp_file_path) else: - self._file = _create_temp_file_with_content(self._data, self._temp_file_path) + self._file = _create_temp_file_with_content( + self._data, self._temp_file_path) if self._file and not os.path.isfile(self._file): raise ConfigException("File does not exist: %s" % self._file) return self._file diff --git a/config/kube_config_test.py b/config/kube_config_test.py index a82ef40..d73655a 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -1290,6 +1290,18 @@ def test_load_kube_config_from_dict(self): client_configuration=actual) self.assertEqual(expected, actual) + def test_load_kube_config_from_dict_with_temp_files_path(self): + expected = FakeConfig(host=TEST_HOST, + token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64) + actual = FakeConfig() + tmp_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), + 'tmp_file_path_test') + load_kube_config_from_dict(config_dict=self.TEST_KUBE_CONFIG, + context="simple_token", + client_configuration=actual, + temp_files_path=tmp_path) + self.assertEqual(expected, actual) + def test_load_kube_config_from_empty_file_like_object(self): config_file_like_object = io.StringIO() self.assertRaises( From 4f946bf1e0c4607220c0852dfb99241c2436daf1 Mon Sep 17 00:00:00 2001 From: JackYoon Date: Tue, 13 Apr 2021 17:03:10 +0800 Subject: [PATCH 03/11] fixes typo --- config/kube_config_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/kube_config_test.py b/config/kube_config_test.py index d73655a..c6e74d9 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -1290,7 +1290,7 @@ def test_load_kube_config_from_dict(self): client_configuration=actual) self.assertEqual(expected, actual) - def test_load_kube_config_from_dict_with_temp_files_path(self): + def test_load_kube_config_from_dict_with_temp_file_path(self): expected = FakeConfig(host=TEST_HOST, token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64) actual = FakeConfig() @@ -1299,7 +1299,7 @@ def test_load_kube_config_from_dict_with_temp_files_path(self): load_kube_config_from_dict(config_dict=self.TEST_KUBE_CONFIG, context="simple_token", client_configuration=actual, - temp_files_path=tmp_path) + temp_file_path=tmp_path) self.assertEqual(expected, actual) def test_load_kube_config_from_empty_file_like_object(self): From 53f31d84feaaceb4d6d472ffaf3ea8f7840dd525 Mon Sep 17 00:00:00 2001 From: JackYoon Date: Tue, 13 Apr 2021 17:36:07 +0800 Subject: [PATCH 04/11] update codestyle --- config/kube_config_test.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/config/kube_config_test.py b/config/kube_config_test.py index c6e74d9..a7d1eab 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -1294,8 +1294,11 @@ def test_load_kube_config_from_dict_with_temp_file_path(self): expected = FakeConfig(host=TEST_HOST, token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64) actual = FakeConfig() - tmp_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), - 'tmp_file_path_test') + tmp_path = os.path.join( + os.path.dirname( + os.path.dirname( + os.path.abspath(__file__))), + 'tmp_file_path_test') load_kube_config_from_dict(config_dict=self.TEST_KUBE_CONFIG, context="simple_token", client_configuration=actual, From c4481a4063fdf91367d2d4d94337224179e5f054 Mon Sep 17 00:00:00 2001 From: Feeding <5670119@qq.com> Date: Wed, 14 Apr 2021 09:19:45 +0800 Subject: [PATCH 05/11] Update kube_config_test.py add an assertion for empty dir --- config/kube_config_test.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/config/kube_config_test.py b/config/kube_config_test.py index a7d1eab..e0d3a92 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -1291,8 +1291,6 @@ def test_load_kube_config_from_dict(self): self.assertEqual(expected, actual) def test_load_kube_config_from_dict_with_temp_file_path(self): - expected = FakeConfig(host=TEST_HOST, - token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64) actual = FakeConfig() tmp_path = os.path.join( os.path.dirname( @@ -1303,7 +1301,7 @@ def test_load_kube_config_from_dict_with_temp_file_path(self): context="simple_token", client_configuration=actual, temp_file_path=tmp_path) - self.assertEqual(expected, actual) + self.assertEqual(True if not os.listdir(tmp_path) else False) def test_load_kube_config_from_empty_file_like_object(self): config_file_like_object = io.StringIO() From d46bd0e766e6ac0bf43b34469f09b294671e3553 Mon Sep 17 00:00:00 2001 From: Feeding <5670119@qq.com> Date: Wed, 14 Apr 2021 11:21:36 +0800 Subject: [PATCH 06/11] Update kube_config_test.py --- 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 e0d3a92..2c2b5ae 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -1298,7 +1298,7 @@ def test_load_kube_config_from_dict_with_temp_file_path(self): os.path.abspath(__file__))), 'tmp_file_path_test') load_kube_config_from_dict(config_dict=self.TEST_KUBE_CONFIG, - context="simple_token", + context="ssl", client_configuration=actual, temp_file_path=tmp_path) self.assertEqual(True if not os.listdir(tmp_path) else False) From a9463ef8047ce65857413bfdd1843a1a5c940c9e Mon Sep 17 00:00:00 2001 From: Feeding <5670119@qq.com> Date: Wed, 14 Apr 2021 11:39:18 +0800 Subject: [PATCH 07/11] Update kube_config.py --- config/kube_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/kube_config.py b/config/kube_config.py index 6b8ac73..584b8a4 100644 --- a/config/kube_config.py +++ b/config/kube_config.py @@ -69,7 +69,7 @@ def _create_temp_file_with_content(content, temp_file_path=None): if content_key in _temp_files: return _temp_files[content_key] if temp_file_path and not os.path.isdir(temp_file_path): - os.makedirs(name=temp_file_path, exist_ok=True) + os.makedirs(name=temp_file_path) _, name = tempfile.mkstemp(dir=temp_file_path) _temp_files[content_key] = name with open(name, 'wb') as fd: From bad146f0ac354fa7dc74b9165f8c747334cb2303 Mon Sep 17 00:00:00 2001 From: Feeding <5670119@qq.com> Date: Wed, 14 Apr 2021 11:40:56 +0800 Subject: [PATCH 08/11] Update kube_config_test.py --- 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 2c2b5ae..735b2ca 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -1301,7 +1301,7 @@ def test_load_kube_config_from_dict_with_temp_file_path(self): context="ssl", client_configuration=actual, temp_file_path=tmp_path) - self.assertEqual(True if not os.listdir(tmp_path) else False) + self.assertFalse(True if not os.listdir(tmp_path) else False) def test_load_kube_config_from_empty_file_like_object(self): config_file_like_object = io.StringIO() From 65cb8f0be51609c75b7a60736e40fa1b9e7219d2 Mon Sep 17 00:00:00 2001 From: Feeding <5670119@qq.com> Date: Wed, 14 Apr 2021 11:55:37 +0800 Subject: [PATCH 09/11] Update kube_config_test.py --- config/kube_config_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/config/kube_config_test.py b/config/kube_config_test.py index 735b2ca..5183eb1 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -1302,6 +1302,7 @@ def test_load_kube_config_from_dict_with_temp_file_path(self): client_configuration=actual, temp_file_path=tmp_path) self.assertFalse(True if not os.listdir(tmp_path) else False) + _cleanup_temp_files def test_load_kube_config_from_empty_file_like_object(self): config_file_like_object = io.StringIO() From b97bb098ea5ffb4838d1efd751e75727b9b2b9b2 Mon Sep 17 00:00:00 2001 From: Feeding <5670119@qq.com> Date: Thu, 15 Apr 2021 02:05:51 +0800 Subject: [PATCH 10/11] Update kube_config_test.py --- config/kube_config_test.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/kube_config_test.py b/config/kube_config_test.py index 5183eb1..846b2d5 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -1291,6 +1291,8 @@ def test_load_kube_config_from_dict(self): self.assertEqual(expected, actual) def test_load_kube_config_from_dict_with_temp_file_path(self): + expected = FakeConfig(host=TEST_HOST, + token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64) actual = FakeConfig() tmp_path = os.path.join( os.path.dirname( @@ -1302,6 +1304,7 @@ def test_load_kube_config_from_dict_with_temp_file_path(self): client_configuration=actual, temp_file_path=tmp_path) self.assertFalse(True if not os.listdir(tmp_path) else False) + self.assertEqual(expected, actual) _cleanup_temp_files def test_load_kube_config_from_empty_file_like_object(self): From 0e75a47b97ec93a4f6e974f56e388b7d1126d15f Mon Sep 17 00:00:00 2001 From: Feeding <5670119@qq.com> Date: Thu, 15 Apr 2021 02:14:42 +0800 Subject: [PATCH 11/11] Update kube_config_test.py --- config/kube_config_test.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/config/kube_config_test.py b/config/kube_config_test.py index 846b2d5..c33ffed 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -1291,8 +1291,14 @@ def test_load_kube_config_from_dict(self): self.assertEqual(expected, actual) def test_load_kube_config_from_dict_with_temp_file_path(self): - expected = FakeConfig(host=TEST_HOST, - token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64) + expected = FakeConfig( + host=TEST_SSL_HOST, + token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64, + cert_file=self._create_temp_file(TEST_CLIENT_CERT), + key_file=self._create_temp_file(TEST_CLIENT_KEY), + ssl_ca_cert=self._create_temp_file(TEST_CERTIFICATE_AUTH), + verify_ssl=True + ) actual = FakeConfig() tmp_path = os.path.join( os.path.dirname(