A. B. Khan

Building a Kubernetes operator in C# — Kubernetes has no opinion about Go

The Mesh Series · Article 44 · Part H

Kubernetes operators.NET & C#KubernetesPolicy as codeGitOpsSupply-chain signingLGTM stack

Cover: Building a Kubernetes operator in C#

The platform team reviews the Article 43 design and says, kindly, that operators are written in Go. Kubebuilder, controller‑runtime, the Operator SDK, the language the API server itself is written in. They are not wrong about the ecosystem. They are wrong about what the question was.

The ComplianceProfile controller is the component that computes and signs the compliance verdict for your regulated estate. Under DORA that puts it inside the ICT risk framework — Articles 5 to 16 apply to the systems that manage risk, not only to the systems that carry payments. NIS2 Article 21(2)(e) wants secure development and maintenance for it. So the question an auditor asks about this component is not "what is it written in." It is who can read it, who reviewed the last change, and is the binary running in the cluster the one in the repo.

For a .NET shop, the answer that satisfies all three is C#.

One idea: an operator is a loop, not a language

Strip the ecosystem away and an operator is four verbs. Watch a resource. Compare desired and actual. Act on the difference. Requeue to do it again. Kubernetes exposes all four through a REST API and a watch stream that any language can consume. Go got there first and built the most tooling; nothing in the API server knows or cares which language is on the other end of the connection.

Diagram: controller lifecycle

Article 43 swapped the noun from replicas to controls. This article swaps nothing. It takes the same loop and writes it in the language the team that owns the controls already reviews, tests, scans and signs every day. KubeOps — the .NET Foundation's dotnet-operator-sdk, v13 on .NET 10, kubebuilder‑inspired down to the attribute names — makes that a few hundred lines rather than a port of controller‑runtime.

The contrarian core, stated fairly: Go still wins when the platform team is a Go shop, or when the operator is shared infrastructure that will outlive any one application team's stack. Pick the language of the people who will be paged for it. For the component that narrates your controls to your auditor, those people are your .NET team.

Why it matters: the compliance brain is in audit scope

Three reasons the language decision is a governance decision, not a taste one.

Same pipeline, same reviewers. The ComplianceProfile controller computes a verdict that will be shown to a regulator. If it lives in a Go repository nobody on the controls team can read, every change to it is reviewed by someone who does not own the controls, scanned by a SAST tool configured for a language the security team did not tune, and signed by a pipeline the platform team runs. Put it in C# and it goes through the same CI, the same SAST, the same cosign step and the same four‑eyes review as the payment services it reports on — reviewed by the engineers who know what Satisfied is supposed to mean.

The CRD and the RBAC are generated from the type. KubeOps builds the CustomResourceDefinition from the C# entity class and the Role from an attribute on the controller. One source of truth for the API shape, the permissions and the reconciler, in one language, in one review. An assessor who asks "what can this component write" is pointed at a single attribute: [EntityRbac(typeof(V1ComplianceProfile), Verbs = RbacVerb.All)] on its own resource, and read‑only verbs on everything it watches. The generated manifests ship through GitOps (Article 31), so the merged PR is the change record for the compliance brain itself.

Correct blast radius by construction. The operator has read access to PeerAuthentication, Gatekeeper constraints, ServiceEntry and a Loki query endpoint, and write access to its own status subresource and nothing else. Compromise it and an attacker can corrupt the narration of your controls, not the controls. Article 43 chose that design; the generated RBAC makes it enforceable, and the signed status (Article 27) makes any corruption detectable.

How it comes together: entity, controller, registration

The entity is the CRD. A class deriving from CustomKubernetesEntity<TSpec, TStatus> with a [KubernetesEntity] attribute naming the group, version, kind and plural. The Spec carries exactly the Article 43 shape — Satisfies, Selector, Retention, Controls — and the Status carries the conditions and the signature. KubeOps generates the structural schema, the status subresource and the printer columns from this type. There is no hand‑written YAML for the CRD, so there is nothing for the YAML and the code to disagree about.

The controller is the loop. IEntityController<V1ComplianceProfile> has two methods: ReconcileAsync and DeletedAsync. Both return a plain Task. Requeue is not a return value — it is an injected EntityRequeue<V1ComplianceProfile> delegate you call with the entity and a TimeSpan. Status is written through IKubernetesClient.UpdateStatusAsync, which patches only the subresource. The probes — one per source kind — are ordinary DI services that read Gatekeeper status, query Loki, or verify a cosign bundle; they are the "compose, don't rebuild" rule from Article 43 made into interfaces.

Registration is three lines. AddKubernetesOperator() on the service collection, AddController<TController, TEntity>() for the loop, and the probes and signer registered as normal services. The host is a standard .NET generic host: health checks, OpenTelemetry (Article 24's tracing applies to the operator too), structured logging, the same Program.cs idioms as every service the team already runs.

Generate, then ship through Git. The KubeOps CLI emits the CRD, the Role, the RoleBinding and the Deployment from the compiled assembly. Those manifests are committed, reviewed and applied by Argo CD. A change to a field on the entity is a diff on the CRD in the same PR as the code that uses it.

Show the code: three files that compile

The entity. The attributes are the CRD.

Code: entity

The controller. Read the RBAC attribute first; it is the sentence an auditor wants.

Code: controller

The host. Everything else is the .NET you already run.

Code: program

Security · Audit · Integration

Security: read‑only verbs on every primitive, RbacVerb.All on its own resource only, generated from attributes rather than hand‑written; the status is signed, so a tampered verdict fails verification even if the operator is compromised.

Audit: the CRD, the Role and the reconciler come from one C# type and one attribute, reviewed by the controls team in the same PR, scanned by the same SAST, signed by the same cosign step as the services it reports on — DORA Articles 5–16 and NIS2 21(2)(e) satisfied for the component that computes the verdict, not just for the workloads.

Integration: probes compose Gatekeeper status, Loki and cosign bundles (Articles 21/24/26/27); the generated manifests ship through GitOps (Article 31); the host carries the same OpenTelemetry tracing as the application tier.

Alternatives, and when you'd pick them

Go with kubebuilder or the Operator SDK. The default, and the right one when the platform team is a Go shop, or when the operator is shared infrastructure — a cluster‑wide controller that outlives any one application team's stack choice. The ecosystem is deeper: envtest, controller‑runtime's caching, a decade of examples. Pick it on the basis of who will be paged, not on the basis of what Kubernetes is written in.

Metacontroller, or a plain webhook reconciler. Metacontroller runs the watch loop and calls your HTTP endpoint with the observed state; you return desired state in any language. Right for a small, stateless transform where you want no SDK dependency at all. Wrong for the ComplianceProfile, which needs to read several primitives and sign a result — too much for a hook.

Do not build an operator; read Kyverno or Gatekeeper status directly. Still the closest alternative, as Article 43 said. If the primitive‑shaped verdict is enough, do not write a controller to re‑wrap it. The operator earns its place only for the framework‑shaped composition — and, per the project's own guidance, not until a readiness review has a client asking for exactly this. The code above exists so the design is credible, not because it has shipped.

From simple to standard, and what's next

The simple version is a CronJob in whatever language, writing a Slack message. The standard version is a C# entity that is the CRD, a controller whose RBAC is an attribute, probes that read rather than evaluate, a signed status written only to the subresource, a Program.cs indistinguishable from the rest of the estate, and manifests generated from the assembly and shipped through Git. Nothing exotic; KubeOps has been a .NET Foundation project for years. What changes is that the component an auditor will ask about hardest is reviewed by the people who can answer.

A status in a cluster is a verdict. It is not yet something an auditor can take away. Next, Article 45: shipping continuous evidence as a product — the WORM store, the signed‑pack API, the auditor's portal, and the retained tier that follows from never letting the chain lapse.

An auditor doesn't ask what language your operator is written in. They ask who can read it.


Next in the series: Art. 45, shipping continuous evidence as a product (Part H closer). Earlier: Art. 43 (the ComplianceProfile design this makes run), Art. 31 (CRD and RBAC through GitOps), Art. 27 (the signed verdict), Art. 24/26 (the access‑log probe), Art. 21 (Gatekeeper status composed).

Key skills & topics in this article

Kubernetes operators.NET & C#KubernetesPolicy as codeGitOpsSupply-chain signingLGTM stack

  • #dotnet
  • #CSharp
  • #Kubernetes
  • #Operator
  • #KubeOps
  • #DORA
  • #NIS2
  • #Compliance
  • #ServiceMesh
  • #RegTech

Also on LinkedIn.