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

Commit 257ec27

Browse files
author
JackYin
committed
load_kube_config_from_dict() support define custom temp files path
1 parent db50d02 commit 257ec27

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

Diff for: config/kube_config.py

+29-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 not os.path.isdir(temp_file_path):
72+
os.makedirs(name=temp_file_path, exist_ok=True)
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,9 @@ 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(self._data, self._temp_file_path)
122126
if self._file and not os.path.isfile(self._file):
123127
raise ConfigException("File does not exist: %s" % self._file)
124128
return self._file
@@ -182,7 +186,8 @@ class KubeConfigLoader(object):
182186
def __init__(self, config_dict, active_context=None,
183187
get_google_credentials=None,
184188
config_base_path="",
185-
config_persister=None):
189+
config_persister=None,
190+
temp_file_path=None):
186191

187192
if config_dict is None:
188193
raise ConfigException(
@@ -199,6 +204,7 @@ def __init__(self, config_dict, active_context=None,
199204
self.set_active_context(active_context)
200205
self._config_base_path = config_base_path
201206
self._config_persister = config_persister
207+
self._temp_file_path = temp_file_path
202208

203209
def _refresh_credentials_with_cmd_path():
204210
config = self._user['auth-provider']['config']
@@ -489,12 +495,14 @@ def _load_from_exec_plugin(self):
489495
status, None,
490496
data_key_name='clientCertificateData',
491497
file_base_path=base_path,
492-
base64_file_content=False).as_file()
498+
base64_file_content=False,
499+
temp_file_path=self._temp_file_path).as_file()
493500
self.key_file = FileOrData(
494501
status, None,
495502
data_key_name='clientKeyData',
496503
file_base_path=base_path,
497-
base64_file_content=False).as_file()
504+
base64_file_content=False,
505+
temp_file_path=self._temp_file_path).as_file()
498506
return True
499507
logging.error('exec: missing token or clientCertificateData field '
500508
'in plugin output')
@@ -507,7 +515,8 @@ def _load_user_token(self):
507515
token = FileOrData(
508516
self._user, 'tokenFile', 'token',
509517
file_base_path=base_path,
510-
base64_file_content=False).as_data()
518+
base64_file_content=False,
519+
temp_file_path=self._temp_file_path).as_data()
511520
if token:
512521
self.token = "Bearer %s" % token
513522
return True
@@ -533,17 +542,20 @@ def _load_cluster_info(self):
533542
base_path = self._get_base_path(self._cluster.path)
534543
self.ssl_ca_cert = FileOrData(
535544
self._cluster, 'certificate-authority',
536-
file_base_path=base_path).as_file()
545+
file_base_path=base_path,
546+
temp_file_path=self._temp_file_path).as_file()
537547
if 'cert_file' not in self.__dict__:
538548
# cert_file could have been provided by
539549
# _load_from_exec_plugin; only load from the _user
540550
# section if we need it.
541551
self.cert_file = FileOrData(
542552
self._user, 'client-certificate',
543-
file_base_path=base_path).as_file()
553+
file_base_path=base_path,
554+
temp_file_path=self._temp_file_path).as_file()
544555
self.key_file = FileOrData(
545556
self._user, 'client-key',
546-
file_base_path=base_path).as_file()
557+
file_base_path=base_path,
558+
temp_file_path=self._temp_file_path).as_file()
547559
if 'insecure-skip-tls-verify' in self._cluster:
548560
self.verify_ssl = not self._cluster['insecure-skip-tls-verify']
549561

@@ -811,7 +823,8 @@ def load_kube_config(config_file=None, context=None,
811823

812824
def load_kube_config_from_dict(config_dict, context=None,
813825
client_configuration=None,
814-
persist_config=True):
826+
persist_config=True,
827+
temp_file_path=None):
815828
"""Loads authentication and cluster information from config_dict file
816829
and stores them in kubernetes.client.configuration.
817830
@@ -822,16 +835,17 @@ def load_kube_config_from_dict(config_dict, context=None,
822835
set configs to.
823836
:param persist_config: If True, config file will be updated when changed
824837
(e.g GCP token refresh).
838+
:param temp_file_path: store temp files path.
825839
"""
826-
827840
if config_dict is None:
828841
raise ConfigException(
829842
'Invalid kube-config dict. '
830843
'No configuration found.')
831844

832845
loader = _get_kube_config_loader(
833846
config_dict=config_dict, active_context=context,
834-
persist_config=persist_config)
847+
persist_config=persist_config,
848+
temp_file_path=temp_file_path)
835849

836850
if client_configuration is None:
837851
config = type.__call__(Configuration)

0 commit comments

Comments
 (0)