Files
ljwx-k8s/CACHE-IMAGE-BUGFIX.md
2026-02-20 22:50:45 +08:00

5.1 KiB
Raw Permalink Blame History

cache-image.sh 多前缀镜像处理 BUG 修复

日期: 2025-12-31 问题镜像: registry.k8s.io/prometheus-adapter/prometheus-adapter:v0.12.0 状态: ✅ 已修复


问题分析

传递给 EU Harbor 的镜像路径 ✅

结论: 传递给 EU Harbor 的镜像路径完整且正确,无问题。

对于镜像 registry.k8s.io/prometheus-adapter/prometheus-adapter:v0.12.0:

  1. parse_image_name 处理:

    • 移除 registry 前缀: registry.k8s.io/
    • 结果: prometheus-adapter/prometheus-adapter:v0.12.0 ✅
  2. 传递给 EU Harbor library:

    harbor.eu.lingjingwanxiang.cn/library/prometheus-adapter/prometheus-adapter:v0.12.0
    

    ✅ 完整路径

  3. 传递给 EU Harbor dockerhub-proxy:

    harbor.eu.lingjingwanxiang.cn/dockerhub-proxy/prometheus-adapter/prometheus-adapter:v0.12.0
    

    ✅ 完整路径


发现的 BUG ❌

问题描述

在 update_deployment 函数(cache-image.sh:397-398)中,更新 Deployment 时使用的镜像路径与实际缓存的路径不一致:

操作 位置 使用的函数 生成的路径
缓存存储 第154、194行 normalize_for_local_storage harbor.omniverseai.net/library/prometheus-adapter-prometheus-adapter:v0.12.0 ✅
更新 Deployment 第397-398行(修复前) 仅 parse_image_name harbor.omniverseai.net/library/prometheus-adapter/prometheus-adapter:v0.12.0 ❌

路径不匹配 → Deployment 更新后找不到镜像 → Pod 仍然 ImagePullBackOff

原因分析

cache_single_image 使用 normalize_for_local_storage 将路径扁平化(/ → -)以避免 Harbor 项目路径问题,但 update_deployment 没有使用相同的扁平化逻辑。


修复方案

修改内容

文件: cache-image.sh 位置: 第397-399行

修复前:

while IFS=: read -r container_name original_image; do
    local normalized_image=$(parse_image_name "$original_image")
    local new_image="${LOCAL_HARBOR}/${HARBOR_PROJECT}/${normalized_image}"  # ❌ 未扁平化

修复后:

while IFS=: read -r container_name original_image; do
    local normalized_image=$(parse_image_name "$original_image")
    local local_image_name=$(normalize_for_local_storage "$normalized_image")  # ✅ 扁平化
    local new_image="${LOCAL_HARBOR}/${HARBOR_PROJECT}/${local_image_name}"

验证结果

一致性验证

对于 registry.k8s.io/prometheus-adapter/prometheus-adapter:v0.12.0:

  • ✅ cache_single_image 存储: harbor.omniverseai.net/library/prometheus-adapter-prometheus-adapter:v0.12.0
  • ✅ update_deployment 更新: harbor.omniverseai.net/library/prometheus-adapter-prometheus-adapter:v0.12.0
  • ✅ 路径一致

多种镜像格式测试

原始镜像 解析后 本地存储(扁平化)
registry.k8s.io/prometheus-adapter/prometheus-adapter:v0.12.0 prometheus-adapter/prometheus-adapter:v0.12.0 prometheus-adapter-prometheus-adapter:v0.12.0 ✅
registry.k8s.io/metrics-server/metrics-server:v0.7.2 metrics-server/metrics-server:v0.7.2 metrics-server-metrics-server:v0.7.2 ✅
gcr.io/kaniko-project/executor:latest kaniko-project/executor:latest kaniko-project-executor:latest ✅
quay.io/prometheus/node-exporter:v1.8.2 prometheus/node-exporter:v1.8.2 prometheus-node-exporter:v1.8.2 ✅
docker.io/library/nginx:alpine library/nginx:alpine library-nginx:alpine ✅
nginx:alpine nginx:alpine nginx:alpine ✅
aquasec/trivy-operator:0.29.0 aquasec/trivy-operator:0.29.0 aquasec-trivy-operator:0.29.0 ✅
mysql/mysql-server:8.0 mysql/mysql-server:8.0 mysql-mysql-server:8.0 ✅

影响范围

修复前受影响的操作

❌ ./cache-image.sh --update-deployment <ns/deploy>

  • 镜像缓存成功,但 Deployment 更新后引用错误路径
  • Pod 无法启动(ImagePullBackOff)

不受影响的操作

✅ ./cache-image.sh <image>(单镜像缓存) ✅ ./cache-image.sh -f images.txt(批量缓存) ✅ ./cache-image.sh --from-pod <ns/pod>(从 Pod 缓存)


推荐使用方式

修复后,推荐使用以下方式处理 ImagePull 问题:

# 自动缓存并更新 Deployment(一键修复)
./cache-image.sh --update-deployment kubernetes-dashboard/kubernetes-dashboard

# 缓存单个镜像
./cache-image.sh registry.k8s.io/prometheus-adapter/prometheus-adapter:v0.12.0

# 批量缓存
./cache-image.sh registry.k8s.io/prometheus-adapter/prometheus-adapter:v0.12.0 \
                registry.k8s.io/metrics-server/metrics-server:v0.7.2

总结

  1. ✅ 传递给 EU Harbor 的镜像路径完整且正确
  2. ✅ 修复了 update_deployment 函数的路径不一致问题
  3. ✅ 支持所有多前缀镜像格式(registry.k8s.io, gcr.io, quay.io 等)
  4. ✅ 修复后缓存和更新路径完全一致

修复状态: ✅ 完成 测试状态: ✅ 通过 可用性: ✅ 正常