Skip to content

Commit ce901f3

Browse files
committed
fix: Try to recreate cached file if tmp file is not exist
1 parent 6e7c539 commit ce901f3

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

kubernetes/base/config/kube_config.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ def _cleanup_temp_files():
6060
_temp_files = {}
6161

6262

63-
def _create_temp_file_with_content(content, temp_file_path=None):
63+
def _create_temp_file_with_content(content, temp_file_path=None, force_recreate=False):
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)
69-
if content_key in _temp_files:
69+
if not force_recreate and content_key in _temp_files:
7070
return _temp_files[content_key]
7171
if temp_file_path and not os.path.isdir(temp_file_path):
7272
os.makedirs(name=temp_file_path)
@@ -115,16 +115,10 @@ def as_file(self):
115115
decoded obj[%data_key_name] content otherwise obj[%file_key_name]."""
116116
use_data_if_no_file = not self._file and self._data
117117
if use_data_if_no_file:
118-
if self._base64_file_content:
119-
if isinstance(self._data, str):
120-
content = self._data.encode()
121-
else:
122-
content = self._data
123-
self._file = _create_temp_file_with_content(
124-
base64.standard_b64decode(content), self._temp_file_path)
125-
else:
126-
self._file = _create_temp_file_with_content(
127-
self._data, self._temp_file_path)
118+
self._write_file()
119+
120+
if self._file and not os.path.isfile(self._file):
121+
self._write_file(force_rewrite=True)
128122
if self._file and not os.path.isfile(self._file):
129123
raise ConfigException("File does not exist: %s" % self._file)
130124
return self._file
@@ -142,6 +136,18 @@ def as_data(self):
142136
self._data = f.read()
143137
return self._data
144138

139+
def _write_file(self, force_rewrite=False):
140+
if self._base64_file_content:
141+
if isinstance(self._data, str):
142+
content = self._data.encode()
143+
else:
144+
content = self._data
145+
self._file = _create_temp_file_with_content(
146+
base64.standard_b64decode(content), self._temp_file_path, force_recreate=force_rewrite)
147+
else:
148+
self._file = _create_temp_file_with_content(
149+
self._data, self._temp_file_path, force_recreate=force_rewrite)
150+
145151

146152
class CommandTokenSource(object):
147153
def __init__(self, cmd, args, tokenKey, expiryKey):

0 commit comments

Comments
 (0)