Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions kubernetes/base/config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ def _cleanup_temp_files():
_temp_files = {}


def _create_temp_file_with_content(content, temp_file_path=None):
def _create_temp_file_with_content(content, temp_file_path=None, force_recreate=False):
if len(_temp_files) == 0:
atexit.register(_cleanup_temp_files)
# Because we may change context several times, try to remember files we
# created and reuse them at a small memory cost.
content_key = str(content)
if content_key in _temp_files:
if not force_recreate and 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)
Expand Down Expand Up @@ -115,16 +115,10 @@ def as_file(self):
decoded obj[%data_key_name] content otherwise obj[%file_key_name]."""
use_data_if_no_file = not self._file and self._data
if use_data_if_no_file:
if self._base64_file_content:
if isinstance(self._data, str):
content = self._data.encode()
else:
content = self._data
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._write_file()

if self._file and not os.path.isfile(self._file):
self._write_file(force_rewrite=True)
if self._file and not os.path.isfile(self._file):
raise ConfigException("File does not exist: %s" % self._file)
return self._file
Expand All @@ -142,6 +136,18 @@ def as_data(self):
self._data = f.read()
return self._data

def _write_file(self, force_rewrite=False):
if self._base64_file_content:
if isinstance(self._data, str):
content = self._data.encode()
else:
content = self._data
self._file = _create_temp_file_with_content(
base64.standard_b64decode(content), self._temp_file_path, force_recreate=force_rewrite)
else:
self._file = _create_temp_file_with_content(
self._data, self._temp_file_path, force_recreate=force_rewrite)


class CommandTokenSource(object):
def __init__(self, cmd, args, tokenKey, expiryKey):
Expand Down