|
@@ -0,0 +1,284 @@
|
|
|
|
|
+# ISSUE:Agent / Component Controller 标签互写导致持续 reconcile 和高 CPU
|
|
|
|
|
+
|
|
|
|
|
+- **Issue ID**:ISSUE-2026-10-21-operator-reconcile-hot-loop
|
|
|
|
|
+- **状态**:Open
|
|
|
|
|
+- **严重程度**:P0 / 性能与稳定性
|
|
|
|
|
+- **发现时间**:2026-10-21(Asia/Shanghai)
|
|
|
|
|
+- **发现来源**:LambdAgent + PaaS 远程 Agent 联调
|
|
|
|
|
+- **代码基线**:`feat/lambagent-adapt`
|
|
|
|
|
+- **结论**:该问题属于 LocoStack Operator 原有控制器实现,不是 PaaS 远程创建功能引入。
|
|
|
|
|
+
|
|
|
|
|
+## 1. 现象
|
|
|
|
|
+
|
|
|
|
|
+Operator 启动后长期占用约 65%–70% 单核 CPU:
|
|
|
|
|
+
|
|
|
|
|
+```text
|
|
|
|
|
+PID %CPU COMMAND
|
|
|
|
|
+1717 69.4 ./manager --kubeconfig ...
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+同时:
|
|
|
|
|
+
|
|
|
|
|
+- `Agent` 资源稳定,但对应 `Component` 的 `resourceVersion` 和 `generation` 持续增长;
|
|
|
|
|
+- Component 每秒约增加 30–40 个 generation;
|
|
|
|
|
+- Operator 日志持续高频输出 `Reconciled Agent/Component/ExternalModel/ExternalTool`;
|
|
|
|
|
+- Agent reconcile 频繁报错:
|
|
|
|
|
+
|
|
|
|
|
+```text
|
|
|
|
|
+Operation cannot be fulfilled on components.locostack.com
|
|
|
|
|
+the object has been modified; please apply your changes to the latest version and try again
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+- PaaS 自身 CPU 较低,仅 LocoStack Operator 持续高负载。
|
|
|
|
|
+
|
|
|
|
|
+## 2. 运行证据
|
|
|
|
|
+
|
|
|
|
|
+### 2.1 Component 持续写 spec
|
|
|
|
|
+
|
|
|
|
|
+以 `agent-ter` 为例,连续读取:
|
|
|
|
|
+
|
|
|
|
|
+```text
|
|
|
|
|
+rv=1684738 gen=133251
|
|
|
|
|
+rv=1684875 gen=133289
|
|
|
|
|
+rv=1685008 gen=133322
|
|
|
|
|
+rv=1685142 gen=133356
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+Agent 本身保持不变,但 Component 的 generation 每秒增长约 30–40。
|
|
|
|
|
+
|
|
|
|
|
+### 2.2 API Server 请求量异常
|
|
|
|
|
+
|
|
|
|
|
+kind API Server 指标:
|
|
|
|
|
+
|
|
|
|
|
+```text
|
|
|
|
|
+components PUT: 308,695
|
|
|
|
|
+components/status PATCH: 368,861
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+这些请求远高于正常控制器稳态量。
|
|
|
|
|
+
|
|
|
|
|
+### 2.3 CPU profile
|
|
|
|
|
+
|
|
|
|
|
+`perf` 热点包含:
|
|
|
|
|
+
|
|
|
|
|
+```text
|
|
|
|
|
+AgentReconciler.reconcileComponent
|
|
|
|
|
+DefaultComponentReconciler.reconcileDeployment
|
|
|
|
|
+controllerutil.CreateOrUpdate
|
|
|
|
|
+Kubernetes object decode/update
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+说明 CPU 主要来自控制器持续执行 CreateOrUpdate、对象序列化和 API 交互。
|
|
|
|
|
+
|
|
|
|
|
+## 3. 根因
|
|
|
|
|
+
|
|
|
|
|
+### 3.1 Agent 与 Component 控制器重复管理同一标签
|
|
|
|
|
+
|
|
|
|
|
+Agent 控制器创建 Component 时写入:
|
|
|
|
|
+
|
|
|
|
|
+```go
|
|
|
|
|
+maps.Copy(comp.Labels, reconciler.ResourceLabels(
|
|
|
|
|
+ stackName, "Agent", agent.Name, "",
|
|
|
|
|
+))
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+因此 `ter` 对应的 Component 标签为:
|
|
|
|
|
+
|
|
|
|
|
+```text
|
|
|
|
|
+locostack.com/name=ter
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+但 `DefaultComponentReconciler.ReconcileComponent()` 又会改写为:
|
|
|
|
|
+
|
|
|
|
|
+```go
|
|
|
|
|
+r.component.Labels = r.ResourceLabels()
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+这里使用的是 Component 名称:
|
|
|
|
|
+
|
|
|
|
|
+```text
|
|
|
|
|
+locostack.com/name=agent-ter
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+### 3.2 两个值反复互相覆盖
|
|
|
|
|
+
|
|
|
|
|
+- Agent 控制器写入 `ter`;
|
|
|
|
|
+- Component 控制器写入 `agent-ter`;
|
|
|
|
|
+- Agent 控制器 `Watches(&v1alpha1.Component{}, ComponentEventHandler("Agent"))`;
|
|
|
|
|
+- Component 变化再次触发 Agent reconcile;
|
|
|
|
|
+- Agent 再次写入 `ter`。
|
|
|
|
|
+
|
|
|
|
|
+形成持续反馈循环。
|
|
|
|
|
+
|
|
|
|
|
+实测标签会在以下两个值之间反复切换:
|
|
|
|
|
+
|
|
|
|
|
+```text
|
|
|
|
|
+locostack.com/name:
|
|
|
|
|
+ ter
|
|
|
|
|
+ agent-ter
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+### 3.3 模板标签还有同类覆盖问题
|
|
|
|
|
+
|
|
|
|
|
+Component 控制器使用赋值:
|
|
|
|
|
+
|
|
|
|
|
+```go
|
|
|
|
|
+r.component.Labels = r.ResourceLabels()
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+会删除非 Operator 基础标签,例如 Gateway 上的:
|
|
|
|
|
+
|
|
|
|
|
+```text
|
|
|
|
|
+stack.locostack.com/default=true
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+Stack 控制器会重新添加,Component 控制器再删除,同样可能造成循环。
|
|
|
|
|
+
|
|
|
|
|
+### 3.4 CreateOrUpdate 缺少稳定态短路
|
|
|
|
|
+
|
|
|
|
|
+即使部分 reconcile 中 spec/metadata 的可见字段相同,`controllerutil.CreateOrUpdate` 仍可能持续发出等价 PUT。
|
|
|
|
|
+
|
|
|
|
|
+实测 Component 的 API 读取结果在短时间内除 `generation/observedGeneration` 外完全一致,但仍持续产生:
|
|
|
|
|
+
|
|
|
|
|
+```text
|
|
|
|
|
+PUT /apis/locostack.com/v1alpha1/namespaces/locostack/components/agent-ter
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+需要进一步确认并补充:
|
|
|
|
|
+
|
|
|
|
|
+- 稳定态语义比较;
|
|
|
|
|
+- 避免每次 reconcile 无条件重建 Template/Dependencies;
|
|
|
|
|
+- 检查 Status Patch 是否携带主资源字段变化。
|
|
|
|
|
+
|
|
|
|
|
+## 4. 影响
|
|
|
|
|
+
|
|
|
|
|
+- Operator 持续消耗 60%–70% 单核 CPU;
|
|
|
|
|
+- API Server 和 etcd 承受大量无意义写请求;
|
|
|
|
|
+- Agent reconcile 频繁冲突,状态收敛速度下降;
|
|
|
|
|
+- 多 Agent 场景下 CPU/API 压力会线性放大;
|
|
|
|
|
+- 可能影响 PaaS 远程 Agent 创建后的 Ready 时间和远程发现稳定性;
|
|
|
|
|
+- 与 PaaS 侧业务修改无直接因果关系。
|
|
|
|
|
+
|
|
|
|
|
+## 5. 复现方式
|
|
|
|
|
+
|
|
|
|
|
+1. 使用 `feat/lambagent-adapt` Operator 连接 kind 集群;
|
|
|
|
|
+2. 创建至少一个 LambdAgent Agent;
|
|
|
|
|
+3. 等待 Agent 和 Component 创建完成;
|
|
|
|
|
+4. 观察 CPU:
|
|
|
|
|
+
|
|
|
|
|
+```bash
|
|
|
|
|
+top -p <manager-pid>
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+5. 连续读取 Component generation:
|
|
|
|
|
+
|
|
|
|
|
+```bash
|
|
|
|
|
+kubectl get component agent-ter \
|
|
|
|
|
+ -o jsonpath='{.metadata.resourceVersion} {.metadata.generation}{"\n"}'
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+6. 观察标签在 `ter` 和 `agent-ter` 间切换:
|
|
|
|
|
+
|
|
|
|
|
+```bash
|
|
|
|
|
+kubectl get component agent-ter \
|
|
|
|
|
+ -o jsonpath='{.metadata.labels.locostack\.com/name}{"\n"}'
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+7. 查看 API Server metrics:
|
|
|
|
|
+
|
|
|
|
|
+```bash
|
|
|
|
|
+kubectl get --raw /metrics | grep 'resource="components"'
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+## 6. 建议修复
|
|
|
|
|
+
|
|
|
|
|
+### 6.1 明确单一标签所有者
|
|
|
|
|
+
|
|
|
|
|
+建议由 Component 控制器唯一管理 Component 的 Operator 标签。
|
|
|
|
|
+
|
|
|
|
|
+Agent 控制器只管理:
|
|
|
|
|
+
|
|
|
|
|
+- Component spec;
|
|
|
|
|
+- dependencies;
|
|
|
|
|
+- stackRef;
|
|
|
|
|
+- ownerReference。
|
|
|
|
|
+
|
|
|
|
|
+Agent 控制器不应写 `locostack.com/name`。
|
|
|
|
|
+
|
|
|
|
|
+### 6.2 Component 控制器保留额外标签
|
|
|
|
|
+
|
|
|
|
|
+不要使用:
|
|
|
|
|
+
|
|
|
|
|
+```go
|
|
|
|
|
+r.component.Labels = r.ResourceLabels()
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+应改为增量合并:
|
|
|
|
|
+
|
|
|
|
|
+```go
|
|
|
|
|
+if r.component.Labels == nil {
|
|
|
|
|
+ r.component.Labels = make(map[string]string)
|
|
|
|
|
+}
|
|
|
|
|
+maps.Copy(r.component.Labels, r.ResourceLabels())
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+保留 Gateway/Stack 等控制器添加的标签。
|
|
|
|
|
+
|
|
|
|
|
+### 6.3 稳定态不写 Component
|
|
|
|
|
+
|
|
|
|
|
+Agent 创建 Component 前应计算完整的期望 spec,并与当前对象做语义比较。
|
|
|
|
|
+
|
|
|
|
|
+建议至少比较:
|
|
|
|
|
+
|
|
|
|
|
+- Category
|
|
|
|
|
+- Template
|
|
|
|
|
+- Variables
|
|
|
|
|
+- Dependencies
|
|
|
|
|
+- StackRef
|
|
|
|
|
+- ownerReference
|
|
|
|
|
+
|
|
|
|
|
+仅当期望字段发生变化时调用 CreateOrUpdate。
|
|
|
|
|
+
|
|
|
|
|
+### 6.4 避免修改缓存对象中的嵌套 map
|
|
|
|
|
+
|
|
|
|
|
+检查是否存在从 informer/cache 对象直接复用 map、slice、pointer 后原地修改的情况。
|
|
|
|
|
+
|
|
|
|
|
+更新前应使用 `DeepCopy()`,避免共享缓存对象导致 watch 自身触发。
|
|
|
|
|
+
|
|
|
|
|
+### 6.5 增加回归测试
|
|
|
|
|
+
|
|
|
|
|
+建议增加 fake-client 控制器测试:
|
|
|
|
|
+
|
|
|
|
|
+```text
|
|
|
|
|
+第一次 reconcile:创建 Component
|
|
|
|
|
+第二次 reconcile:无 spec 变化,不应发生 Update
|
|
|
|
|
+稳定状态下连续 reconcile 10 次:resourceVersion/generation 不变
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+同时覆盖:
|
|
|
|
|
+
|
|
|
|
|
+- Agent label 与 Component label 不冲突;
|
|
|
|
|
+- Gateway 标签 `stack.locostack.com/default` 不被删除;
|
|
|
|
|
+- 外部模型/工具状态变化只触发必要 reconcile。
|
|
|
|
|
+
|
|
|
|
|
+## 7. 验收标准
|
|
|
|
|
+
|
|
|
|
|
+- [ ] Operator 稳态 CPU 低于 5% 单核
|
|
|
|
|
+- [ ] Component generation 在无配置变化时保持不变
|
|
|
|
|
+- [ ] Component 稳态 PUT 低于每分钟 1 次
|
|
|
|
|
+- [ ] 不再出现 Agent/Component 标签互写
|
|
|
|
|
+- [ ] Gateway 的 `stack.locostack.com/default` 标签保持稳定
|
|
|
|
|
+- [ ] Agent、Gateway、Model、Tool 状态仍能正常收敛
|
|
|
|
|
+- [ ] 远程 LambdAgent 创建、Ready、执行链路不受影响
|
|
|
|
|
+
|
|
|
|
|
+## 8. 与本次 PaaS 修改的边界
|
|
|
|
|
+
|
|
|
|
|
+PaaS 修改仅包括:
|
|
|
|
|
+
|
|
|
|
|
+- 调用 LocoStack Admin API 创建/更新 Agent;
|
|
|
|
|
+- 在 Agent payload / Component variables 中补齐 namespace;
|
|
|
|
|
+- 远程发现、状态探测和 UI。
|
|
|
|
|
+
|
|
|
|
|
+PaaS 不持续写 LocoStack Component,也不参与 Operator 的标签 reconciliation。
|
|
|
|
|
+
|
|
|
|
|
+因此本 issue 归入 LocoStack Operator 原有问题。PaaS 侧仅负责在 Operator 修复后重新构建镜像并完成端到端回归。
|