DEBUG.WIKI
ZgoCloud:多地区部署,按需选节点;日本、美国、香港、德国
V.PS:让项目靠近用户,亚太、欧洲、北美 KVM VPS
晚安云;香港 CN2 ¥15/月

解决 ArgoCD 报错 ComparisonError cannot unmarshal into Go struct

分析 ArgoCD 应用同步状态为 ComparisonError 且 json cannot unmarshal 根因,详解 CRD schema 冲突、字段类型变更与忽略差异修复方案。

问题现象

在 ArgoCD 控制台上,应用处于 ComparisonError 状态,无法进行比对与自动同步(OutOfSync/Unknown),UI 和日志中提示报错:

ComparisonError: rpc error: code = Internal desc = Failed to load target state:
failed to unmarshal manifest: error unmarshaling JSON: while decoding ratio:
json: cannot unmarshal string into Go struct field ... of type int32

触发根因

  1. CRD 版本与 Schema 不匹配:Kubernetes 集群内已注册的 CRD OpenAPI 验证结构定义与 Git 仓库中声明的字段类型不一致(例如旧版定义为数字,而 Git 提交为带双引号的字符串)。
  2. K8s API 版本废弃或升级断层:Ingress、HorizontalPodAutoscaler 或第三方 Operator(如 Cert-Manager)升级后,字段结构从一维标量转为对象。
  3. ArgoCD 缓存了损坏的集群 API 发现资源:在 CRD 热更后,ArgoCD Repo-Server 未能及时刷新集群的 API Resource 缓存。

诊断排查步骤

  1. 查看目标资源在集群上的实际 CRD 字段定义:
kubectl explain <crd-kind>.spec.<problematic-field>
  1. 检查 Git 仓库中声明的该资源对应配置,对比两者的类型差异。

解决步骤与操作命令

步骤一:修正 Git 仓库中的字段类型

如果字段类型被要求为整数或布尔值,删除多余引号。例如:

# 错误写法
replicas: "3"
# 正确写法
replicas: 3

提交并推送到远端 Git 仓库。

步骤二:硬刷新 ArgoCD 应用状态

在 ArgoCD Web UI 上点击应用详情,点击右上角 REFRESH 下拉菜单,选择 Hard Refresh。 或者通过 CLI 执行:

argocd app get <app-name> --hard-refresh

步骤三:清除 Repo Server 缓存(如仍报错)

kubectl rollout restart deployment argocd-repo-server -n argocd
kubectl rollout restart deployment argocd-application-controller -n argocd

步骤四:对无法兼容的动态字段配置 ignoreDifferences

若该字段是由第三方 Admission Webhook 动态注入导致结构偏差,可在 Application CR 中配置忽略比对:

spec:
  ignoreDifferences:
  - group: apps
    kind: Deployment
    jsonPointers:
    - /spec/template/spec/containers/0/resources

再次执行 Sync 即可恢复正常。