llm-wiki wiki · sources 2026-06-14

原文:raw/controller-runtime-architecture-analysis.md · 仓库:https://github.com/kubernetes-sigs/controller-runtime · 优先级 P0

一句话定位

controller-runtime 是现代 Kubernetes controller 的通用库,封装 Manager、cache、client、reconcile、webhook、envtest 等生产控制器骨架。

核心架构图

┌────────────────────────────────────────────────────────────────────────────┐
│ Custom controller binary                                                   │
│ Operator code is built around controller-runtime primitives.               │
└────────────────────────────────────────────────────────────────────────────┘
                                       │
                                       ▼
┌────────────────────────────────────────────────────────────────────────────┐
│ Manager                                                                    │
│ Scheme, leader election, metrics, health probes, webhooks, cache, and      │
│ lifecycle.                                                                 │
└────────────────────────────────────────────────────────────────────────────┘
                                       │
                                       ▼
┌────────────────────────────────────────────────────────────────────────────┐
│ Controller layer                                                           │
│ Informer cache, typed client, workqueue, reconciler, retry, status, and    │
│ finalizers.                                                                │
└────────────────────────────────────────────────────────────────────────────┘
                                       │
                                       ▼
┌────────────────────────────────────────────────────────────────────────────┐
│ Runtime boundary                                                           │
│ Kubernetes API server, admission webhooks, and envtest form the execution  │
│ surface.                                                                   │
└────────────────────────────────────────────────────────────────────────────┘

模块分层

层 / 模块 职责
Manager lifecycle、leader election、scheme、metrics
Cache/Client informer cache + API writer
Controller/Reconciler workqueue and reconcile loop
Webhook/envtest admission and test harness

关键数据流

manager 启动 cache/webhook/controllers
        │
        ▼
watch 事件进入 workqueue
        │
        ▼
reconciler 读取 cache/API
        │
        ▼
patch status/finalizer/owned resources
        │
        ▼
错误重试或完成

设计决策与哲学