"""파드 사이 연결 결과를 표로 보여 주는 점검 화면. - python view.py : 브라우저용 화면 (http://127.0.0.1:5420) - python view.py --text : 같은 표를 터미널에 출력 각 칸은 출발 파드 안에서 wget 으로 대상에 연결해 본 실제 결과입니다.""" import html import subprocess import sys import unicodedata from concurrent.futures import ThreadPoolExecutor from flask import Flask # (설명, 출발 네임스페이스, 출발 파드, 대상 주소, 수정 후 기대) CASES = [ ("outside 의 client -> web", "outside", "client", "web.shop:8080", True), ("web -> db", "shop", "web", "db.shop:5432", True), ("intruder -> db", "shop", "intruder", "db.shop:5432", False), ("intruder -> web", "shop", "intruder", "web.shop:8080", False), ("web -> intruder", "shop", "web", "intruder.shop:8080", False), ("outside 의 client -> db", "outside", "client", "db.shop:5432", False), ] app = Flask(__name__) def kubectl(*args, timeout=30): p = subprocess.run(["kubectl", *args], capture_output=True, text=True, timeout=timeout) return p.returncode, (p.stdout + p.stderr).strip() def probe(ns, pod, target): code, out = kubectl("exec", "-n", ns, pod, "--", "timeout", "4", "wget", "-T", "2", "-qO-", f"http://{target}/", timeout=30) return code == 0, out.splitlines()[0] if out else "" def run_all(): with ThreadPoolExecutor(max_workers=len(CASES)) as pool: results = list(pool.map(lambda c: probe(c[1], c[2], c[3]), CASES)) return [(label, target, ok, expect, body) for (label, _, _, target, expect), (ok, body) in zip(CASES, results)] def pad(text, width): """한글은 두 칸을 차지하므로 화면 너비를 기준으로 맞춥니다.""" used = sum(2 if unicodedata.east_asian_width(c) in "WF" else 1 for c in text) return text + " " * max(width - used, 0) def text_table(rows): widths = (28, 22, 8, 14) head = ("경로", "대상", "결과", "수정 후 기대") lines = [" | ".join(pad(h, w) for h, w in zip(head, widths)) + " | 판정"] for label, target, ok, expect, _ in rows: cells = (label, target, "연결됨" if ok else "차단", "허용" if expect else "차단") lines.append(" | ".join(pad(c, w) for c, w in zip(cells, widths)) + (" | 일치" if ok == expect else " | 불일치")) return "\n".join(lines) @app.get("/") def index(): rows = run_all() _, pols = kubectl("get", "networkpolicy", "-n", "shop", "--no-headers", "-o", "custom-columns=NAME:.metadata.name,POD:.spec.podSelector.matchLabels") _, pods = kubectl("get", "pods", "-A", "-o", "wide", "--no-headers", "--field-selector", "metadata.namespace!=kube-system,metadata.namespace!=local-path-storage") body = [] for label, target, ok, expect, _ in rows: match = ok == expect body.append( f"
{html.escape(target)}{html.escape(summary)} 수정 후 기대는 web 만 db 에 닿고, outside 에서는 web 의 8080 만 열리는 상태입니다.
| 경로 | 대상 | 지금 결과 | 수정 후 기대 | 판정 |
|---|
{html.escape(pods)}
{html.escape(pols) if pols.strip() else '없음'}"""
if __name__ == "__main__":
if "--text" in sys.argv:
print(text_table(run_all()))
else:
app.run(host="127.0.0.1", port=5420)