feat(kubernetes): 新增 K8s 学习站内容生产套件与最终契约

- 01 路线:6 阶段 26 章,Aurora Shop 单主线串起从单 Pod 到生产平台

- 06 最终契约:度量口径、易错点四要素、边界去重、M0–M13 骨架、34 条门禁

- 04 黄金样例按新契约补齐图注、易错点、一句话与可跳过折叠

- tools/check-chapter.py:机检脚本,样例 22 项全绿

- 02 标记废弃,README 与 05 的投喂顺序改为 06+03+04
This commit is contained in:
2026-11-06 15:08:21 +08:00
parent f171808072
commit 851dd3d722
9 changed files with 2798 additions and 0 deletions

View File

@@ -0,0 +1,233 @@
#!/usr/bin/env python3
"""按《06-最终契约》第 9 节做章节页机检。
用法:
python tools/check-chapter.py 04-示例页-ch08-存储与有状态.md
python tools/check-chapter.py --all chapters/*.md
只实现 QG 中标为「机检」的项;人检项(QG-04/05/06/16/18/19/26/31/32/33/34)仍需人读。
退出码 0 = 全绿,1 = 有未通过项。
"""
import re
import sys
import glob
import os
CJK = re.compile(r'[\u4e00-\u9fff]')
MODULES = [
("M1", r'^##\s*本章在哪'),
("M2", r'^##\s*学习目标'),
("M3", r'^##\s*为什么需要它'),
("M4", r'^##\s*心智模型'),
("M5", r'^##\s*概念与字段速查'),
("M6", r'^##\s*动手'),
("M7", r'^##\s*验收清单'),
("M8", r'^##\s*故障实验室'),
("M9", r'^##\s*生产对照'),
("M10", r'^##\s*清理与回滚'),
("M11", r'^##\s*小结'),
("M12", r'^##\s*衔接'),
("M13", r'^##\s*参考'),
]
BLACKLIST = ["guestbook", "podinfo", "whoami", "nginx demo"]
def split_blocks(text):
"""返回 [(lang, body)] 与去掉代码块后的文本。"""
blocks, out, buf, lang = [], [], None, None
for line in text.split('\n'):
m = re.match(r'^```([a-zA-Z]*)', line)
if buf is None and m:
buf, lang = [], m.group(1)
continue
if buf is not None and line.startswith('```'):
blocks.append((lang, '\n'.join(buf)))
out.extend('<CODE>' for _ in buf)
buf = None
continue
if buf is not None:
buf.append(line)
else:
out.append(line)
if buf is not None: # 未闭合
blocks.append((lang, '\n'.join(buf)))
return blocks, '\n'.join(out)
def cjk(s):
return len(CJK.findall(s))
def paragraphs(nocode):
paras, cur = [], []
for line in nocode.split('\n'):
s = line.strip()
if not s:
if cur:
paras.append(' '.join(cur)); cur = []
continue
if s.startswith('#') or s.startswith('|') or s.startswith('<'):
if cur:
paras.append(' '.join(cur)); cur = []
continue
cur.append(re.sub(r'^[>\-\*\d\.\)]\s*', '', s))
if cur:
paras.append(' '.join(cur))
return paras
def check(path):
raw = open(path, encoding='utf-8').read()
blocks, nocode = split_blocks(raw)
rows = []
def add(qg, ok, detail):
rows.append((qg, bool(ok), detail))
# QG-02 front-matter
fm = re.match(r'^---\n(.*?)\n---\n', raw, re.S)
need = ['id:', 'slug:', 'title:', 'stage:', 'order:', 'mainline:', 'version_baseline:', 'last_verified:']
add('QG-02', fm and all(k in fm.group(1) for k in need),
'front-matter 缺失字段' if not fm else 'ok')
# QG-01 模块齐全有序
pos, missing = -1, []
for name, pat in MODULES:
m = re.search(pat, raw, re.M)
if not m:
missing.append(name); continue
if m.start() < pos:
missing.append(name + '(顺序错)')
pos = m.start()
add('QG-01', not missing, '缺失/乱序: ' + ','.join(missing) if missing else '13 个模块齐全')
# QG-03 make verify
fid = (fm.group(1) if fm else '')
idnum = re.search(r'id:\s*ch(\d\d)', fid)
want = f'make verify-ch{idnum.group(1)}' if idnum else None
add('QG-03', want and want in raw, want or 'front-matter 缺 id')
bash = [b for b in blocks if b[0] in ('bash', 'sh')]
txt = [b for b in blocks if b[0] == 'text']
yaml = [b for b in blocks if b[0] in ('yaml', 'yml')]
mermaid = [b for b in blocks if b[0] == 'mermaid']
add('QG-07', bash and all(re.match(r'\s*#\s*env:\s*(all|wsl|linux|linux-multi)', b[1]) for b in bash),
f'{len(bash)} 个 bash 块')
add('QG-08', txt and all(b[1].lstrip().startswith('# 预期输出') for b in txt),
f'{len(txt)} 个输出块')
# QG-09/10 双栏
win = len(re.findall(r'^#### Windows 11 \+ WSL2\s*$', raw, re.M))
lin = len(re.findall(r'^#### Linux \(Ubuntu 24\.04\)\s*$', raw, re.M))
concl = len(re.findall(r'> \*\*差异结论\*\*:', raw))
add('QG-09', win == lin == concl, f'Windows={win} Linux={lin} 差异结论={concl}')
# QG-11 占位符
bad = []
if re.search(r'(image:.*:latest)|(tag:\s*latest)', raw):
bad.append('latest')
if re.search(r'^\s*\.\.\.\s*$', raw, re.M):
bad.append('...')
if re.search(r'<[^>]*(你的|占位|TODO|xxx)[^>]*>', raw):
bad.append('占位符')
add('QG-11', not bad, ','.join(bad) if bad else 'ok')
# QG-12/13 图与图注
caps = re.findall(r'^> \*\*图 (\d+)\*\*', raw, re.M)
refs = re.findall(r'见图 (\d+)', raw)
add('QG-12', 2 <= len(mermaid) <= 5 and len(caps) == len(mermaid),
f'图={len(mermaid)} 图注={len(caps)}')
seq = [int(c) for c in caps] == list(range(1, len(caps) + 1))
add('QG-13', refs and set(refs) <= set(caps) and seq,
f'引用={sorted(set(refs))} 图注编号连续={seq}')
# QG-14 表格
tables = len(re.findall(r'^\|\s*---', raw, re.M))
m5 = re.search(r'^##\s*概念与字段速查(.*?)^##', raw, re.M | re.S)
add('QG-14', tables >= 5 and m5 and '默认值' in m5.group(1), f'表格={tables}')
# QG-15 易错点
errs = re.findall(r'> \*\*易错点\*\*:(.*?)(?=\n\n|\Z)', raw, re.S)
good = [e for e in errs if all(k in e for k in ('现象', '原因', '怎么验证'))]
add('QG-15', 3 <= len(errs) <= 5 and len(good) == len(errs),
f'易错点={len(errs)} 四要素齐全={len(good)}')
# QG-17 M9 行数
m9 = re.search(r'^##\s*生产对照(.*?)^##', raw, re.M | re.S)
m9rows = len([l for l in (m9.group(1) if m9 else '').split('\n')
if l.strip().startswith('|') and '---' not in l]) - 1
add('QG-17', m9rows >= 4, f'M9 数据行={max(m9rows, 0)}')
# QG-20 黑名单
hit = [b for b in BLACKLIST if b in raw.lower()]
add('QG-20', not hit, ','.join(hit) if hit else 'ok')
# QG-21/22/23 篇幅与块数
# 口径:排除代码块、表格行、标题行;包含列表项与引用块(见契约 §1.1)
prose_lines = [l for l in nocode.split('\n')
if not l.strip().startswith('|') and not l.strip().startswith('#')]
total = cjk('\n'.join(prose_lines))
add('QG-21', 1800 <= total <= 3200, f'正文汉字={total}')
add('QG-22', 8 <= len(bash) <= 20 and 6 <= len(txt) <= 18 and len(yaml) <= 6,
f'命令={len(bash)} 输出={len(txt)} yaml={len(yaml)}')
add('QG-23', not re.search(r'^\s*\$\s+kubectl|^PS>', raw, re.M), 'ok')
# QG-24 工作目录
add('QG-24', '/mnt/c' not in raw or '不得用' in raw, 'ok' if '/mnt/c' not in raw else '出现 /mnt/c')
# QG-25 常量:命名空间必须落在 shop/platform/kube-system,域名不得自造
ns = re.findall(r'namespace:\s*(\S+)', raw)
bad_ns = [n for n in ns if n not in ('shop', 'platform', 'kube-system')]
bad_dom = [d for d in re.findall(r'[a-z0-9.-]+\.example\.[a-z]+', raw)
if d not in ('shop.example.com', 'api.shop.example.com')]
add('QG-25', not bad_ns and not bad_dom,
f'命名空间={sorted(set(ns))}' + (f' 非法={bad_ns + bad_dom}' if bad_ns or bad_dom else ''))
# QG-27 段落
paras = [p for p in paragraphs(nocode) if p.strip()]
lens = [cjk(p) for p in paras]
over = [l for l in lens if l > 200]
add('QG-27', not over, f'段数={len(paras)} 最长={max(lens) if lens else 0} 超200={len(over)}')
# QG-28 密度
inline = len(re.findall(r'`[^`\n]+`', '\n'.join(
l for l in nocode.split('\n') if not l.strip().startswith('|'))))
dens = inline / total * 100 if total else 0
add('QG-28', dens <= 7.0, f'密度={dens:.2f}')
# QG-29 一句话
takes = re.findall(r'^> \*\*一句话\*\*:(.*)$', raw, re.M)
tl = [cjk(t) for t in takes]
add('QG-29', 3 <= len(takes) <= 6 and all(x <= 50 for x in tl),
f'一句话={len(takes)} 最长={max(tl) if tl else 0}')
# QG-30 可跳过
skip = len(re.findall(r'<summary>可跳过', raw))
add('QG-30', 2 <= skip <= 4, f'可跳过={skip}')
return rows
def main(argv):
files = argv[1:]
if '--all' in files:
files = sorted(glob.glob('*.md'))
if not files:
print('用法: check-chapter.py <章节目录或文件>'); return 2
failed = 0
for f in files:
rows = check(f)
bad = [r for r in rows if not r[1]]
failed += len(bad)
print(f"\n=== {os.path.basename(f)} ===")
for qg, ok, detail in rows:
print(f" {'✔' if ok else '✘'} {qg} {detail}")
print(f" -- {'通过' if not bad else f'{len(bad)} 项未通过'}")
return 1 if failed else 0
if __name__ == '__main__':
sys.exit(main(sys.argv))