共享业务栈与专用验收实例共用同一个 WeKnora 镜像,原来只按镜像判"未声明写入者", 任何一台开着共享栈的机器都永远开不了窗。判据改为:同项目或无项目标签即冲突; 另一个项目只有数据库/队列目标与源相同才算冲突。单测覆盖六种组合。
This commit is contained in:
+53
-4
@@ -732,7 +732,11 @@ func runCapture(ctx context.Context, opts options) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return checkNoUndeclaredWriter(ctx, images, declared)
|
||||
app, err := containerEnv(ctx, opts.sourceApp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return checkNoUndeclaredWriter(ctx, images, declared, opts.project, sourceTargetOf(app))
|
||||
}
|
||||
result, err := v071.CaptureLibrary(ctx, coordinator, archive, pool, search, objects, sourceReady,
|
||||
v071.LibraryCaptureRequest{JobID: job, Scope: opts.scope,
|
||||
@@ -809,21 +813,66 @@ func writerImageIDs(ctx context.Context, items []libraryfreeze.Service) ([]strin
|
||||
// checkNoUndeclaredWriter refuses to run when a running container uses a declared
|
||||
// writer image but is not part of the declared group: such a container could keep
|
||||
// writing during the maintenance window.
|
||||
func checkNoUndeclaredWriter(ctx context.Context, images []string, declared map[string]bool) error {
|
||||
func checkNoUndeclaredWriter(ctx context.Context, images []string, declared map[string]bool,
|
||||
sourceProject string, source map[string]string) error {
|
||||
for _, image := range images {
|
||||
out, err := dockerOutput(ctx, "ps", "-q", "--filter", "ancestor="+image)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, id := range strings.Fields(out) {
|
||||
if !declared[id] {
|
||||
return fmt.Errorf("container %s uses a declared writer image but is not in the declared writer group", id[:12])
|
||||
if declared[id] {
|
||||
continue
|
||||
}
|
||||
project, err := dockerOutput(ctx, "inspect", "--format", `{{index .Config.Labels "com.docker.compose.project"}}`, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
env, err := containerEnv(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if conflict, reason := undeclaredWriterConflict(false, project, sourceProject, env, source); conflict {
|
||||
return fmt.Errorf("container %s uses a declared writer image but is not in the declared writer group: %s", id[:12], reason)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// sourceTargetOf 只取"写到哪儿"的身份:同一个数据库或同一个队列才是真冲突。
|
||||
func sourceTargetOf(env map[string]string) map[string]string {
|
||||
return map[string]string{
|
||||
"DB_HOST": env["DB_HOST"], "DB_NAME": env["DB_NAME"], "DB_PORT": env["DB_PORT"],
|
||||
"REDIS_ADDR": env["REDIS_ADDR"], "REDIS_NAMESPACE": env["REDIS_NAMESPACE"],
|
||||
}
|
||||
}
|
||||
|
||||
// undeclaredWriterConflict 判断一个同镜像容器是否构成"未声明的并发写入者"。
|
||||
//
|
||||
// 同一 compose 项目或没有项目标签 ⇒ 无法证明隔离,直接冲突。
|
||||
// 另一个项目 ⇒ 只有数据目标(数据库/队列)与源相同才算冲突:
|
||||
// 镜像相同不等于写同一个库,本机共享业务栈与专用验收实例就是这种关系。
|
||||
func undeclaredWriterConflict(declared bool, project, sourceProject string, env, source map[string]string) (bool, string) {
|
||||
if declared {
|
||||
return false, ""
|
||||
}
|
||||
if project == "" {
|
||||
return true, "没有 compose 项目标签,无法证明它不写同一个目标"
|
||||
}
|
||||
if project == sourceProject {
|
||||
return true, "属于同一 compose 项目,却不是声明的写入服务"
|
||||
}
|
||||
if source["DB_HOST"] != "" && env["DB_HOST"] == source["DB_HOST"] && env["DB_NAME"] == source["DB_NAME"] {
|
||||
return true, "另一个项目的同镜像容器指向同一个源数据库"
|
||||
}
|
||||
if source["REDIS_ADDR"] != "" && env["REDIS_ADDR"] == source["REDIS_ADDR"] &&
|
||||
env["REDIS_NAMESPACE"] == source["REDIS_NAMESPACE"] {
|
||||
return true, "另一个项目的同镜像容器共用同一个源队列"
|
||||
}
|
||||
return false, "另一套隔离栈(数据目标不同)"
|
||||
}
|
||||
|
||||
func containerImageOf(ctx context.Context, id string) (string, error) {
|
||||
image, err := dockerOutput(ctx, "inspect", "--format", "{{.Image}}", id)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user