feat(ci): PLAN 动态映射——条目自带 repos/keys/appFile/valuesFiles 即信任(格式校验),三张字典降级为缺省

- ADR-0075 加服务不改程序:门户登记映射(DB)随 PLAN 条目透传,promote 端信任执行
- valuesFiles 钉版新路径(独立 chart 服务如 wifi):替换客户 values 的 image.tag,
  兼容整行 repo:tag / repository+tag 拆分 / image 块三形态
- keys 空 → 不动 image-version;appFile 空 → 不随统一 chart(独立 chart 自然编码)
- deliver 缺省 harborTag:有 keys 查 image-version,无 keys 查 demo 的 values 文件
- ALL → DEFAULT_SERVICES(缺省全量 deliver 名单);DB_CHANGE 按计划内服务校验
- 兼容:六服务不带映射回退字典;未知服务非 keep 拒绝、keep 放行(留痕)
- 演练:PROMOTE_DRY_RUN 双路径(wifi pin-acr 钉直推 tag / deliver retag 新版本)+
  缺省全量回归,均在 /tmp 沙盒 GIT_CONFIG_GLOBAL 隔离下通过
This commit is contained in:
ximenchuifeng committed 2026-09-08 14:52:54 +08:00
1 parent 6df6d014cc
commit 6450d3084c
1 file changed
+167 -33
+167 -33
View File
@@ -12,14 +12,18 @@
# 逐镜像记录 digest(.digests,供 release-notes 反向追踪)
# chart chart 仓库 Chart.yaml bump + commit + tag chart-vX.Y.Z + push
# (tag 已存在则复用跳过;纯复用发布整步跳过)
# pin release 分支:按 PLAN 逐服务钉 image tag 与 chart targetRevision,
# pin release 分支:按 PLAN 逐服务钉 image tag(image-version keys 与/或
# valuesFiles 里的 image.tag)与 chart targetRevision(appFile 非空时),
# 追加 release-notes.yaml(最新在前),push
#
# 服务计划 PLAN(JSON 数组,每服务一条;未出现的服务 = keep):
# [{"service":"sso","action":"deliver","harborTag":"develop-v.1.0.1-202608250629-41"},
# {"service":"agent","action":"reuse","version":"v1.0.1"},
# {"service":"ultra","action":"keep"}]
# service: gateway / sso / ultra / audit / agent / edge(前后端一对,共用 tag)
# {"service":"ultra","action":"keep"},
# {"service":"wifi","action":"pin-acr","acrTag":"develop-v.1.0.1-202609080530-26",
# "repos":["network-agent"],"keys":[],"appFile":"",
# "valuesFiles":["network-agent-values.yaml"]}]
# service: 任意服务名(六内置 + 带映射的自定义服务,见下)
# action : keep(缺省,不动)
# reuse(复用已交付版本 version,镜像已在 ACR 不重推)
# deliver(harborTag 为未交付过的 Harbor tag,缺省取 demo 当前,retag 到新版本号)
@@ -27,9 +31,19 @@
# pin-acr(acrTag 为 ACR 上已有 tag,原样钉进客户目录,不搬运不 retag——
# 镜像搬运已剥离给门户「直推 ACR」;版本号即所选 tag 名,
# chart 引用保持不变,ArgoCD 由 values 变更触发 sync)
# 交付映射(PLAN 条目自带,带任一字段即整体信任,缺省字段视为空):
# repos Harbor 镜像仓列表(retag/pin 的镜像来源;自带映射时必填)
# keys environments/<客户>/image-version.yaml 的行名(空 = 不进版本表)
# appFile applications/<客户>/ 下的 Application 文件(空 = 不随统一 chart,
# 即独立 chart 服务,Application 引用其自有 chart 的 master)
# valuesFiles environments/<客户>/ 下的 values 文件列表(钉版落点:替换其中
# image.tag;形态兼容 整行 registry/repo:tag / repository+tag 拆分 /
# image: 块下 tag:)
# 未带映射的条目回退 REPOS/KEYS/APPFILE 三张缺省字典(六内置服务);
# 非 keep 动作必须能落点:keys 与 valuesFiles 至少一项非空。
# 兼容参数: SERVICES=sso,agent(=这些服务 deliver,其余 keep)
# IMAGES=sso=tagX(=该服务 deliver 且显式 harborTag;逗号分隔多个)
# PLAN 存在时优先;全部缺省 = 6 服务全 deliver demo 当前 tag(与改造前一致)
# PLAN 存在时优先;全部缺省 = 6 内置服务全 deliver demo 当前 tag(与改造前一致)
# protocol mock 不进 PLAN,保留 PROMOTE_PROTOCOLS 独立开关
#
# 版本号分配:
@@ -148,10 +162,11 @@ CHART_ONLY = os.environ.get("CHART_ONLY", "false") == "true"
WITH_PROTOCOLS = os.environ.get("PROMOTE_PROTOCOLS", "false") == "true"
HARBOR_HOST = os.environ.get("HARBOR_HOST", "192.168.1.119:8888")
# ── 服务映射常量表(pin 步骤经 .plan.json 继承同一份定义,勿另起副本)──
ALL = ["gateway", "sso", "ultra", "audit", "agent", "edge"]
# ── 交付映射:PLAN 条目自带映射优先(信任 + 格式校验),三张字典是未带时的缺省 ──
# (pin 步骤经 .plan.json 继承每条目解析后的映射,勿另起副本)
DEFAULT_SERVICES = ["gateway", "sso", "ultra", "audit", "agent", "edge"]
ACTIONS = ["keep", "reuse", "deliver", "ci-now", "pin-acr"]
# 服务 → 镜像 repo 列表(前后端共用同一 tag)
# 缺省服务 → 镜像 repo 列表(前后端共用同一 tag)
REPOS = {
"gateway": ["gateway"],
"sso": ["sso", "sso-web"],
@@ -160,7 +175,7 @@ REPOS = {
"agent": ["agent", "agent-web"],
"edge": ["edge", "edge-web"],
}
# 服务 → image-version.yaml 中 images 下的 key 列表
# 缺省服务 → image-version.yaml 中 images 下的 key 列表
KEYS = {
"gateway": ["gateway"],
"sso": ["sso", "ssoWeb"],
@@ -169,7 +184,7 @@ KEYS = {
"agent": ["agent", "agentWeb"],
"edge": ["edge", "edgeWeb"],
}
# 服务 → applications/<客户>/ 下的 Application 文件
# 缺省服务 → applications/<客户>/ 下的 Application 文件
APPFILE = {
"gateway": "gateway.yaml",
"sso": "sso.yaml",
@@ -179,16 +194,61 @@ APPFILE = {
"edge": "edges.yaml",
}
MAPPING_FIELDS = ("repos", "keys", "appFile", "valuesFiles")
def norm_mapping(svc, e):
"""PLAN 条目映射:带任一映射字段 = 整体信任(缺省字段视为空,不与字典合并);
未带映射 = 三张缺省字典;字典也没有 = None(keep 可无映射,非 keep 拒绝)"""
if not any(f in e for f in MAPPING_FIELDS):
if svc in REPOS:
return {"repos": REPOS[svc], "keys": KEYS[svc],
"appFile": APPFILE[svc], "valuesFiles": []}
return None
def field_list(name, pat, what):
raw = e.get(name) or []
if not isinstance(raw, list):
die(f"服务 {svc} 的映射字段 {name} 应为列表")
vals = []
for x in raw:
v = str(x).strip()
if v:
if not re.fullmatch(pat, v):
die(f"服务 {svc} 的映射字段 {name} 含非法值: {v}(应为{what})")
vals.append(v)
return vals
m = {
"repos": field_list("repos", r"[a-z0-9][a-z0-9._-]*", "镜像仓名"),
"keys": field_list("keys", r"[A-Za-z0-9][A-Za-z0-9_-]*", "image-version 行名"),
"appFile": "",
"valuesFiles": field_list("valuesFiles", r"[\w.-]+\.ya?ml", "values 文件名"),
}
app = str(e.get("appFile") or "").strip()
if app:
if not re.fullmatch(r"[\w.-]+\.ya?ml", app):
die(f"服务 {svc} 的映射字段 appFile 含非法值: {app}(应为文件名)")
m["appFile"] = app
if not m["repos"]:
die(f"服务 {svc} 自带映射缺 repos(Harbor 镜像仓,retag/pin 的镜像来源)")
return m
def die(msg):
sys.exit("[promote] 错误: " + msg)
data = yaml.safe_load(open(demo_path, encoding="utf-8")) or {}
images = data.get("images") or {}
def demo_tag(svc):
"""deliver/ci-now 缺省 harborTag = demo 当前 tag(前后端应共用同一 tag)"""
def demo_tag(svc, mapping):
"""deliver/ci-now 缺省 harborTag = demo 当前 tag(前后端应共用同一 tag)。
有 keys 从 image-version 取;无 keys(独立 chart 服务)从 demo 的首个 values 文件取"""
if not mapping["keys"]:
p = os.path.join(os.path.dirname(demo_path), mapping["valuesFiles"][0])
if os.path.exists(p):
t = re.search(r"tag:\s*[\"']?([\w.\-]+)", open(p, encoding="utf-8").read())
if t:
return t.group(1)
die(f"服务 {svc} 缺省 harborTag 无法从 demo 的 {mapping['valuesFiles'][0]} 读取,请显式指定")
tags = []
for k in KEYS[svc]:
for k in mapping["keys"]:
node = images.get(k)
if not isinstance(node, dict) or not node.get("tag"):
die(f"demo image-version 缺少 images.{k}.tag,无法取 {svc} 的 demo 当前 tag")
@@ -232,23 +292,34 @@ else:
e["harborTag"] = t
entries.append(e)
else:
# 全部缺省 = 6 服务全 deliver demo 当前 tag(行为与改造前一致)
entries = [{"service": s, "action": "deliver"} for s in ALL]
# 全部缺省 = 6 内置服务全 deliver demo 当前 tag(行为与改造前一致)
entries = [{"service": s, "action": "deliver"} for s in DEFAULT_SERVICES]
# ── 2. 校验 + 规范化为全量 6 服务计划 ──
# ── 2. 校验 + 规范化(缺省六服务 + PLAN 带映射的额外服务,全量计划)──
norm, seen = {}, set()
extra_order = [] # 非缺省字典的服务(PLAN 出现序;keep 条目也进,留痕一致)
for e in entries:
svc = e.get("service")
if svc not in ALL:
die(f"非法服务名: {svc}(合法值: {', '.join(ALL)})")
if not isinstance(svc, str) or not svc.strip():
die(f"PLAN 条目缺 service 或非字符串: {e}")
svc = svc.strip()
if svc in seen:
die(f"PLAN 中服务重复: {svc}")
seen.add(svc)
action = e.get("action", "keep")
if action not in ACTIONS:
die(f"非法 action: {action}(合法值: {', '.join(ACTIONS)})")
n = {"service": svc, "action": action,
"repos": REPOS[svc], "keys": KEYS[svc], "appFile": APPFILE[svc]}
mapping = norm_mapping(svc, e)
if mapping is None:
# keep 允许无映射(纯 no-op 留痕);非 keep 必须自带映射或命中缺省字典
if action != "keep":
die(f"服务 {svc} 无交付映射(PLAN 未带映射,也不在缺省字典: {', '.join(DEFAULT_SERVICES)}),只能 keep")
mapping = {"repos": [], "keys": [], "appFile": "", "valuesFiles": []}
if action != "keep" and not mapping["keys"] and not mapping["valuesFiles"]:
die(f"服务 {svc} 非 keep 动作但映射无钉版落点(keys/valuesFiles 均空)")
if svc not in REPOS:
extra_order.append(svc)
n = {"service": svc, "action": action, **mapping}
if action == "reuse":
ver = str(e.get("version") or "")
if not re.fullmatch(r"v\d+\.\d+\.\d+", ver):
@@ -256,7 +327,7 @@ for e in entries:
n["version"] = ver
elif action in ("deliver", "ci-now"):
tag = str(e.get("harborTag") or "").strip()
n["harborTag"] = tag or demo_tag(svc)
n["harborTag"] = tag or demo_tag(svc, mapping)
n["harborTagExplicit"] = bool(tag)
elif action == "pin-acr":
tag = str(e.get("acrTag") or "").strip()
@@ -265,16 +336,21 @@ for e in entries:
n["acrTag"] = tag
norm[svc] = n
plan_out = [norm.get(s) or {"service": s, "action": "keep",
"repos": REPOS[s], "keys": KEYS[s], "appFile": APPFILE[s]}
for s in ALL]
"repos": REPOS[s], "keys": KEYS[s], "appFile": APPFILE[s],
"valuesFiles": []}
for s in DEFAULT_SERVICES]
plan_out += [norm.get(s) or {"service": s, "action": "keep",
"repos": [], "keys": [], "appFile": "", "valuesFiles": []}
for s in extra_order]
has_deliver = any(e["action"] in ("deliver", "ci-now", "pin-acr") for e in plan_out)
# ── 3. DB_CHANGE 校验:逗号分隔服务名,须为本次非 keep 服务的子集 ──
# ── 3. DB_CHANGE 校验:逗号分隔服务名,须为本次计划内非 keep 服务的子集 ──
db_change = [s.strip() for s in (os.environ.get("DB_CHANGE") or "").split(",") if s.strip()]
known = {e["service"] for e in plan_out}
nonkeep = {e["service"] for e in plan_out if e["action"] != "keep"}
for s in db_change:
if s not in ALL:
die(f"DB_CHANGE 含非法服务名: {s}(合法值: {', '.join(ALL)})")
if s not in known:
die(f"DB_CHANGE 含未在计划中的服务名: {s}(计划内: {', '.join(sorted(known))})")
if s not in nonkeep:
die(f"DB_CHANGE 服务 {s} 本次为 keep,不在交付范围内(非 keep 服务: {', '.join(sorted(nonkeep)) or '无'})")
@@ -543,19 +619,21 @@ if os.path.exists(digests_path):
iv_path = os.path.join(envdir, "image-version.yaml")
rn_path = os.path.join(envdir, "release-notes.yaml")
# ── 1) image-version.yaml:只钉非 keep 服务的 key(CHART_ONLY 不动镜像文件)──
# ── 1) image-version.yaml:只钉有 keys 且非 keep 的服务(CHART_ONLY 不动镜像文件)──
from_tags, to_tags = {}, {}
def new_tag_of(e):
if e["action"] in ("deliver", "ci-now"):
return RELEASE_VERSION
if e["action"] == "pin-acr":
return e["acrTag"] # 所选 ACR tag 原样钉入,不 retag
return e["version"]
if not CHART_ONLY:
data = yaml.safe_load(open(iv_path, encoding="utf-8")) or {}
images = data.get("images") or {}
for e in actions:
for e in [x for x in actions if x["keys"]]:
svc = e["service"]
if e["action"] in ("deliver", "ci-now"):
new_tag = RELEASE_VERSION
elif e["action"] == "pin-acr":
new_tag = e["acrTag"] # 所选 ACR tag 原样钉入,不 retag
else:
new_tag = e["version"]
new_tag = new_tag_of(e)
to_tags[svc] = new_tag
for i, key in enumerate(e["keys"]):
node = images.get(key)
@@ -573,6 +651,59 @@ if not CHART_ONLY:
yaml.safe_dump(data, open(iv_path, "w", encoding="utf-8"),
default_flow_style=False, sort_keys=False, allow_unicode=True)
print(f"[promote] {iv_path} 钉版完成(未选中服务原样保留)")
# ── 1.5) valuesFiles 服务:逐文件替换镜像 tag(独立 chart 服务的钉版落点)──
# 三种形态:A. <repo>:<tag> 整行 B. repository: 行 + 相邻 tag:
# C. image: 块下相邻 tag:(registry 由 _common/registry.yaml 提供、
# 仓名由 chart 缺省提供,客户 values 只钉 tag 时无 repo 行)
def replace_values_tag(vpath, repo, new_tag):
"""就地替换 vpath 中服务镜像的 tag,返回旧 tag;三种形态均未命中返回 None"""
lines = open(vpath, encoding="utf-8").readlines()
pat_a = re.compile("(" + re.escape(repo) + r"):([A-Za-z0-9][\w.\-]*)")
pat_b = re.compile(r"^(\s*tag:\s*)(\S+)\s*$")
for idx, line in enumerate(lines):
if not repo or repo not in line:
continue
m = pat_a.search(line)
if m: # 形态 A
old = m.group(2).strip("\"'")
lines[idx] = pat_a.sub(lambda _m: _m.group(1) + ":" + new_tag, line)
open(vpath, "w", encoding="utf-8").write("".join(lines))
return old
for j in range(idx + 1, min(idx + 4, len(lines))): # 形态 B
mb = pat_b.match(lines[j])
if mb:
old = mb.group(2).strip("\"'")
lines[j] = mb.group(1) + new_tag + "\n"
open(vpath, "w", encoding="utf-8").write("".join(lines))
return old
for idx, line in enumerate(lines): # 形态 C:首个 image: 块
if re.match(r"^image:\s*(#.*)?$", line):
for j in range(idx + 1, min(idx + 4, len(lines))):
mb = pat_b.match(lines[j])
if mb:
old = mb.group(2).strip("\"'")
lines[j] = mb.group(1) + new_tag + "\n"
open(vpath, "w", encoding="utf-8").write("".join(lines))
return old
break
return None
for e in [x for x in actions if x.get("valuesFiles")]:
svc = e["service"]
new_tag = new_tag_of(e)
olds = []
for vf in e["valuesFiles"]:
vpath = os.path.join(envdir, vf)
if not os.path.exists(vpath):
sys.exit(f"[promote] 错误: 客户目录缺 {vf}(服务 {svc} 的钉版 values 文件)")
old = replace_values_tag(vpath, e["repos"][0], new_tag)
if old is None:
sys.exit(f"[promote] 错误: {vf} 中未找到服务 {svc} 的镜像 tag(整行/拆分/image 块三种形态均未命中)")
olds.append(old)
print(f"[promote] {svc}: {vf} {old} → {new_tag}")
if svc not in from_tags:
from_tags[svc] = " / ".join(olds)
to_tags[svc] = new_tag
else:
print("[promote] CHART_ONLY=true:image-version.yaml 不动")
@@ -600,6 +731,9 @@ def retarget(path, new_ref, required):
return old_ref, new_ref
for e in actions:
if not e["appFile"]:
print(f"[promote] {e['service']}: 无 appFile 映射,chart 引用不动(独立 chart 服务)")
continue
new_ref = CHART_TAG if e["action"] in ("deliver", "ci-now", "pin-acr") else f"chart-{e['version']}"
r = retarget(os.path.join(appdir, e["appFile"]), new_ref, required=True)
if r: