agentleFS
leroyguillaumeleroyguillaume/claude/kubernetes-operator-conventions/SKILL.md

kubernetes-operator-conventions

Skill7 starsChanged 3 days agoMITView on GitHub

Kubernetes operator/controller reconcile loops, any framework. TRIGGER when: editing reconcile handlers or controllers (`kopf`, `controller-runtime`, `operator-sdk`, `Reconcile()`); handling errors, retries, events or finalizers in a controller; generating CRD manifests; setting a field manager; documenting an operator's README; user asks about requeue, CRDs or status conditions. SKIP when: not operator code.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

9 rules. About 0 are common, 4 are distinctive, and 5 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    A resource is never dropped into a permanently-failed state: Every failure must requeue the object so it recovers on its own once the cause …

    line 25
  2. Distinctive:

    The requeue logic converges on one chokepoint: when the third handler repeats the extract-cause, log, requeue sequence, extract it into one …

    line 54
  3. Very specific:

    Treat already-exists (409) on create and not-found (404) on delete as success, not failure — log and continue.

    line 72
  4. Distinctive:

    Prefer create-or-skip / select-then-insert over blind inserts so a retry after a partial run never duplicates or errors.

    line 74
  5. Very specific:

    Use a finalizer only for teardown Kubernetes can't do itself: external, non-Kubernetes resources (SaaS workspaces, DB rows, cloud objects).

    line 82
  6. Very specific:

    When generating CRD YAML (e.g.

    line 88
  7. Very specific:

    Default the command with no output dir to a multi-document YAML stream on stdout (all kinds, ----separated).

    line 106
  8. Very specific:

    Every write to the API (server-side apply, JSON/merge/strategic-merge patch, create, update) must set an explicit, stable field_manager …

    line 127
  9. Distinctive:

    Document failure/retry semantics in the project README.md (a *Troubleshooting* section): where to look (events + logs), and that every …

    line 171

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Kubernetes operator conventions

These rules govern any reconcile path: the handler itself and everything it calls (clients, token providers, DB access), whatever the framework. The kopf wiring is kopf-conventions; in controller-runtime and Operator SDK, requeue with return ctrl.Result{}, err (the Result is ignored when err is non-nil), or ctrl.Result{RequeueAfter: d}, nil for a chosen delay.

Error handling — always requeue, never abandon

This is the non-negotiable core rule.

  • A resource is never dropped into a permanently-failed state. Every failure must requeue the object so it recovers on its own once the cause is fixed (an admin grants RBAC, a CRD gets installed, a dependency comes back, the user corrects the spec).
  • Never take the framework's give-up path — kopf's PermanentError, for one: it stops the retries, which violates this rule.
  • Every failure must do three things, uniformly:

    1. Log at error level with the underlying cause as a structured field — the API reason / response message, not just an HTTP status code, and never the raw stack trace as the only signal.
    2. Emit a Kubernetes Warning event on the object so it shows up in kubectl describe. Post it explicitly (e.g. a ReconcileFailed Warning from a reconcile wrapper) — do not assume the framework turns a raised error into an event; kopf, for one, does not by default. The kopf-conventions skill has the posting mechanics.
    3. Requeue with a sensible delay.

    A periodic status rollup — a kopf timer, a RequeueAfter health check — reports a failure it keeps seeing through a status condition, not an event on every tick: the same Warning repeated each interval buries the others.

  • Tune the requeue delay by error class, but always requeue:
    • Transient (5xx, 429, network/timeout): retry quickly (e.g. 30s).
    • Client / input errors (4xx such as 422 invalid manifest, 403 RBAC; or a failed spec validation): retry slowly (e.g. 300s) to avoid a hot loop, since they need an external change — but still retry.
    • This is the deliberate opposite of an HTTP client, which stops on a 422 (rest-conventions): a reconciler's desired state persists in the object, and the fix lands outside it, so stopping would leave it broken after the fix.
  • The requeue logic converges on one chokepoint: when the third handler repeats the extract-cause, log, requeue sequence, extract it into one module with a single requeue entry point and per-client cause extractors, and route every reconcile-path failure through it instead of scattering bare status checks and re-raises. Not before the third copy.
  • Validate the spec first and requeue on invalid input the same way — a malformed spec is a slow-retry client error, not a silent no-op and not a crash.

Anti-patterns to fix on sight: raise kopf.PermanentError; re-raising a raw client exception (the Kubernetes client's ApiException, an HTTP client's status error) out of a handler; logging only status without the reason; swallowing an error and returning so the resource silently stops reconciling.

Idempotency

  • Reconcile handlers run repeatedly; every side effect must be safe to repeat.
  • Treat already-exists (409) on create and not-found (404) on delete as success, not failure — log and continue.
  • Prefer create-or-skip / select-then-insert over blind inserts so a retry after a partial run never duplicates or errors.

Ownership, finalizers, garbage collection

  • Set an ownerReference on every in-cluster child object so Kubernetes garbage-collects it when the parent is deleted — don't delete children by hand in the delete handler.
  • Use a finalizer only for teardown Kubernetes can't do itself: external, non-Kubernetes resources (SaaS workspaces, DB rows, cloud objects). Make that teardown idempotent too (a 404/already-gone is success).

CRD manifests — one file per kind, kebab-case filename

When generating CRD YAML (e.g. a crd CLI subcommand driven by the typed spec models), emit one file per kind into an output directory, not a single --kind selector or one concatenated file. The generator owns the filename↔manifest mapping in a single place (e.g. an all_crds() registry next to the builders) so the CLI stays a thin caller and the pre-commit hook is one entry, not one-per-kind.

Name each file after the kind in kebab-case, acronym-aware — never glue the words together:

  • Widget → widget.yaml
  • WidgetAPIServer → widget-api-server.yaml (not widgetapiserver.yaml)

Kebab conversion needs two passes: split lower/digit→upper boundaries ((?<=[a-z0-9])(?=[A-Z])), then acronym→word boundaries ((?<=[A-Z])(?=[A-Z][a-z]), the I→Server split in APIServer), then lowercase. A naive .lower() of the PascalCase kind glues acronyms and is wrong.

Default the command with no output dir to a multi-document YAML stream on stdout (all kinds, ----separated). A single pre-commit hook regenerates the whole crds/ directory and the files: regex covers the spec modules plus crds/.*\.yaml, so the committed CRDs can never drift from the models.

Optional spec fields — nullable, never type: "null"

A Kubernetes structural schema rejects both type: "null" and an anyOf node that lacks its own type. Schema generators commonly emit an optional field as anyOf: [{type: T}, {type: "null"}] (Pydantic does for T | None), which the apiserver refuses on kubectl apply of the CRD. When converting a spec model's JSON Schema to the CRD's OpenAPI v3 subset, collapse <type> | null unions into the single branch with nullable: true (carrying over sibling keywords like description). The same converter must strip additionalProperties: false wherever it sits next to properties (a model that forbids extra fields leaks it, and structural schemas forbid the pair). Add a test that walks the generated CRD asserting no node has type: "null" nor additionalProperties: false beside properties.

Field manager — always identify yourself on writes

Every write to the API (server-side apply, JSON/merge/strategic-merge patch, create, update) must set an explicit, stable field_manager equal to the operator's name (e.g. widget-operator). Never let the client default it: the apiserver will then attribute the fields to a generic or empty manager, which is invisible to downstream tooling.

This is non-negotiable because anything that reads .metadata.managedFields relies on the manager name being recognisable:

  • GitOps drift suppression. ArgoCD's managedFieldsManagers (and Flux's equivalent) tells the GitOps engine "this manager owns these fields, don't flag them as drift". The lookup is by exact manager name. If the operator's writes don't carry a recognisable manager, the GitOps engine cannot defer ownership and the resource stays permanently OutOfSync.
  • SSA conflict resolution. Without a stable manager, two controllers fighting over the same field can't be diagnosed (kubectl get ... -o yaml shows nameless entries) and force_conflicts decisions become guesswork.
  • Auditability. kubectl get ... --show-managed-fields becomes the canonical answer to "who last wrote this field?". A blank or generic manager defeats the audit.

Apply this uniformly: the SSA call, every patch helper (json-patch+json, merge-patch+json, strategic-merge-patch+json), and any direct create/update. Centralise the constant (FIELD_MANAGER = "<operator>") in one module so every I/O call site uses the same string. A patch helper that silently omits field_manager is a bug to fix on sight — even if the write succeeds, it breaks the contract with GitOps and audit tooling downstream.

The manager appears in managedFields only after a successful write to the resource — creation by another controller is not enough. When debugging "my manager doesn't show up", it takes one reconcile that actually writes before re-checking: on a live cluster, ask the user to trigger it, or for the go-ahead to.

Observability

  • Logs follow logging-conventions.
  • Surface user-facing state through events and status conditions, not just logs — the spec author sees kubectl describe, not the operator's pod logs.

Documentation

  • Document failure/retry semantics in the project README.md (a Troubleshooting section): where to look (events + logs), and that every failure requeues — transient errors quickly, client errors slowly. Point at the module that holds the delays rather than copying their values, which would drift.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.