Skip to content

Commit a8a9ff2

Browse files
Merge pull request #154 from fmount/cmaps-optimize
Fix double YAML round-trip in ConfigMap splitting
2 parents 95605c7 + b75ea13 commit a8a9ff2

2 files changed

Lines changed: 32 additions & 15 deletions

File tree

pyscripts/cmaps.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414

1515
# Import mask module for applying masking
1616
try:
17-
from mask import mask_resource
17+
from mask import mask_data
1818
except ImportError:
19-
# Fallback if mask module not found
20-
mask_resource = None
19+
mask_data = None
2120

2221

2322
def split_configmaps(input_file, output_dir='configmaps', apply_mask=False):
@@ -50,6 +49,9 @@ def split_configmaps(input_file, output_dir='configmaps', apply_mask=False):
5049
configmaps = data.get('items', [])
5150
created_files = []
5251

52+
total = len(configmaps)
53+
print(f"Processing {total} ConfigMaps from {input_file}", flush=True)
54+
5355
# Process each ConfigMap
5456
for i, cm in enumerate(configmaps, 1):
5557
# Extract metadata
@@ -60,21 +62,20 @@ def split_configmaps(input_file, output_dir='configmaps', apply_mask=False):
6062
filename = f"{name}.yaml"
6163
filepath = output_path / filename
6264

63-
# Write the ConfigMap to its own file
64-
with open(filepath, 'w') as f:
65-
yaml.dump(cm, f, default_flow_style=False, sort_keys=False)
66-
67-
created_files.append(str(filepath))
65+
print(f" [{i}/{total}] {name}", flush=True)
6866

69-
# Apply masking if requested and mask_resource is available
70-
if apply_mask and mask_resource:
71-
for file_path in created_files:
67+
if apply_mask and mask_data:
7268
try:
73-
mask_resource(file_path)
69+
mask_data(cm, str(filepath))
7470
except Exception as e:
75-
print(f"Warning: Could not mask {file_path}: {e}")
76-
elif apply_mask and not mask_resource:
77-
print("Warning: Masking requested but mask module not available")
71+
print(f"Warning: Could not mask {filepath}: {e}")
72+
elif apply_mask and not mask_data:
73+
print(f"Warning: Masking requested but mask module not available")
74+
else:
75+
with open(filepath, 'w') as f:
76+
yaml.dump(cm, f, default_flow_style=False, sort_keys=False)
77+
78+
created_files.append(str(filepath))
7879

7980
return created_files
8081

pyscripts/mask.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ def _applyMaskRecursive(self, obj: Any) -> Any:
292292
"""
293293
if isinstance(obj, dict):
294294
for key, value in obj.items():
295+
if key == "binaryData":
296+
continue
295297
if isinstance(value, str):
296298
# If key name matches sensitive pattern and value is single-line, fully mask
297299
# This catches: password: secret123, transport_url: mysql://..., etc.
@@ -337,6 +339,20 @@ def get_resource_kind(path: str) -> Optional[str]:
337339
return None
338340

339341

342+
def mask_data(data: Dict[str, Any], path: str) -> bool:
343+
"""
344+
Mask an already-parsed resource dict and write the result to path.
345+
Avoids a redundant YAML load when the caller already has the data
346+
in memory (e.g. after splitting a ConfigMapList).
347+
"""
348+
if not data:
349+
return True
350+
m = PlaintextMask(path)
351+
m._applyMaskRecursive(data)
352+
m._writeYaml(data)
353+
return True
354+
355+
340356
def mask_resource(path: str, dump_conf: bool = False) -> bool:
341357
"""
342358
Dispatcher function that determines the resource type and applies

0 commit comments

Comments
 (0)