跳转至

从集群里读出一个 instance 的 identity

直到 2026-11-20,identity 被记录的地方只有 claim 上的一个 annotation(byre.io/identity,即客户端密钥的 fingerprint)。其余全是常量:SandboxTemplate 给每个 pod 都挂 hermes.profile: default,不管是谁占着它,于是 kubectl get pods 显示出一池一模一样的 sandbox,而那个合理却错误的结论是「default profile 跑了两份」。

现在让这件事可见的有两处:claim 上带着 byre 自己的 byre.io/identity-ref label,以及 operator 声明的 per-identity 值随 claim 经由 spec.additionalPodMetadata 传进 pod。template 不再声称自己知道 identity,因为一个常量比什么都没有更糟。

byre 写什么

在它绑定的每个对象上(claim 或 pod):

key kind 值
byre.io/identity annotation 精确归一化后的 fingerprint
byre.io/identity-ref label 同一个值,转写成 label 语法(+→-,/→_,= 丢弃)
byre.io/instance、byre.io/uid annotations 对象的 name 和 UID
identities[].labels 下的任何键 labels operator 声明;在 claims 路径上,key 必须带白名单里的域名
identities[].annotations 下的任何键 annotations operator 声明;会送到 pod,不需要域名

配置:

identities:
  - fingerprint: "WcHWo47zy7eNKb8CgeTkYX4rjr4CoNcp+xtNo63NYRE"
    backend: sandbox
    name: eng
    labels:
      sandbox.users.io/profile: eng   # 可以被 select 选中,且对 sandbox controller 合法
    annotations:
      hermes.profile: eng                    # 不带域名的那个 key,runbook 早就在 grep 它

key 在加载时就校验:一个 Kubernetes 会拒绝的值会让 -check-config 失败,而不是等到几小时后第一次 Acquire。byre.io/ 下的 label 和 app.kubernetes.io/managed-by 是保留的。

于是单看 claim 就能回答这些问题:

kubectl -n hermes get sandboxclaims \
  -o custom-columns='CLAIM:.metadata.name,IDENT:.metadata.annotations.byre\.io/identity,PROFILE:.metadata.labels.sandbox\.users\.io/profile'
kubectl -n hermes get sandboxclaims -l sandbox.users.io/profile=eng

怎么让它上到 pod

用 provision: claims 时,byre 创建一条 claim,sandbox controller 按 SandboxTemplate 建出 pod。byre 不拥有 pod,所以没法直接给它打 label。受支持的路径是 claim 的 spec.additionalPodMetadata:byre 在创建 claim 时填好,controller 再把它 merge 进 pod。template 里不能带常量 identity label:与不同值 merge 会被直接拒掉,而每个 pod 都是同一个值,正是这套改动要消掉的那个缺陷。

controller 在干任何别的事之前先校验这个字段

它看不顺眼的 key 不会只是丢掉那个 key——它会让整条 claim 失败,Ready=False, reason=InvalidMetadata,这个 identity 一点 sandbox 都拿不到。两条规则,而且不是同一条规则:

map 规则 你会看到的报错
labels 每个 key 都必须带域名前缀,且该域名在白名单里(默认 sandbox.users.io;子域名也通过) label "hermes.profile" must have a domain prefix 或 label domain "app.kubernetes.io" is not in the allowlist
annotations 只有一个三域名黑名单(kubernetes.io、k8s.io、agents.x-k8s.io) restricted system domain: …

所以 hermes.profile 是合法的 annotation、非法的 label。0.8.5 发出去的是 label 那种写法;每一条新 claim 都返回 InvalidMetadata,受影响的 identity 连不上。byre 现在在 -check-config 里按同一套默认值检查这两个 map,于是这种配置在 rollout 时就失败,而不是把集群搞挂:

identities[2] (WcHWo47…): labels: "hermes.profile" has no domain prefix: the sandbox controller
rejects the whole claim (Ready=False, reason=InvalidMetadata) …; use sandbox.users.io/profile

白名单由 controller 在启动时从 agent-sandbox-config(allowed-label-domains)读一次。byre 读不到它,所以如果某个集群确实允许另一个域名,要在两处都写上:

# byre 配置
backends:
  - name: sandbox
    provision: claims
    provider:
      allowed_label_domains: ["sandbox.users.io", "example.com"]
kubectl -n agent-sandbox-system create configmap agent-sandbox-config \
  --from-literal=allowed-label-domains='sandbox.users.io,example.com'
kubectl -n agent-sandbox-system rollout restart deploy/agent-sandbox-controller

这个值是替换默认值,不是追加,所以如果还有别的东西依赖 sandbox.users.io,就把它留在列表里;还要重启:文件只读一次。

用 pod 验证,不要用 claim 验证

kubectl -n hermes get sandboxclaims \
  -o custom-columns='CLAIM:.metadata.name,SANDBOX:.status.sandbox.name,READY:.status.conditions[0].status,IDENT:.metadata.annotations.byre\.io/identity'
kubectl -n hermes get pods -l 'app.kubernetes.io/part-of=hermes-sandbox' \
  -o custom-columns='POD:.metadata.name,PROFILE:.metadata.labels.sandbox\.users\.io/profile,ANNOT:.metadata.annotations.hermes\.profile'

claim 拥有的每个 pod 都该在 label 和 annotation 里显示自己的 identity。一个写着 default、sbx-eng 或任何其他常量的 pod 就是没迁移。scripts/verify-sandbox-lease-and-labels.sh 正是检查这个(check 3),从 claims 出发:没有 pod 的 claim,或者不是 Ready=True 的 claim,算失败,而不是一个空集判为通过。

如果 controller 比 additionalPodMetadata 老

kubectl explain sandboxclaim.spec.additionalPodMetadata 不用猜就能回答这个问题:结果为空,说明已安装的 CRD 里没有这个字段,byre 的 claim 如果带上它就会被拒。无论如何先把常量从 template 里删掉,然后分两跳做归属:

# claim → sandbox
kubectl -n hermes get sandboxclaim -l sandbox.users.io/profile=eng \
  -o custom-columns='CLAIM:.metadata.name,SANDBOX:.status.sandbox.name,IDENT:.metadata.annotations.byre\.io/identity'
# sandbox → pod
kubectl -n hermes get pods -l agents.x-k8s.io/sandbox=<sandbox-name>

另外值得记住:不同的 pool pod 是预期之内的。lease.pool_size 个 warm instance 在任何 identity 认领它们之前就已存在,所以几个没有 identity 的 sandbox 是正常的,而一个 warm pod 必须不带任何 profile label——它不属于任何人。缺陷从来不是数量,而是绑定了的那个和一个没绑定的分不出来。

这没解决什么

对象上的 identity 说明谁占着这个 instance。它完全没说重连的客户端拿到的是不是同一个 instance——那个问题以及它为什么是客户端的事,见 docs/lease-instance-identity.md。