{"slug":"kubernetes-operator-2","title":"kubernetes-operator","summary":"Use when building a Kubernetes operator or CRD (controller-runtime, kubebuilder, operator-sdk, KOPF), before writing the controller. Validates CRDs with crd_validator.py, lints reconcile loops with reconcile_lint.py and scores OperatorHub capability level.","platform":"Claude","tags":["kubernetes","devops"],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-12-02T16:53:18.765794Z","repo":{"url":"https://github.com/beginwithu/agent-skills","stars":11,"forks":2,"license":"MIT","updatedAt":"2026-12-02T16:37:11Z"},"bodyHtml":"<hr>\n<h2>name: kubernetes-operator\ndescription: \"Use when building a Kubernetes operator or CRD (controller-runtime, kubebuilder, operator-sdk, KOPF), before writing the controller. Validates CRDs with crd_validator.py, lints reconcile loops with reconcile_lint.py and scores OperatorHub capability level.\"\ncontext: fork\nlicense: MIT\nmetadata:\nversion: 2.9.0\nauthor: claude-code-skills\ntags: [kubernetes, operator, crd, controller-runtime, kubebuilder, operator-sdk, metacontroller, kopf, reconcile, devops]\ncompatible_tools: [claude-code, codex-cli, cursor, antigravity, opencode, gemini-cli]</h2>\n<h1>Kubernetes Operator</h1>\n<p>Build operators that reconcile correctly. Most operator bugs are not Kubernetes bugs — they are reconcile-loop bugs: missing finalizers, blocking calls, no requeue on transient errors, status drift, RBAC over-grants. This skill catches them deterministically before they reach a cluster.</p>\n<h2>When to use</h2>\n<ul>\n<li>Building a new Kubernetes Operator (controller for a CRD)</li>\n<li>Reviewing an existing operator for capability-level gaps</li>\n<li>Auditing a CRD spec for status/conditions/finalizer correctness</li>\n<li>Choosing a framework (controller-runtime / kubebuilder / operator-sdk / metacontroller / KOPF)</li>\n<li>Designing the API surface of a Custom Resource</li>\n<li>Hardening RBAC, leader election, or webhook validation</li>\n</ul>\n<h2>When NOT to use</h2>\n<ul>\n<li>Plain Helm chart packaging → use <code>helm-chart-builder</code></li>\n<li>Standard kubectl operations / blue-green deploys → use <code>senior-devops</code></li>\n<li>General k8s security posture → use <code>cloud-security</code></li>\n<li>\"I want to run a workload\" — that's a Deployment / Job, not an operator</li>\n</ul>\n<h2>Core principle: an operator is a reconcile loop, not a script</h2>\n<pre><code>observe(actual) → desired = read(spec) → diff(actual, desired) → act → update(status)\n                                                                          ↓\n                                                                   requeue / done\n</code></pre>\n<p>Operators that fail are the ones that:</p>\n<ol>\n<li>Treat reconcile as imperative (do this, then this, then this) instead of declarative (make actual=desired, idempotently)</li>\n<li>Don't requeue transient failures</li>\n<li>Don't use finalizers, leaving orphan resources</li>\n<li>Mutate spec instead of status</li>\n<li>Don't use the status subresource (status updates trigger spec reconciles → loop)</li>\n<li>Block in reconcile (long HTTP calls, locks)</li>\n<li>Forget leader election → split-brain on multi-replica deploys</li>\n</ol>\n<p>The 3 tools below catch each of these.</p>\n<h2>Quick start</h2>\n<pre><code>SKILL=engineering/kubernetes-operator/skills/kubernetes-operator\n\n# Validate a CRD design\npython \"$SKILL/scripts/crd_validator.py\" --crd config/crd/myapp.yaml\n\n# Lint a Go reconcile function\npython \"$SKILL/scripts/reconcile_lint.py\" --controller controllers/myapp_controller.go\n\n# Score against OperatorHub Capability Levels (1-5)\npython \"$SKILL/scripts/operator_capability_audit.py\" --operator-dir .\n</code></pre>\n<h2>The 3 Python tools</h2>\n<p>All stdlib-only. Run with <code>--help</code>.</p>\n<h3><code>crd_validator.py</code></h3>\n<p>Validates a CRD YAML against operator-pattern best practices.</p>\n<pre><code>python scripts/crd_validator.py --crd config/crd/myapp.yaml\npython scripts/crd_validator.py --crd config/crd/ --format json\n</code></pre>\n<p><strong>Checks:</strong></p>\n<ul>\n<li><code>spec.versions[*].subresources.status</code> is set (status subresource)</li>\n<li><code>spec.scope</code> is <code>Namespaced</code> (not <code>Cluster</code>) unless explicitly justified</li>\n<li>Singular and listKind defined</li>\n<li><code>spec.versions[*].schema.openAPIV3Schema</code> has type definitions (no <code>x-kubernetes-preserve-unknown-fields: true</code> at top level)</li>\n<li>A version is marked <code>served: true</code> AND <code>storage: true</code></li>\n<li>Conditions array is in the schema (allows <code>metav1.Conditions</code>)</li>\n<li>Printer columns include <code>Age</code> and <code>Status</code>/<code>Phase</code></li>\n</ul>\n<h3><code>reconcile_lint.py</code></h3>\n<p>Lints a Go controller reconcile function for anti-patterns.</p>\n<pre><code>python scripts/reconcile_lint.py --controller controllers/myapp_controller.go\n</code></pre>\n<p><strong>Checks (regex-based heuristics):</strong></p>\n<ul>\n<li>Returns are <code>(ctrl.Result, error)</code> shape</li>\n<li>Errors trigger a non-zero requeue (<code>return ctrl.Result{Requeue: true}, err</code>)</li>\n<li><code>client.Update()</code> on the spec object is flagged (controllers should update only status)</li>\n<li><code>time.Sleep</code> inside reconcile is flagged (use <code>RequeueAfter</code>)</li>\n<li>HTTP calls without context cancellation are flagged</li>\n<li>Missing <code>defer</code> after a finalizer add</li>\n<li>No <code>IsConditionTrue</code> / <code>SetCondition</code> calls when conditions present in CRD</li>\n<li>Reconcile function exceeds 80 lines (extract subroutines)</li>\n</ul>\n<h3><code>operator_capability_audit.py</code></h3>\n<p>Scores an operator against OperatorHub's 5 Capability Levels.</p>\n<pre><code>python scripts/operator_capability_audit.py --operator-dir .\n</code></pre>\n<p><strong>Levels:</strong></p>\n<ul>\n<li><strong>L1 — Basic Install:</strong> CRD defined, controller deploys it</li>\n<li><strong>L2 — Seamless Upgrades:</strong> PDBs, conversion webhooks, version skew strategy</li>\n<li><strong>L3 — Full Lifecycle:</strong> backups, restores, failure recovery</li>\n<li><strong>L4 — Deep Insights:</strong> metrics endpoint, Prometheus rules, alerts</li>\n<li><strong>L5 — Auto Pilot:</strong> auto-scaling, auto-tuning, anomaly detection</li>\n</ul>\n<p>Reports current level + concrete next steps to advance one level.</p>\n<h2>Tooling landscape</h2>\n<p>Pick a framework based on language and complexity. See <code>references/tooling_landscape.md</code>.</p>\n<table>\n<thead>\n<tr>\n<th>Framework</th>\n<th>Language</th>\n<th>Best for</th>\n<th>Maintenance</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><strong>controller-runtime</strong></td>\n<td>Go</td>\n<td>Production-grade, low-level control</td>\n<td>Active (sig-api-machinery)</td>\n</tr>\n<tr>\n<td><strong>kubebuilder</strong></td>\n<td>Go</td>\n<td>Standard scaffolding, opinionated</td>\n<td>Active (Kubernetes SIGs)</td>\n</tr>\n<tr>\n<td><strong>operator-sdk</strong></td>\n<td>Go / Helm / Ansible</td>\n<td>OpenShift / mixed-paradigm teams</td>\n<td>Active (Red Hat)</td>\n</tr>\n<tr>\n<td><strong>metacontroller</strong></td>\n<td>Any (webhook-based)</td>\n<td>Polyglot teams, avoiding Go</td>\n<td>Less active</td>\n</tr>\n<tr>\n<td><strong>KOPF</strong></td>\n<td>Python</td>\n<td>Python shops, async-first</td>\n<td>Active (community)</td>\n</tr>\n<tr>\n<td><strong>java-operator-sdk</strong></td>\n<td>Java</td>\n<td>JVM shops</td>\n<td>Active (Red Hat / Java SIG)</td>\n</tr>\n</tbody>\n</table>\n<p>Decision rules:</p>\n<ul>\n<li>New operator + Go shop → kubebuilder</li>\n<li>New operator + Python shop → KOPF</li>\n<li>New operator + can't pick a language → metacontroller</li>\n<li>OpenShift target → operator-sdk</li>\n</ul>\n<h2>CRD design principles</h2>\n<p>See <code>references/crd_design.md</code> for full detail. Quick rules:</p>\n<ol>\n<li><strong>status is the source of truth for the controller's view of the world.</strong> Spec is what the user wants; status is what the controller observed.</li>\n<li><strong>Use the status subresource.</strong> Without it, status updates re-trigger reconcile (loop).</li>\n<li><strong>Use Conditions.</strong> <code>Ready</code>, <code>Reconciling</code>, <code>Degraded</code>. Each carries a reason and message.</li>\n<li><strong>Add finalizers.</strong> Without finalizers, deletion races the controller and orphans external resources.</li>\n<li><strong>Version your CRD from day 1.</strong> <code>v1alpha1</code> → <code>v1beta1</code> → <code>v1</code>. Plan a conversion webhook.</li>\n<li><strong>Validate via OpenAPI v3 schema.</strong> Don't rely on the controller for validation that should fail at admission.</li>\n<li><strong>Use <code>additionalPrinterColumns</code> for <code>kubectl get</code>.</strong> Show <code>Age</code>, <code>Phase</code>, <code>Ready</code> at minimum.</li>\n<li><strong>Namespace your CRDs unless they manage cluster-scoped resources.</strong></li>\n</ol>\n<h2>Reconcile loop principles</h2>\n<p>See <code>references/reconcile_loop.md</code> for full detail. Quick rules:</p>\n<ol>\n<li><strong>Idempotent.</strong> Reconciling the same state twice → same result, zero side effects.</li>\n<li><strong>Read once, decide, act.</strong> Don't observe the world repeatedly during reconcile.</li>\n<li><strong>Update status, not spec.</strong> Spec belongs to the user.</li>\n<li><strong>Return errors that requeue.</strong> Use <code>ctrl.Result{RequeueAfter: ...}</code> for known transient cases.</li>\n<li><strong>Never block.</strong> No <code>time.Sleep</code>. No long HTTP calls without context.</li>\n<li><strong>Use the cache.</strong> Read via the controller's cached client; only escape the cache for a specific reason.</li>\n<li><strong>Leader-elect when running &gt;1 replica.</strong> Otherwise enable single-replica mode.</li>\n<li><strong>Set OwnerReferences.</strong> Cascading deletion is the operator pattern's free gift.</li>\n</ol>\n<h2>Workflows</h2>\n<h3>Workflow 1: Bootstrap a new operator (Go + kubebuilder)</h3>\n<pre><code>1. Pick a Group/Version/Kind: e.g., apps.example.com/v1alpha1, kind=MyApp\n2. kubebuilder init --domain example.com --repo github.com/org/myapp-operator\n3. kubebuilder create api --group apps --version v1alpha1 --kind MyApp\n4. Run crd_validator.py on config/crd/bases/apps.example.com_myapps.yaml\n   → Fix every WARN before writing controller code\n5. Implement the reconcile function (Karpathy principle 2: simplest correct version first)\n6. Run reconcile_lint.py on controllers/myapp_controller.go\n7. Run operator_capability_audit.py --operator-dir . — confirm L1\n8. Test in a kind cluster: kubectl apply -f config/samples/\n9. Add status conditions; aim for L2 in the same PR\n</code></pre>\n<h3>Workflow 2: Audit an existing operator</h3>\n<pre><code>1. Run operator_capability_audit.py --operator-dir &lt;path&gt;\n2. Run crd_validator.py --crd config/crd/\n3. Run reconcile_lint.py --controller controllers/\n4. Triage findings:\n   - FAIL → block release; fix before next deploy\n   - WARN → file an issue; fix in next 30 days\n5. Document current capability level in README; commit\n6. Plan one capability level advancement per quarter\n</code></pre>\n<h3>Workflow 3: Choose a framework</h3>\n<pre><code>1. Identify primary language constraint (team skill)\n2. Identify deployment target (vanilla k8s vs OpenShift)\n3. Identify operator complexity (single CRD vs multi-CRD vs cluster-wide)\n4. Cross-reference with references/tooling_landscape.md\n5. Build a 1-week proof-of-concept before committing\n</code></pre>\n<h2>References</h2>\n<ul>\n<li><code>references/operator_pattern.md</code> — what an operator IS, when to use vs alternatives</li>\n<li><code>references/crd_design.md</code> — CRD design principles, versioning, conversion webhooks</li>\n<li><code>references/reconcile_loop.md</code> — reconcile patterns, error handling, idempotency</li>\n<li><code>references/tooling_landscape.md</code> — framework comparison + decision tree</li>\n</ul>\n<h2>Slash command</h2>\n<p><code>/operator-audit</code> — Run all 3 tools on an operator repo and produce a markdown report.</p>\n<h2>Asset templates</h2>\n<ul>\n<li><code>assets/crd_template.yaml</code> — CRD with status subresource, conditions, finalizer hint, printer columns</li>\n<li><code>assets/reconcile_skeleton.go</code> — Go controller reconcile function with idempotency, conditions, finalizers, requeue patterns</li>\n</ul>\n<h2>Anti-patterns</h2>\n<ul>\n<li><strong><code>time.Sleep(30 * time.Second)</code> inside reconcile</strong> — block other reconciles. Use <code>RequeueAfter</code>.</li>\n<li><strong><code>r.Client.Update(ctx, obj)</code> to set status</strong> — use <code>r.Status().Update(ctx, obj)</code> instead.</li>\n<li><strong>No leader election + 2+ replicas</strong> — split-brain.</li>\n<li><strong>No finalizer</strong> — external resources orphan on deletion.</li>\n<li><strong>CRD without status subresource</strong> — status updates trigger spec reconciles (infinite loop).</li>\n<li><strong>Reconcile function &gt; 200 lines</strong> — extract reconcileXxx subroutines per condition.</li>\n<li><strong><code>x-kubernetes-preserve-unknown-fields: true</code> on spec root</strong> — defeats validation.</li>\n<li><strong>Imperative reconcile</strong> — \"if creating, do A; if updating, do B; if deleting, do C\". Wrong shape. Reconcile = make actual=desired, regardless of how we got here.</li>\n</ul>\n<h2>Verifiable success</h2>\n<p>A team using this skill should achieve:</p>\n<ul>\n<li>100% of new CRDs pass <code>crd_validator.py</code> before merge</li>\n<li>All reconcile functions pass <code>reconcile_lint.py</code> strict mode</li>\n<li>Operators reach OperatorHub Capability Level 3 (Full Lifecycle) before public release</li>\n<li>Mean time to fix a reconcile bug: &lt;1 day (no infinite loops in production)</li>\n</ul>\n","files":[{"path":"assets/crd_template.yaml","sizeBytes":2620,"isText":true},{"path":"assets/reconcile_skeleton.go","sizeBytes":4052,"isText":false},{"path":"LICENSE","sizeBytes":1072,"isText":false},{"path":"references/crd_design.md","sizeBytes":6998,"isText":true},{"path":"references/operator_pattern.md","sizeBytes":7157,"isText":true},{"path":"references/reconcile_loop.md","sizeBytes":7275,"isText":true},{"path":"references/tooling_landscape.md","sizeBytes":7642,"isText":true},{"path":"scripts/crd_validator.py","sizeBytes":5939,"isText":true},{"path":"scripts/operator_capability_audit.py","sizeBytes":5916,"isText":true},{"path":"scripts/reconcile_lint.py","sizeBytes":6495,"isText":true},{"path":"SKILL.md","sizeBytes":10938,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-12-02T16:54:58.113887Z","sha256":"9E396BE638F7B791D738AC50897EC8ECCB617BD8D3BA12B3D212A2561DE72775","sizeBytes":27037},"review":null,"source":{"repositoryUrl":"https://github.com/beginwithu/agent-skills","path":"skills/kubernetes-operator","license":"MIT","commit":"a960304c52212f2e845067047f5cefc0c594d787","subtreeSha":"20B6D83F2211A7F1B328147A6660F0B83B4CEAF3569C607B8BFC6F3A78EE3C30","lastSyncedAt":"2026-12-02T16:52:59.20206Z"},"reviewedAt":"2026-12-02T16:57:55.254092Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/beginwithu/agent-skills/tree/main/skills/kubernetes-operator"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install beginwithu-agent-skills@llmmart"},{"target":"git","command":"git clone https://github.com/beginwithu/agent-skills.git"}]}