prole/etc/render_manifest.py

114 lines
3.2 KiB
Python

#!/usr/bin/env python3
import os
import sys
from pathlib import Path
def _strip_blocks(text: str) -> str:
lines = text.splitlines()
out = []
skip_block = False
skip_indent = 0
i = 0
while i < len(lines):
line = lines[i]
stripped = line.lstrip()
if skip_block:
if not stripped:
i += 1
continue
indent = len(line) - len(stripped)
if indent > skip_indent:
i += 1
continue
skip_block = False
skip_indent = 0
continue
if stripped.startswith("affinity:"):
block_indent = len(line) - len(stripped)
block_lines = [line]
j = i + 1
while j < len(lines):
next_line = lines[j]
next_stripped = next_line.lstrip()
if not next_stripped:
block_lines.append(next_line)
j += 1
continue
next_indent = len(next_line) - len(next_stripped)
if next_indent <= block_indent:
break
block_lines.append(next_line)
j += 1
if any("kubernetes.io/hostname" in bl for bl in block_lines):
i = j
continue
out.extend(block_lines)
i = j
continue
if stripped.startswith("selector:"):
block_indent = len(line) - len(stripped)
block_lines = [line]
j = i + 1
while j < len(lines):
next_line = lines[j]
next_stripped = next_line.lstrip()
if not next_stripped:
block_lines.append(next_line)
j += 1
continue
next_indent = len(next_line) - len(next_stripped)
if next_indent <= block_indent:
break
block_lines.append(next_line)
j += 1
if any("synology.storage/" in bl for bl in block_lines):
i = j
continue
out.extend(block_lines)
i = j
continue
if stripped.startswith("nodeSelector:"):
skip_block = True
skip_indent = len(line) - len(stripped)
i += 1
continue
if stripped.startswith("storageClassName:") and "synology-iscsi" in stripped:
i += 1
continue
out.append(line)
i += 1
tail = "\n" if text.endswith("\n") else ""
return "\n".join(out) + tail
def main() -> int:
if len(sys.argv) < 2:
print("Usage: render_manifest.py <manifest.yaml>", file=sys.stderr)
return 2
path = Path(sys.argv[1]).expanduser()
if not path.exists():
print(f"ERROR: manifest not found: {path}", file=sys.stderr)
return 1
text = path.read_text()
mode = os.environ.get("KNOE_MODE", "").strip().lower()
if mode != "k3d":
sys.stdout.write(text)
return 0
sys.stdout.write(_strip_blocks(text))
return 0
if __name__ == "__main__":
raise SystemExit(main())