Golang管理Kubernetes CRD的核心是通过client-go构建类型安全客户端,需正确注册Scheme、选择类型化或动态客户端、执行CRUD操作,并用Informer监听事件;关键在Scheme注册顺序、ResourceVersion处理和Informer启动时机。
用 Golang 管理 Kubernetes CRD(CustomResourceDefinition)的核心,是通过 client-go 构建类型安全的客户端,结合自定义结构体和 Scheme 注册,实现对 CR 的增删改查、监听与事件处理。关键不在“能不能写”,而在“怎么注册得对、怎么调用得稳、怎么扩展得活”。
先在 YAML 中定义 CRD(如 example.com/v1alpha1/MyApp),再用 controller-gen 自动生成 Go 结构体和 clientset:
go install sigs.k8s.io/controller-tools/cmd/controller-gen@latest
// +kubebuilder:object:root=true 等注释,运行 controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
types.go 包含 MyApp 和 MyAppList,clientset/ 提供了类型化客户端CRD 资源必须被 Scheme “认识”,否则 client-go 无法序列化/反序列化:
scheme 中注册你的 CR 类型:myappv1alpha1.AddToScheme(scheme)
rest.InClusterConfig() 或 clientcmd.BuildConfigFromFlags() 获取 configclientset.MyappV1alpha1().MyApps(namespace),IDE 有提示、编译期检查强dynamic.NewForConfig(config),适合 CRD 尚未固定或需泛化处理的场景以类型化客户端为例,操作直观且不易出错:
client.MyApps("default").Create(ctx, &myapp, metav1.CreateOptions{})
client.MyApps("default").Get(ctx, "myapp-1", metav1.GetOptions{})
ObjectMeta.ResourceVersion 再调 Update(),避免冲突client.MyApps("default").Delete(ctx, "myapp-1", metav1.DeleteOptions{})
*v1alpha1.MyApp,不是 *unstructured.Unstructured
用 Informers 实现高效、低开销的事件监听(比轮询好,比直接 watch 稳):
sharedInformerFactory 获取你的 CR Informer:informer := informerFactory.MyappV1alpha1().MyApps("default")
informer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{...})
OnAdd/OnUpdate 中解析对象、触发业务逻辑(如创建 Job、更新 ConfigMap)informerFactory.Start(stopCh) 和 informerFactory.WaitForCacheSync(stopCh)
基本上就这些。不复杂但容易忽略的是 Scheme 注册顺序、ResourceVersion 处理、Informer 启动时机——这三处出错,90% 的 CR 操作会静默失败或 panic。