Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit fc5b730

Browse files
committed
load_kube_config_from_dict() support define custom temp files path
1 parent db50d02 commit fc5b730

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

Diff for: config/kube_config.py

+30-15
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,17 @@ def _cleanup_temp_files():
6060
_temp_files = {}
6161

6262

63-
def _create_temp_file_with_content(content):
63+
def _create_temp_file_with_content(content, temp_file_path=None):
6464
if len(_temp_files) == 0:
6565
atexit.register(_cleanup_temp_files)
6666
# Because we may change context several times, try to remember files we
6767
# created and reuse them at a small memory cost.
6868
content_key = str(content)
6969
if content_key in _temp_files:
7070
return _temp_files[content_key]
71-
_, name = tempfile.mkstemp()
71+
if temp_file_path and not os.path.isdir(temp_file_path):
72+
os.makedirs(name=temp_file_path)
73+
_, name = tempfile.mkstemp(dir=temp_file_path)
7274
_temp_files[content_key] = name
7375
with open(name, 'wb') as fd:
7476
fd.write(content.encode() if isinstance(content, str) else content)
@@ -91,12 +93,14 @@ class FileOrData(object):
9193
result in base64 encode of the file content after read."""
9294

9395
def __init__(self, obj, file_key_name, data_key_name=None,
94-
file_base_path="", base64_file_content=True):
96+
file_base_path="", base64_file_content=True,
97+
temp_file_path=None):
9598
if not data_key_name:
9699
data_key_name = file_key_name + "-data"
97100
self._file = None
98101
self._data = None
99102
self._base64_file_content = base64_file_content
103+
self._temp_file_path = temp_file_path
100104
if not obj:
101105
return
102106
if data_key_name in obj:
@@ -116,9 +120,10 @@ def as_file(self):
116120
else:
117121
content = self._data
118122
self._file = _create_temp_file_with_content(
119-
base64.standard_b64decode(content))
123+
base64.standard_b64decode(content), self._temp_file_path)
120124
else:
121-
self._file = _create_temp_file_with_content(self._data)
125+
self._file = _create_temp_file_with_content(
126+
self._data, self._temp_file_path)
122127
if self._file and not os.path.isfile(self._file):
123128
raise ConfigException("File does not exist: %s" % self._file)
124129
return self._file
@@ -182,7 +187,8 @@ class KubeConfigLoader(object):
182187
def __init__(self, config_dict, active_context=None,
183188
get_google_credentials=None,
184189
config_base_path="",
185-
config_persister=None):
190+
config_persister=None,
191+
temp_file_path=None):
186192

187193
if config_dict is None:
188194
raise ConfigException(
@@ -199,6 +205,7 @@ def __init__(self, config_dict, active_context=None,
199205
self.set_active_context(active_context)
200206
self._config_base_path = config_base_path
201207
self._config_persister = config_persister
208+
self._temp_file_path = temp_file_path
202209

203210
def _refresh_credentials_with_cmd_path():
204211
config = self._user['auth-provider']['config']
@@ -489,12 +496,14 @@ def _load_from_exec_plugin(self):
489496
status, None,
490497
data_key_name='clientCertificateData',
491498
file_base_path=base_path,
492-
base64_file_content=False).as_file()
499+
base64_file_content=False,
500+
temp_file_path=self._temp_file_path).as_file()
493501
self.key_file = FileOrData(
494502
status, None,
495503
data_key_name='clientKeyData',
496504
file_base_path=base_path,
497-
base64_file_content=False).as_file()
505+
base64_file_content=False,
506+
temp_file_path=self._temp_file_path).as_file()
498507
return True
499508
logging.error('exec: missing token or clientCertificateData field '
500509
'in plugin output')
@@ -507,7 +516,8 @@ def _load_user_token(self):
507516
token = FileOrData(
508517
self._user, 'tokenFile', 'token',
509518
file_base_path=base_path,
510-
base64_file_content=False).as_data()
519+
base64_file_content=False,
520+
temp_file_path=self._temp_file_path).as_data()
511521
if token:
512522
self.token = "Bearer %s" % token
513523
return True
@@ -533,17 +543,20 @@ def _load_cluster_info(self):
533543
base_path = self._get_base_path(self._cluster.path)
534544
self.ssl_ca_cert = FileOrData(
535545
self._cluster, 'certificate-authority',
536-
file_base_path=base_path).as_file()
546+
file_base_path=base_path,
547+
temp_file_path=self._temp_file_path).as_file()
537548
if 'cert_file' not in self.__dict__:
538549
# cert_file could have been provided by
539550
# _load_from_exec_plugin; only load from the _user
540551
# section if we need it.
541552
self.cert_file = FileOrData(
542553
self._user, 'client-certificate',
543-
file_base_path=base_path).as_file()
554+
file_base_path=base_path,
555+
temp_file_path=self._temp_file_path).as_file()
544556
self.key_file = FileOrData(
545557
self._user, 'client-key',
546-
file_base_path=base_path).as_file()
558+
file_base_path=base_path,
559+
temp_file_path=self._temp_file_path).as_file()
547560
if 'insecure-skip-tls-verify' in self._cluster:
548561
self.verify_ssl = not self._cluster['insecure-skip-tls-verify']
549562

@@ -811,7 +824,8 @@ def load_kube_config(config_file=None, context=None,
811824

812825
def load_kube_config_from_dict(config_dict, context=None,
813826
client_configuration=None,
814-
persist_config=True):
827+
persist_config=True,
828+
temp_file_path=None):
815829
"""Loads authentication and cluster information from config_dict file
816830
and stores them in kubernetes.client.configuration.
817831
@@ -822,16 +836,17 @@ def load_kube_config_from_dict(config_dict, context=None,
822836
set configs to.
823837
:param persist_config: If True, config file will be updated when changed
824838
(e.g GCP token refresh).
839+
:param temp_file_path: store temp files path.
825840
"""
826-
827841
if config_dict is None:
828842
raise ConfigException(
829843
'Invalid kube-config dict. '
830844
'No configuration found.')
831845

832846
loader = _get_kube_config_loader(
833847
config_dict=config_dict, active_context=context,
834-
persist_config=persist_config)
848+
persist_config=persist_config,
849+
temp_file_path=temp_file_path)
835850

836851
if client_configuration is None:
837852
config = type.__call__(Configuration)

Diff for: config/kube_config_test.py

+23
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,29 @@ def test_load_kube_config_from_dict(self):
12901290
client_configuration=actual)
12911291
self.assertEqual(expected, actual)
12921292

1293+
def test_load_kube_config_from_dict_with_temp_file_path(self):
1294+
expected = FakeConfig(
1295+
host=TEST_SSL_HOST,
1296+
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
1297+
cert_file=self._create_temp_file(TEST_CLIENT_CERT),
1298+
key_file=self._create_temp_file(TEST_CLIENT_KEY),
1299+
ssl_ca_cert=self._create_temp_file(TEST_CERTIFICATE_AUTH),
1300+
verify_ssl=True
1301+
)
1302+
actual = FakeConfig()
1303+
tmp_path = os.path.join(
1304+
os.path.dirname(
1305+
os.path.dirname(
1306+
os.path.abspath(__file__))),
1307+
'tmp_file_path_test')
1308+
load_kube_config_from_dict(config_dict=self.TEST_KUBE_CONFIG,
1309+
context="ssl",
1310+
client_configuration=actual,
1311+
temp_file_path=tmp_path)
1312+
self.assertFalse(True if not os.listdir(tmp_path) else False)
1313+
self.assertEqual(expected, actual)
1314+
_cleanup_temp_files
1315+
12931316
def test_load_kube_config_from_empty_file_like_object(self):
12941317
config_file_like_object = io.StringIO()
12951318
self.assertRaises(

0 commit comments

Comments
 (0)