mirror of
https://github.com/element-hq/ess-helm.git
synced 2026-11-30 22:02:56 -08:00
Merge pull request #1298 from element-hq/gaelg/memory-optimization
Extra file: lazy loading of discovered files
This commit is contained in:
5 files changed
+69
-15
No files matched your search
@@ -0,0 +1 @@
|
||||
Lazy load extra files instead of loading them in memory before dumping them into our output directory.
|
||||
@@ -400,32 +400,44 @@ class ExtraFilesDiscovery:
|
||||
Validate that a file path exists and is readable.
|
||||
|
||||
Args:
|
||||
path: Path to validate
|
||||
discovered_path: The discovered path to validate
|
||||
override_path: Optional path to use instead of discovered_path.source_path
|
||||
|
||||
Returns:
|
||||
True if file is valid, False if it's a binary file
|
||||
DiscoveredExtraFile with source_path set (content NOT loaded to save memory)
|
||||
|
||||
Raises:
|
||||
ExtraFilesError: If file doesn't exist or isn't readable
|
||||
"""
|
||||
file_path = override_path or Path(discovered_path.source_path)
|
||||
extra_file = DiscoveredExtraFile(filename=file_path.name, discovered_source_paths=[discovered_path])
|
||||
extra_file = DiscoveredExtraFile(
|
||||
filename=file_path.name,
|
||||
discovered_source_paths=[discovered_path],
|
||||
source_path=file_path, # Store path for lazy loading
|
||||
)
|
||||
|
||||
# Validate file exists
|
||||
if not file_path.exists():
|
||||
raise ExtraFilesError(f"Extra file does not exist: {file_path}")
|
||||
|
||||
# Validate it's a file (not a directory)
|
||||
if not file_path.is_file():
|
||||
raise ExtraFilesError(f"Extra file path is not a file: {file_path}")
|
||||
|
||||
# Check if file is binary
|
||||
# Check if file is binary BEFORE attempting to read
|
||||
if self._is_binary_file(file_path):
|
||||
extra_file.cleartext = False
|
||||
# Don't read binary files - they can't be ConfigMaps anyway
|
||||
return extra_file
|
||||
|
||||
# Validate we have permission to read the file
|
||||
try:
|
||||
with open(file_path):
|
||||
extra_file.content = file_path.read_bytes()
|
||||
pass # Just test opening, don't read content yet
|
||||
except PermissionError as err:
|
||||
raise ExtraFilesError(f"Permission denied when reading file: {file_path}") from err
|
||||
|
||||
# DON'T read content here - defer to when ConfigMap is created (saves memory)
|
||||
return extra_file
|
||||
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ from .secrets import SecretDiscovery
|
||||
from .utils import (
|
||||
find_matching_schema_key,
|
||||
get_nested_value,
|
||||
press_enter_to_continue,
|
||||
remove_nested_value,
|
||||
set_nested_value,
|
||||
sort_tracked_values_for_filtering,
|
||||
@@ -373,9 +374,40 @@ class ConfigValueTransformer:
|
||||
|
||||
configmap_data = {}
|
||||
|
||||
# Maximum file size to load into ConfigMap (1MB)
|
||||
# Larger files are skipped to prevent memory issues
|
||||
MAX_EXTRA_FILE_SIZE = 1 * 1024 * 1024
|
||||
|
||||
for discovered_extra_file in extra_files_discovery.discovered_extra_files.values():
|
||||
# Encode the file content
|
||||
configmap_data[discovered_extra_file.filename] = discovered_extra_file.content.decode("utf-8")
|
||||
# Skip non-cleartext files (binary files)
|
||||
if not discovered_extra_file.cleartext:
|
||||
logger.debug(f"Skipping non-cleartext file: {discovered_extra_file.filename}")
|
||||
continue
|
||||
|
||||
# Skip files that are too large
|
||||
if (
|
||||
discovered_extra_file.source_path is not None
|
||||
and discovered_extra_file.source_path.stat().st_size > MAX_EXTRA_FILE_SIZE
|
||||
):
|
||||
logger.warning(
|
||||
f"Skipping large file {discovered_extra_file.source_path} "
|
||||
f"({discovered_extra_file.source_path.stat().st_size} bytes > {MAX_EXTRA_FILE_SIZE} bytes)"
|
||||
)
|
||||
press_enter_to_continue(pretty_logger=self.pretty_logger)
|
||||
continue
|
||||
|
||||
# Read content from source_path (lazy loading - content read once here, not during discovery)
|
||||
if discovered_extra_file.source_path is not None:
|
||||
try:
|
||||
with open(discovered_extra_file.source_path, encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
configmap_data[discovered_extra_file.filename] = content
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to read file {discovered_extra_file.source_path}: {e}")
|
||||
continue
|
||||
else:
|
||||
logger.warning(f"No source_path for discovered extra file: {discovered_extra_file.filename}")
|
||||
continue
|
||||
|
||||
configmap = ConfigMap(name=configmap_name, data=configmap_data)
|
||||
|
||||
|
||||
@@ -102,8 +102,7 @@ class DiscoveredExtraFile:
|
||||
init=True
|
||||
) # Source configuration file (e.g., "synapse.yaml", "mas.yaml")
|
||||
filename: str = field(init=True) # Original file path in source file
|
||||
# and the file was discovered by listing its content
|
||||
content: bytes = field(default_factory=bytes) # Extra file content. File bigger than 100KiB will be skipped
|
||||
source_path: Path | None = field(default=None) # Path to the source file (lazy-loaded, not content)
|
||||
cleartext: bool = field(default=True) # If true, the file will be stored in a ConfigMap
|
||||
|
||||
|
||||
|
||||
@@ -56,9 +56,12 @@ def test_validate_extra_files_success(tmp_path):
|
||||
assert len(discovery.discovered_extra_files) == 2
|
||||
for file in discovery.discovered_extra_files.values():
|
||||
if file.discovered_source_paths[0] == test1:
|
||||
assert file.content == b"test content 1"
|
||||
# Content is now lazy-loaded from source_path
|
||||
assert file.source_path is not None
|
||||
assert file.source_path.read_bytes() == b"test content 1"
|
||||
elif file.discovered_source_paths[0] == test2:
|
||||
assert file.content == b"test: content 2"
|
||||
assert file.source_path is not None
|
||||
assert file.source_path.read_bytes() == b"test: content 2"
|
||||
else:
|
||||
pytest.fail("Unexpected file")
|
||||
|
||||
@@ -309,12 +312,15 @@ def test_duplicate_file_paths(tmp_path):
|
||||
|
||||
for path, discovered_file in discovery.discovered_extra_files.items():
|
||||
if path == str(tmp_path / "templates"):
|
||||
assert discovered_file.content == "header"
|
||||
# Content is now lazy-loaded from source_path
|
||||
assert discovered_file.source_path is not None
|
||||
assert discovered_file.source_path.read_text() == "header"
|
||||
assert len(discovered_file.discovered_source_paths) == 3
|
||||
for discovered in discovered_file.discovered_source_paths:
|
||||
assert discovered.source_path == str(tmp_path / "templates")
|
||||
elif path == str(tmp_path / "other"):
|
||||
assert discovered_file.content == "other"
|
||||
assert discovered_file.source_path is not None
|
||||
assert discovered_file.source_path.read_text() == "other"
|
||||
assert discovered_file.discovered_source_paths[0].source_path == str(tmp_path / "other")
|
||||
|
||||
|
||||
@@ -359,10 +365,14 @@ def test_binary_file_detection(tmp_path):
|
||||
for discovered_file in discovery.discovered_extra_files.values():
|
||||
if discovered_file.discovered_source_paths[0].source_path == text_file:
|
||||
assert discovered_file.cleartext
|
||||
assert discovered_file.content == text_file.read_bytes()
|
||||
# Text files have source_path set and can be read
|
||||
assert discovered_file.source_path is not None
|
||||
assert discovered_file.source_path.read_bytes() == text_file.read_bytes()
|
||||
if discovered_file.discovered_source_paths[0].source_path == binary_file:
|
||||
assert not discovered_file.cleartext
|
||||
assert discovered_file.content == binary_file.read_bytes()
|
||||
# Binary files have source_path set but cleartext=False
|
||||
assert discovered_file.source_path is not None
|
||||
assert discovered_file.source_path.read_bytes() == binary_file.read_bytes()
|
||||
|
||||
|
||||
def test_ignored_config_keys():
|
||||
|
||||
Reference in new issue
Block a user