Skip to content

Commit 1d6c076

Browse files
authored
Merge pull request #2471 from NicoPolazzi/fix/recreate-deleted-temp-files
Fix/recreate deleted temp files
2 parents 4ecaa15 + 25f5c73 commit 1d6c076

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

kubernetes/base/config/kube_config.py

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

6969

70-
def _create_temp_file_with_content(content, temp_file_path=None):
70+
def _create_temp_file_with_content(content, temp_file_path=None, force_recreate=False):
7171
if len(_temp_files) == 0:
7272
atexit.register(_cleanup_temp_files)
7373
# Because we may change context several times, try to remember files we
7474
# created and reuse them at a small memory cost.
7575
content_key = str(content)
76-
if content_key in _temp_files:
76+
if not force_recreate and content_key in _temp_files:
7777
return _temp_files[content_key]
7878
if temp_file_path and not os.path.isdir(temp_file_path):
7979
os.makedirs(name=temp_file_path)
@@ -122,16 +122,10 @@ def as_file(self):
122122
decoded obj[%data_key_name] content otherwise obj[%file_key_name]."""
123123
use_data_if_no_file = not self._file and self._data
124124
if use_data_if_no_file:
125-
if self._base64_file_content:
126-
if isinstance(self._data, str):
127-
content = self._data.encode()
128-
else:
129-
content = self._data
130-
self._file = _create_temp_file_with_content(
131-
base64.standard_b64decode(content), self._temp_file_path)
132-
else:
133-
self._file = _create_temp_file_with_content(
134-
self._data, self._temp_file_path)
125+
self._write_file()
126+
127+
if self._file and not os.path.isfile(self._file):
128+
self._write_file(force_rewrite=True)
135129
if self._file and not os.path.isfile(self._file):
136130
raise ConfigException("File does not exist: %s" % self._file)
137131
return self._file
@@ -149,6 +143,18 @@ def as_data(self):
149143
self._data = f.read()
150144
return self._data
151145

146+
def _write_file(self, force_rewrite=False):
147+
if self._base64_file_content:
148+
if isinstance(self._data, str):
149+
content = self._data.encode()
150+
else:
151+
content = self._data
152+
self._file = _create_temp_file_with_content(
153+
base64.standard_b64decode(content), self._temp_file_path, force_recreate=force_rewrite)
154+
else:
155+
self._file = _create_temp_file_with_content(
156+
self._data, self._temp_file_path, force_recreate=force_rewrite)
157+
152158

153159
class CommandTokenSource(object):
154160
def __init__(self, cmd, args, tokenKey, expiryKey):

kubernetes/base/config/kube_config_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,26 @@ def test_file_given_no_object_data(self):
267267
data_key_name=TEST_DATA_KEY)
268268
self.assertEqual(t.as_data(), None)
269269

270+
def test_file_recreation(self):
271+
obj = {TEST_DATA_KEY: TEST_DATA_BASE64}
272+
t1 = FileOrData(
273+
obj=obj,
274+
file_key_name=TEST_FILE_KEY,
275+
data_key_name=TEST_DATA_KEY,
276+
)
277+
first_file_path = t1.as_file()
278+
# We manually remove the file from the disk leaving it in the cache
279+
os.remove(first_file_path)
280+
281+
t2 = FileOrData(
282+
obj=obj,
283+
file_key_name=TEST_FILE_KEY,
284+
data_key_name=TEST_DATA_KEY,
285+
)
286+
287+
second_file_path = t2.as_file()
288+
self.assertEqual(TEST_DATA, self.get_file_content(second_file_path))
289+
270290

271291
class TestConfigNode(BaseTestCase):
272292

0 commit comments

Comments
 (0)