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
48 changes: 47 additions & 1 deletion docs/docs/operator/building-blocks/webhooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,52 @@ For local development, your operator must be accessible from the Kubernetes clus
The LocalTunnel option is deprecated due to stability issues. It's recommended to use the certificate provider or other more reliable solutions.
:::

## Endpoint Routing

KubeOps automatically registers an HTTP route for each webhook. The route is determined by the
attribute applied to the webhook class — specifically by the **entity type** passed to that attribute.

| Webhook Type | Attribute | Route Pattern |
|---|---|---|
| Validation | `[ValidationWebhook(typeof(TEntity))]` | `/validate/{entity-type-name}` |
| Mutation | `[MutationWebhook(typeof(TEntity))]` | `/mutate/{entity-type-name}` |
| Conversion | `[ConversionWebhook(typeof(TEntity))]` | `/convert/{group}/{plural-name}` |

### How the route segments are derived

For **validation** and **mutation** webhooks, `{entity-type-name}` is the C# type name of
the entity converted to lowercase. It is computed as:

```
entityType.Name.ToLowerInvariant()
```

This means the route segment reflects the **entity type**, not the webhook class name.

For example, a webhook declared as:

```csharp
[ValidationWebhook(typeof(V1DemoEntity))]
public class DemoValidationWebhook : ValidationWebhook<V1DemoEntity> { /* ... */ }
```

registers the route `/validate/v1demoentity` — derived from `V1DemoEntity`, not from `DemoValidationWebhook`.

| Entity type | Validation route | Mutation route |
|---|---|---|
| `V1DemoEntity` | `/validate/v1demoentity` | `/mutate/v1demoentity` |
| `V1TestEntity` | `/validate/v1testentity` | `/mutate/v1testentity` |
| `MyCustomResource` | `/validate/mycustomresource` | `/mutate/mycustomresource` |

For **conversion** webhooks, the route uses the entity's Kubernetes metadata instead:

- `{group}` — the API group from the `[KubernetesEntity]` attribute (e.g., `webhook.dev`)
- `{plural-name}` — the plural resource name from the CRD metadata (e.g., `testentities`)

A conversion webhook for an entity annotated with
`[KubernetesEntity(Group = "webhook.dev", ApiVersion = "v2", Kind = "TestEntity")]`
registers the route `/convert/webhook.dev/testentities`.

## Validation Webhooks

Validation webhooks allow you to enforce custom validation rules for your resources. They are called before a resource is created or updated.
Expand Down Expand Up @@ -263,4 +309,4 @@ public class DemoConversionWebhook : ConversionWebhook<V3DemoEntity>
- Incomplete conversion paths
- Data loss during conversion
- Missing validation
- Incorrect storage version selection
- Incorrect storage version selection
11 changes: 6 additions & 5 deletions src/KubeOps.Operator.Web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ Key points:

KubeOps automatically configures routes for your webhooks based on the attributes you apply:

- `[ValidationWebhook(typeof(TEntity))]`: Creates an endpoint at `/validate/{webhook-name}`
- `[MutationWebhook(typeof(TEntity))]`: Creates an endpoint at `/mutate/{webhook-name}`
- `[ConversionWebhook(typeof(TStorageEntity))]`: Creates an endpoint at `/convert/{webhook-name}`
- `[ValidationWebhook(typeof(TEntity))]`: Creates an endpoint at `/validate/{entity-type-name}`
- `[MutationWebhook(typeof(TEntity))]`: Creates an endpoint at `/mutate/{entity-type-name}`
- `[ConversionWebhook(typeof(TStorageEntity))]`: Creates an endpoint at `/convert/{group}/{plural-name}`

The `{webhook-name}` is typically derived from the C# class name of your webhook implementation (e.g., `TestValidationWebhook` might map to `/validate/testvalidationwebhook`). These are the endpoints you configure in your `ValidatingWebhookConfiguration`, `MutatingWebhookConfiguration`, or `CustomResourceDefinition` Kubernetes manifests.
The `{entity-type-name}` is derived from the lowercase C# type name of the entity passed to the webhook attribute (e.g., `V1TestEntity` maps to `/validate/v1testentity`).
For conversion webhooks, the URL is constructed from the entity's group and plural name from its metadata (e.g., `/convert/webhook.dev/testentities`).

## Implementation Overview

Expand Down Expand Up @@ -288,4 +289,4 @@ builder.Services
// More code for generation, controllers, etc.
```

The `UseCertificateProvider` method takes an `ICertificateProvider` interface, so it can be used to implement your own certificate generator/loader for development if necessary.
The `UseCertificateProvider` method takes an `ICertificateProvider` interface, so it can be used to implement your own certificate generator/loader for development if necessary.
Loading