Skip to content

feat: Add extension points for resource generation#1118

Merged
kimpenhaus merged 1 commit into
dotnet:mainfrom
BalassaMarton:mbalassa-resource-factory-extensions
May 21, 2026
Merged

feat: Add extension points for resource generation#1118
kimpenhaus merged 1 commit into
dotnet:mainfrom
BalassaMarton:mbalassa-resource-factory-extensions

Conversation

@BalassaMarton
Copy link
Copy Markdown
Contributor

Closes #1114

Summary

Introduces factory interfaces that let operators customize how CRDs, events, and webhook configurations are generated. All factories are registered with TryAdd, so users can register their own implementations before calling AddKubernetesOperator() / AddDevelopmentTunnel() / UseCertificateProvider() to override the defaults.

New abstractions (KubeOps.Abstractions)

Interface Purpose
ICrdResourceFactory Controls how V1CustomResourceDefinition resources are created from entity types
IEventResourceFactory Controls how Corev1Event instances are constructed when publishing events
IWebhookConfigurationFactory Controls how V1MutatingWebhookConfiguration and V1ValidatingWebhookConfiguration are built

Supporting types: MutatingWebhookRegistration, ValidatingWebhookRegistration (records carrying entity metadata, URI, and CA bundle).

Default implementations

  • KubeOpsCrdResourceFactory — Uses the transpiler with a single shared MetadataLoadContext across all entity types. Exposes CreateCrdForEntityType() as a virtual override point.
  • KubeOpsEventResourceFactory — Builds Corev1Event with a SHA-512 hashed name for Kubernetes naming compliance.
  • KubeOpsWebhookConfigurationFactory — Constructs webhook configs with virtual CreateMutatingWebhook() / CreateValidatingWebhook() methods for per-webhook customization.

Other changes

  • IWebhookAttribute interface added to KubeOps.Operator.Web — implemented by MutationWebhookAttribute, ValidationWebhookAttribute, and ConversionWebhookAttribute. WebhookServiceBase.GetWebHookUri() falls back to RouteAttribute.Template for backward compatibility with custom attributes.
  • CrdInstaller refactored to accept ICrdResourceFactory via DI and includes retry logic with exponential backoff for transient API server errors.
  • KubeOpsEventPublisherFactory simplified — event construction delegated to IEventResourceFactory; the publisher only handles get-or-create, count increment, and save.
  • Event name hashing and originalName annotation logic moved from the publisher into KubeOpsEventResourceFactory.

Breaking changes

None. All modified constructors are internal. New interfaces and types are purely additive.

Tests

  • CrdInstallerTest — verifies custom factory DI override, transient error retry, non-transient error handling, and stop cancellation.
  • KubeOpsCrdResourceFactoryTest — verifies default CRD transpilation.
  • KubeOpsEventResourceFactoryTest — verifies event properties and namespace defaulting.
  • EventPublisherCustomResourceFactoryTest — end-to-end test verifying custom factory output flows through the publisher to the Kubernetes client.

Copilot AI review requested due to automatic review settings May 12, 2026 21:53
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds DI-registered factory interfaces to let consumers customize how key Kubernetes resources are generated (CRDs, Events, and webhook configurations), while keeping existing operator workflows (install/publish/register) intact.

Changes:

  • Introduces ICrdResourceFactory, IEventResourceFactory, and IWebhookConfigurationFactory abstractions plus registration DTOs for webhook generation.
  • Refactors CRD installation and event publishing to delegate resource construction to the new factories (registered via TryAdd* so user overrides win).
  • Refactors webhook registration to delegate webhook configuration construction to a factory and adds IWebhookAttribute to support custom URI attributes with a backward-compatible RouteAttribute.Template fallback.

Reviewed changes

Copilot reviewed 25 out of 25 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
test/KubeOps.Operator.Web.Test/Builder/OperatorBuilderExtensions.Test.cs Minor dispose cleanup in web builder tests.
test/KubeOps.Operator.Test/Events/KubeOpsEventResourceFactory.Test.cs Adds unit tests for default event resource factory behavior.
test/KubeOps.Operator.Test/Events/EventPublisherCustomResourceFactory.Test.cs Adds integration-style test ensuring custom event factory is honored via DI.
test/KubeOps.Operator.Test/Crds/KubeOpsCrdResourceFactory.Test.cs Adds unit test for default CRD factory output.
test/KubeOps.Operator.Test/Crds/CrdInstaller.Test.cs Updates installer tests for factory injection, retries, and DI override behavior.
src/KubeOps.Operator/Events/KubeOpsEventResourceFactory.cs Adds default IEventResourceFactory implementation with hashed naming.
src/KubeOps.Operator/Events/KubeOpsEventPublisherFactory.cs Refactors publisher to use IEventResourceFactory for event construction.
src/KubeOps.Operator/Crds/KubeOpsCrdResourceFactory.cs Adds default ICrdResourceFactory using the transpiler with shared MetadataLoadContext.
src/KubeOps.Operator/Crds/CrdInstaller.cs Refactors installer to use ICrdResourceFactory and adds transient-error retry loop.
src/KubeOps.Operator/Builder/OperatorBuilder.cs Registers default CRD/event factories via TryAddSingleton.
src/KubeOps.Operator.Web/Webhooks/WebhookServiceBase.cs Refactors mutator/validator config generation into IWebhookConfigurationFactory; adds URI resolution fallback.
src/KubeOps.Operator.Web/Webhooks/KubeOpsWebhookConfigurationFactory.cs Adds default webhook configuration factory with per-webhook override points.
src/KubeOps.Operator.Web/Webhooks/IWebhookAttribute.cs Adds attribute interface for defining webhook URIs.
src/KubeOps.Operator.Web/Webhooks/Conversion/ConversionWebhookAttribute.cs Implements IWebhookAttribute for conversion webhooks.
src/KubeOps.Operator.Web/Webhooks/Admission/Validation/ValidationWebhookAttribute.cs Implements IWebhookAttribute for validation webhooks.
src/KubeOps.Operator.Web/Webhooks/Admission/Mutation/MutationWebhookAttribute.cs Implements IWebhookAttribute for mutation webhooks.
src/KubeOps.Operator.Web/LocalTunnel/TunnelWebhookService.cs Wires webhook configuration factory into tunnel webhook service.
src/KubeOps.Operator.Web/Certificates/CertificateWebhookService.cs Wires webhook configuration factory into certificate webhook service.
src/KubeOps.Operator.Web/Builder/OperatorBuilderExtensions.cs Registers default webhook configuration factory via TryAddSingleton.
src/KubeOps.Cli/Generators/IConfigGenerator.cs Removes unused Spectre.Console using.
src/KubeOps.Abstractions/Webhooks/ValidatingWebhookRegistration.cs Adds webhook registration record (validating).
src/KubeOps.Abstractions/Webhooks/MutatingWebhookRegistration.cs Adds webhook registration record (mutating).
src/KubeOps.Abstractions/Webhooks/IWebhookConfigurationFactory.cs Adds webhook configuration factory interface.
src/KubeOps.Abstractions/Events/IEventResourceFactory.cs Adds event resource factory interface and contract documentation.
src/KubeOps.Abstractions/Crds/ICrdResourceFactory.cs Adds CRD resource factory interface.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/KubeOps.Operator/Events/KubeOpsEventResourceFactory.cs
Comment thread src/KubeOps.Operator/Events/KubeOpsEventResourceFactory.cs
Comment thread src/KubeOps.Operator/Events/KubeOpsEventPublisherFactory.cs
Comment thread src/KubeOps.Operator/Events/KubeOpsEventPublisherFactory.cs
Comment thread src/KubeOps.Operator/Events/KubeOpsEventPublisherFactory.cs Outdated
Comment thread test/KubeOps.Operator.Test/Crds/CrdInstaller.Test.cs
@BalassaMarton
Copy link
Copy Markdown
Contributor Author

I think this is now ready for a squash-merge.

@kimpenhaus
Copy link
Copy Markdown
Collaborator

kimpenhaus commented May 15, 2026

@BalassaMarton thanks for the pull request - I still need some time to think about this - sorry if you are waiting. In the meantime having a quick glance at the files in the commit I just saw that the entire docs part is missing. Could you add some developer guidance to the new feature - thanks 😄

@BalassaMarton
Copy link
Copy Markdown
Contributor Author

Added some docs.

@kimpenhaus
Copy link
Copy Markdown
Collaborator

kimpenhaus commented May 20, 2026

@BalassaMarton thanks for adding the docs, but I think they could be placed more precisely.

Since this pull request is primarily relevant to development scenarios, the documentation would fit best in the build-customization.mdx page, most likely under the using-with-the-crd-installer-for-local-development section.

As it stands, the current placement feels a bit misleading to me.

Thanks 😄

@BalassaMarton
Copy link
Copy Markdown
Contributor Author

@kimpenhaus this is all runtime behavior, so adding it to the build customization document would be even more misleading.
I specifically added this note to the docs for entities:

This section explains how you can customize CRD generation when using the built-in (runtime) CrdInstaller.
CRD generation using the CLI is not affected.

There are three new APIs in this PR, one of them is for events, which is runtime behavior even in production, and two (CRD generation and webhook registration) are mostly for development scenarios, although they can be used in production as well (that was my intention).

@kimpenhaus
Copy link
Copy Markdown
Collaborator

We don't use customizations by ourselves - so I am not that deep into this subject (you might have already guessed).

I just double checked with the docs and I guess best place to mention runtime customzations (as it is not the default behavior) should go to the advanced configuration page.

I think its fine to cross-link from the other pages but the more detailed part should go there to keep the other pages focused on not to overload on special subjects.

that's all of course my personal perspective - what do you think?

@BalassaMarton
Copy link
Copy Markdown
Contributor Author

Fine by me, I'll update the docs some time Today, I'll also have to deal with merge conflicts.

@kimpenhaus
Copy link
Copy Markdown
Collaborator

I guess those merge conflicts are from the sonar warnings - that shoukld use the correct logger types: https://github.com/dotnet/dotnet-operator-sdk/pull/1124/changes

sorry for that

closes [dotnet#1114](dotnet#1114)

Fix inconsistent naming of GetWebhookUri

Fix remarks for IEventResourceFactory

Fix possible missing namespace on event created by the factory

Fix unnecessary round-trip in EventPublisher

Fix uppercase event naming

Add missing annotations to events

Fix race condition in test

Fix formatting

Fix copilot recommendations

Fix event name generation in tests

Add documentation for the resource factories

Revert "Add documentation for the resource factories"

This reverts commit c1e413983b952c02ccc28a72e37637755129b068.

docs: Add documentation for the resource factories
@BalassaMarton BalassaMarton force-pushed the mbalassa-resource-factory-extensions branch from 37c24ff to 64840b1 Compare May 21, 2026 12:17
@BalassaMarton
Copy link
Copy Markdown
Contributor Author

@kimpenhaus I updated the docs and squashed everything

@kimpenhaus
Copy link
Copy Markdown
Collaborator

@BalassaMarton thanks for the contribution and especially for your patience

@kimpenhaus kimpenhaus merged commit 199e8ce into dotnet:main May 21, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature]: Add extension points for customizing generated resources

3 participants