kubernetes-operator
2
总安装量
2
周安装量
#70434
全站排名
安装命令
npx skills add https://github.com/stormingluke/copilot --skill kubernetes-operator
Agent 安装分布
amp
2
github-copilot
2
codex
2
kimi-cli
2
gemini-cli
2
cursor
2
Skill 文档
Controller-Runtime Patterns
Reconciler Structure
- Implement
reconcile.Reconcilerinterface - Always accept and propagate
context.Context - Return
ctrl.Result{}with appropriate requeue:Result{}â success, no requeueResult{RequeueAfter: 30 * time.Second}â delayed retryResult{Requeue: true}â immediate retry
- Never return an error for expected/permanent failures â log and return
Result{} - Return errors only for transient failures that should be retried
CRD Design
- Group:
<domain>.example.com/v1alpha1âv1beta1âv1 - Status subresource always enabled
- Use status conditions following
metav1.Conditionpattern:Type,Status(True/False/Unknown),Reason,Message,LastTransitionTime
- Printer columns for
kubectl getoutput - Validation via CEL expressions in CRD markers
Finalizers
- Add finalizer on creation if external cleanup is needed
- Check
DeletionTimestampbefore reconciling - Remove finalizer only after cleanup succeeds
- Pattern:
if obj.DeletionTimestamp != nil { if controllerutil.ContainsFinalizer(obj, finalizerName) { // cleanup external resources controllerutil.RemoveFinalizer(obj, finalizerName) return ctrl.Result{}, r.Update(ctx, obj) } return ctrl.Result{}, nil }
Owner References
- Set owner references for all child resources
- Use
controllerutil.SetControllerReference()for single-owner - Use
controllerutil.SetOwnerReference()for shared ownership - Watch owned resources with
.Owns(&corev1.ConfigMap{})
RBAC
- Use kubebuilder RBAC markers:
//+kubebuilder:rbac:groups=...,resources=...,verbs=... - Principle of least privilege â only request what the controller needs
- Separate ClusterRole for cluster-scoped vs namespace-scoped resources
Testing
- Unit tests:
fake.NewClientBuilder().WithScheme(s).WithObjects(objs...).Build() - Integration tests:
envtest.Environmentwith real etcd + API server - Test the full reconcile loop: create â reconcile â verify â update â reconcile â verify
- Test idempotency: running reconcile twice should produce the same result