Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var k8s = builder.AddKubernetesEnvironment("k8s")
helm.WithNamespace("operator-system");
});

builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.RunWithKubernetes(k8s)
.PublishAsKubernetesOperator(k8s);

Expand All @@ -115,7 +115,7 @@ The SDK is designed to be modular. You can include only the packages you need:
|----------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [KubeOps.Abstractions](./src/KubeOps.Abstractions/README.md) | Defines core interfaces, attributes (like `[KubernetesEntity]`), and base classes used across the SDK. Essential for defining your custom resources and controllers. |
| [KubeOps.Aspire](./src/KubeOps.Aspire/README.md) | [.NET Aspire](https://learn.microsoft.com/dotnet/aspire/) service defaults for an operator: a single `AddKubeOpsServiceDefaults()` call wiring up OpenTelemetry, service discovery, HTTP resilience, and health checks. |
| [KubeOps.Aspire.Hosting](./src/KubeOps.Aspire.Hosting/README.md) | [.NET Aspire](https://learn.microsoft.com/dotnet/aspire/) hosting integration. Adds `AddKubeOps<TProject>(...)` so a KubeOps operator can be orchestrated as a resource inside an Aspire AppHost. |
| [KubeOps.Aspire.Hosting](./src/KubeOps.Aspire.Hosting/README.md) | [.NET Aspire](https://learn.microsoft.com/dotnet/aspire/) hosting integration. Adds `AddKubeOps(...)` so a KubeOps operator can be orchestrated as a resource inside an Aspire AppHost. |
| [KubeOps.Cli](./src/KubeOps.Cli/README.md) | A [.NET Tool](https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools) providing commands for scaffolding projects, generating [Custom Resource Definitions (CRDs)](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/), and more. |
| [KubeOps.Generator](./src/KubeOps.Generator/README.md) | Contains [Roslyn Source Generators](https://docs.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview) to automate boilerplate code generation for CRDs and controllers based on your definitions. |
| [KubeOps.KubernetesClient](./src/KubeOps.KubernetesClient/README.md) | Provides an enhanced client for interacting with the [Kubernetes API](https://kubernetes.io/docs/reference/kubernetes-api/), built on top of the official `KubernetesClient` library. Offers convenience methods for common operator tasks. |
Expand Down
54 changes: 35 additions & 19 deletions docs/docs/operator/aspire/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description: Orchestrate and observe a KubeOps operator with .NET Aspire

| Package | Side | Purpose |
| --- | --- | --- |
| [`KubeOps.Aspire.Hosting`](../../packages/aspire-hosting) | AppHost | Adds `AddKubeOps<TProject>(...)` so the operator is orchestrated as a resource. |
| [`KubeOps.Aspire.Hosting`](../../packages/aspire-hosting) | AppHost | Adds `AddKubeOps(...)` so the operator is orchestrated as a resource. |
| [`KubeOps.Aspire`](../../packages/aspire) | Operator | Adds `AddKubeOpsServiceDefaults()` for OpenTelemetry, service discovery, resilience and health checks. |

Together they give you a local dashboard with the operator's logs, traces and metrics, automatic service discovery to other resources, and a single entry point to run, publish, or deploy the whole stack.
Expand Down Expand Up @@ -70,38 +70,53 @@ The repository sample uses `examples/AspireOperator` for this so `examples/Opera
```csharp
var builder = DistributedApplication.CreateBuilder(args);

builder.AddKubernetesEnvironment("k8s")
var k8s = builder.AddKubernetesEnvironment("k8s")
.WithHelm(helm =>
{
helm.WithChartName("my-operator");
helm.WithReleaseName("my-operator");
helm.WithNamespace("operator-system");
});

var apiService = builder.AddProject<Projects.ApiService>("apiservice");
var apiService = builder.AddDotnetProject("apiservice", "../ApiService");

builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.RunWithKubernetes(k8s)
.PublishAsKubernetesOperator(k8s)
.WithReference(apiService);

builder.Build().Run();
```

`AddKubeOps<TProject>` is a thin, KubeOps-flavoured wrapper around the built-in `AddProject<TProject>`. It returns the standard `IResourceBuilder<ProjectResource>`, so every Aspire extension works as usual:
`AddKubeOps` is a thin, KubeOps-flavoured wrapper around Aspire's `AddDotnetProject`. It takes the path of the operator project (the `.csproj` file or the directory that contains it, relative to the AppHost directory) and returns the standard `IResourceBuilder<DotnetProjectResource>`, so every Aspire extension works as usual. Other project types that `AddDotnetProject` accepts (F#, Visual Basic, file-based apps) are rejected with an `ArgumentException`, because the KubeOps CLI generates CRDs and manifests for C# projects only.

The returned builder supports, among others:

- `WithReference(apiService)` injects the service-discovery configuration so the operator can call `apiservice` by name.
- `WithEnvironment(...)`, `WaitFor(...)`, `WithReplicas(...)` and friends all apply unchanged.
- `RunWithKubernetes(k8s)` opts the operator into local execution against a Kubernetes target.
- `PublishAsKubernetesOperator(k8s)` includes the operator, CRDs, RBAC, and service account in the Aspire Kubernetes publish/deploy output.

:::note[Project reference]
When referencing `KubeOps.Aspire.Hosting` from an AppHost, mark it as a normal code reference so the AppHost SDK does not treat it as a resource:
:::note[AppHost project file]
`KubeOps.Aspire.Hosting` requires Aspire 17 or later. The AppHost is a plain `Microsoft.NET.Sdk` project that references `Aspire.Hosting.AppHost` and `KubeOps.Aspire.Hosting` (`dotnet add package KubeOps.Aspire.Hosting`); the obsolete `Aspire.AppHost.Sdk` and project references to the operator are not needed, because `AddKubeOps` locates the operator by its path:

```xml
<ProjectReference Include="..\..\src\KubeOps.Aspire.Hosting\KubeOps.Aspire.Hosting.csproj"
IsAspireProjectResource="false" />
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="17.0.0" />
Comment thread
kimpenhaus marked this conversation as resolved.
Resolved
<PackageReference Include="KubeOps.Aspire.Hosting" Version="15.0.0" />
</ItemGroup>
</Project>
```

When the AppHost references `KubeOps.Aspire.Hosting` as a project instead of a package, as the repository sample does, mark it as a normal code reference so Aspire does not treat it as a resource: `IsAspireProjectResource="false"`.

Building an Aspire 17 AppHost needs the Aspire CLI bundle. On the first build on a machine, `Aspire.Hosting.AppHost` downloads it for its version into `~/.aspire` via `dnx`; without network access that build fails with `ASPIRE009`. Only the AppHost needs the bundle, the operator project itself builds without it.
:::

## Running locally
Expand All @@ -117,7 +132,7 @@ A KubeOps operator is explicit-start by default. It does not run in local mode u
```csharp
var dev = builder.AddKubernetesEnvironment("dev");

builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.RunWithKubernetes(dev);
```

Expand All @@ -126,7 +141,7 @@ By default, `RunWithKubernetes(...)` creates missing CRDs before the local opera
Use the run options when the development cluster should keep CRDs after shutdown or when CRDs are managed separately:

```csharp
builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.RunWithKubernetes(dev, run =>
{
run.WithPersistentCrds(); // create or update CRDs and leave them in the cluster
Expand All @@ -140,7 +155,7 @@ The operator process runs locally and authenticates through the selected Kuberne
If the operator has webhooks and the cluster cannot reach your machine, add `WithWebhookDevTunnel()`. It starts a Microsoft Dev Tunnel for the operator's `http` endpoint and passes the public URL to the operator, which registers its webhooks with `UseExternalWebhookEndpoint()`:

```csharp
builder.AddKubeOps<Projects.WebhookOperator>("operator")
builder.AddKubeOps("operator", "../WebhookOperator")
.RunWithKubernetes(dev)
.WithWebhookDevTunnel();
```
Expand Down Expand Up @@ -171,7 +186,7 @@ The Aspire path for Kubernetes publishing is the official [`Aspire.Hosting.Kuber
aspire publish --project examples/AspireAppHost/AspireAppHost.csproj --output-path ./k8s-artifacts
```

When you call `PublishAsKubernetesOperator(k8s)`, Aspire writes a Helm chart for the resources in the AppHost. `AddKubeOps<TProject>` also contributes the Kubernetes API extension pieces that KubeOps generates:
When you call `PublishAsKubernetesOperator(k8s)`, Aspire writes a Helm chart for the resources in the AppHost. `AddKubeOps` also contributes the Kubernetes API extension pieces that KubeOps generates:

- CRDs are appended to the generated chart.
- RBAC resources are appended to the generated chart.
Expand All @@ -183,8 +198,9 @@ When you call `PublishAsKubernetesOperator(k8s)`, Aspire writes a Helm chart for
By default, the hosting integration invokes `kubeops generate operator`. If the CLI is not on `PATH`, configure the invocation:

```csharp
builder.AddKubeOps<Projects.AspireOperator>(
builder.AddKubeOps(
"operator",
"../AspireOperator",
manifests =>
{
manifests.Namespace = "operator-system";
Expand All @@ -197,7 +213,7 @@ builder.AddKubeOps<Projects.AspireOperator>(
If the AppHost does not define a Kubernetes environment, the operator can still publish KubeOps manifests:

```csharp
builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.PublishAsKubernetesOperator(publish =>
{
publish.Namespace = "operator-system";
Expand All @@ -214,7 +230,7 @@ Local development against a local cluster:
```csharp
var dev = builder.AddKubernetesEnvironment("dev");

builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.RunWithKubernetes(dev, run => run.WithPersistentCrds());
```

Expand All @@ -223,7 +239,7 @@ Local development against a k3s cluster from the [CommunityToolkit Aspire k3s in
```csharp
var k3s = builder.AddK3sCluster("k3s");

builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.RunWithKubernetes(k3s, run => run.WithPersistentCrds());
```

Expand All @@ -234,7 +250,7 @@ Azure deployment into AKS:
```csharp
var aks = builder.AddAzureKubernetesEnvironment("aks");

builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.PublishAsKubernetesOperator(aks, publish =>
{
publish.Namespace = "operator-system";
Expand All @@ -248,7 +264,7 @@ Local run plus Azure deployment:
var dev = builder.AddKubernetesEnvironment("dev");
var aks = builder.AddAzureKubernetesEnvironment("aks");

builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.RunWithKubernetes(dev)
.PublishAsKubernetesOperator(aks, publish => publish.WithServiceAccount("operator"));
```
Expand Down
22 changes: 11 additions & 11 deletions docs/docs/operator/aspire/kubernetes-operator-model.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var dev = builder.AddKubernetesEnvironment("dev")
var aks = builder.AddAzureKubernetesEnvironment("aks")
.WithHelm(helm => helm.WithNamespace("operator-system"));

builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.RunWithKubernetes(dev, run =>
{
run.WithPersistentCrds();
Expand All @@ -53,7 +53,7 @@ builder.Build().Run();

`PublishAsKubernetesOperator(...)` configures the Kubernetes deployment. The operator becomes a Kubernetes workload, and KubeOps-generated CRDs, RBAC, and service-account resources are incorporated into the generated Aspire chart.

If `RunWithKubernetes(...)` is not configured, `AddKubeOps<TProject>(...)` keeps the operator resource in Aspire's explicit-start mode during `aspire run`. The project remains in the AppHost model and publish graph, but the local operator process does not start by default. A KubeOps operator without a Kubernetes run target would otherwise fail against an accidental kube context or mutate the wrong cluster.
If `RunWithKubernetes(...)` is not configured, `AddKubeOps(...)` keeps the operator resource in Aspire's explicit-start mode during `aspire run`. The project remains in the AppHost model and publish graph, but the local operator process does not start by default. A KubeOps operator without a Kubernetes run target would otherwise fail against an accidental kube context or mutate the wrong cluster.

## Run Mode

Expand All @@ -62,7 +62,7 @@ Run mode defaults to a productive inner loop once you opt in:
```csharp
var dev = builder.AddKubernetesEnvironment("dev");

builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.RunWithKubernetes(dev);
```

Expand Down Expand Up @@ -100,7 +100,7 @@ var k8s = builder.AddKubernetesEnvironment("k8s")
helm.WithNamespace("operator-system");
});

builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.PublishAsKubernetesOperator(k8s);
```

Expand Down Expand Up @@ -141,7 +141,7 @@ The integration filters KubeOps' generated `Deployment` out of the additional re
You can also generate KubeOps manifests without an Aspire Kubernetes environment:

```csharp
builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.PublishAsKubernetesOperator(publish =>
{
publish.Namespace = "operator-system";
Expand Down Expand Up @@ -169,7 +169,7 @@ For local run mode, Azure integrations may provision Azure resources unless conf
var aks = builder.AddAzureKubernetesEnvironment("aks")
.RunAsExisting(aksName, aksResourceGroup);

builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.RunWithKubernetes(aks);
```

Expand All @@ -184,7 +184,7 @@ Run the operator as a local process against the selected Kubernetes environment.
```csharp
var dev = builder.AddKubernetesEnvironment("dev");

builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.RunWithKubernetes(dev, run => run.WithPersistentCrds());
```

Expand All @@ -195,7 +195,7 @@ Deploy the operator into AKS without running it as a local process during `aspir
```csharp
var aks = builder.AddAzureKubernetesEnvironment("aks");

builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.PublishAsKubernetesOperator(aks, publish =>
{
publish.Namespace = "operator-system";
Expand All @@ -211,7 +211,7 @@ Use a local Kubernetes environment for the development loop and AKS for publish/
var dev = builder.AddKubernetesEnvironment("dev");
var aks = builder.AddAzureKubernetesEnvironment("aks");

builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.RunWithKubernetes(dev)
.PublishAsKubernetesOperator(aks, publish => publish.WithServiceAccount("operator"));
```
Expand All @@ -221,7 +221,7 @@ builder.AddKubeOps<Projects.AspireOperator>("operator")
Generate standalone KubeOps manifests without registering an Aspire Kubernetes environment.

```csharp
builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.PublishAsKubernetesOperator(publish =>
{
publish.Namespace = "operator-system";
Expand All @@ -238,7 +238,7 @@ Cluster resources that are not `KubernetesEnvironmentResource`s but expose their
```csharp
var k3s = builder.AddK3sCluster("k3s");

builder.AddKubeOps<Projects.AspireOperator>("operator")
builder.AddKubeOps("operator", "../AspireOperator")
.RunWithKubernetes(k3s);
```

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/operator/building-blocks/webhooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ When the operator runs in an Aspire AppHost, `WithWebhookDevTunnel` from `KubeOp

```csharp
// AppHost
builder.AddKubeOps<Projects.WebhookOperator>("operator")
builder.AddKubeOps("operator", "../WebhookOperator")
.RunWithKubernetes(k8s)
.WithWebhookDevTunnel();

Expand Down
5 changes: 5 additions & 0 deletions docs/docs/operator/contribution/running-sdk-tests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ The SDK's test projects live under `test/`, one per package. Two kinds of tests
`test/KubeOps.Cli.Test.Operator` and validate the generated manifests with `kubectl kustomize` and a
server-side dry run.

Building the solution or running all tests also builds the Aspire AppHost in `examples/AspireAppHost`, which
`KubeOps.Aspire.Hosting.Test` references. Aspire 17 AppHosts need the Aspire CLI bundle: the first build on a machine
downloads it for the referenced Aspire version into `~/.aspire` via `dnx`, so that build needs network access and
fails with `ASPIRE009` without it. Later builds reuse the downloaded bundle.

:::caution
Integration tests create and delete resources (CRDs, namespaces, custom resources) in the cluster
of your **current kubeconfig context**. Only run them against a disposable development cluster.
Expand Down
Loading
Loading