Skip to main content

Local Development

Run the operator from your IDE against a development cluster: install the CRDs, start the operator, and make its webhooks reachable for the API server.

You need
A cluster in your kubectl context: kind, Docker Desktop, minikube or remote
CRDs
Installed with kubeops install, the CRD Installer or Aspire
Webhooks
Reachable over HTTPS: a certificate on your IP, a tunnel or Aspire
Never
Ship the development setup; keep only it in #if DEBUG, not the registration

Run the operator locally​

dotnet run or the debugger starts the operator on your machine. It connects to the cluster of your current kubectl context (KUBECONFIG or ~/.kube/config) with your credentials, so check it with kubectl config current-context first.

The CRDs must be in the cluster before the operator starts watching:

OptionUse it when
kubeops installYou install the CRDs once per change from the command line
CRD InstallerThe operator installs its CRDs on every start; development only
AspireAn AppHost starts the operator; it creates missing CRDs

An operator without webhooks needs nothing more.

Webhooks​

The API server calls webhooks over HTTPS, so it must reach the operator on your machine:

Your clusterUse
kind, Docker Desktop, minikube or in your networkCertificate provider
Cloud, or behind NAT or VPNTunnel
Started by an Aspire AppHostAspire

With each of them, the operator registers its webhooks at startup: the validation and mutation webhooks as the configurations dev-validators and dev-mutators, and each conversion webhook in its CRD, if the CRD exists.

Certificate provider​

CertificateGenerator creates a self-signed certificate for an IP address or host name of your machine.

Program.cs
var operatorBuilder = builder.Services
.AddKubernetesOperator()
.RegisterComponents();

#if DEBUG
const string ip = "192.168.1.100";
const ushort port = 443;
using var generator = new CertificateGenerator(ip);
var cert = generator.Server.CopyServerCertWithPrivateKey();

builder.WebHost.ConfigureKestrel(
o => o.Listen(IPAddress.Any, port, l => l.UseHttps(cert)));

operatorBuilder.UseCertificateProvider(port, ip, generator);
#endif
Clusters in containers

With kind, Docker Desktop or minikube, the API server runs in a container or VM, so localhost is not your machine. Use an address that is reachable from there, e.g. your LAN IP or the gateway of the container network.

Tunnel​

UseExternalWebhookEndpoint registers the webhooks with the public HTTPS URL of a tunnel. The tunnel terminates TLS, so the operator listens on plain HTTP.

Program.cs
var operatorBuilder = builder.Services
.AddKubernetesOperator()
.RegisterComponents();

#if DEBUG
operatorBuilder.UseExternalWebhookEndpoint(
new Uri("https://my-tunnel-5000.euw.devtunnels.ms"));
#endif
  • URL from configuration. Without arguments, UseExternalWebhookEndpoint() reads KubeOps:Webhooks:ExternalUrl (environment variable KubeOps__Webhooks__ExternalUrl) and fails the start if it is missing.
  • Own certificate. For an endpoint without a publicly trusted certificate, pass its CA: UseExternalWebhookEndpoint(uri, caBundle).
  • Path prefix. https://proxy.example.com/my-operator registers https://proxy.example.com/my-operator/validate/v1demoentity. The operator serves the routes at the root, so the proxy must strip the prefix.

The examples forward to an operator on http://localhost:5000. A persistent URL saves you from updating the configuration on every run.

# login once, create once, host every session
devtunnel user login
devtunnel create kubeops-dev --allow-anonymous
devtunnel port create kubeops-dev -p 5000 --protocol http
devtunnel host kubeops-dev
# → https://<tunnel-id>-5000.<region>.devtunnels.ms

Aspire​

WithWebhookDevTunnel from KubeOps.Aspire.Hosting starts a Microsoft Dev Tunnel for the operator's http endpoint, passes its URL as KubeOps__Webhooks__ExternalUrl and starts the operator once the tunnel runs.

AppHost.cs
builder.AddKubeOps("operator", "../WebhookOperator")
.RunWithKubernetes(dev)
.WithWebhookDevTunnel();

Install the devtunnel CLI; Aspire asks you to sign in when needed. The URL stays the same across runs. Pass another endpoint name or tunnel options, e.g. WithWebhookDevTunnel("webhooks", tunnel => tunnel.WithExpiration(24)). In publish mode, WithWebhookDevTunnel adds nothing. For the rest of the AppHost setup, see .NET Aspire.

Security and shared clusters​

Anonymous endpoint

The API server cannot authenticate against a tunnel, so the tunnel allows anonymous access. Anyone who knows the URL can call your webhooks while it runs. Your organization's policy may forbid this.

  • Cluster-wide. The webhooks intercept everyone's requests for the resource type, not only yours. On a shared cluster, use a dedicated CRD group or a NamespaceSelector via a custom webhook configuration factory.
  • Cloud clusters. The API server calls the tunnel from the control plane. If its outbound traffic goes through your network (e.g. API server VNet integration on AKS), allow the tunnel domain there.

Common pitfalls​

  • The operator works on the wrong cluster. It uses your current kubectl context; switch it before you start.
  • Writes are blocked after the operator stopped. dev-validators and dev-mutators stay registered after a crash and with UseCertificateProvider, and the API server rejects writes it cannot validate. UseExternalWebhookEndpoint and WithWebhookDevTunnel remove them when the operator stops. Delete them with kubectl delete validatingwebhookconfiguration dev-validators and kubectl delete mutatingwebhookconfiguration dev-mutators.
  • Other versions fail after the operator stopped. The conversion webhook stays in the CRD, so requests for a non-stored version fail. Start the operator again, or set spec.conversion.strategy to None in the CRD.

Next steps​