feat: Add extension points for resource generation#1118
Conversation
There was a problem hiding this comment.
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, andIWebhookConfigurationFactoryabstractions 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
IWebhookAttributeto support custom URI attributes with a backward-compatibleRouteAttribute.Templatefallback.
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.
|
I think this is now ready for a squash-merge. |
|
@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 😄 |
|
Added some docs. |
|
@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 As it stands, the current placement feels a bit misleading to me. Thanks 😄 |
|
@kimpenhaus this is all runtime behavior, so adding it to the build customization document would be even more misleading.
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). |
|
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? |
|
Fine by me, I'll update the docs some time Today, I'll also have to deal with merge conflicts. |
|
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
37c24ff to
64840b1
Compare
|
@kimpenhaus I updated the docs and squashed everything |
|
@BalassaMarton thanks for the contribution and especially for your patience |
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 callingAddKubernetesOperator()/AddDevelopmentTunnel()/UseCertificateProvider()to override the defaults.New abstractions (
KubeOps.Abstractions)ICrdResourceFactoryV1CustomResourceDefinitionresources are created from entity typesIEventResourceFactoryCorev1Eventinstances are constructed when publishing eventsIWebhookConfigurationFactoryV1MutatingWebhookConfigurationandV1ValidatingWebhookConfigurationare builtSupporting types:
MutatingWebhookRegistration,ValidatingWebhookRegistration(records carrying entity metadata, URI, and CA bundle).Default implementations
KubeOpsCrdResourceFactory— Uses the transpiler with a single sharedMetadataLoadContextacross all entity types. ExposesCreateCrdForEntityType()as a virtual override point.KubeOpsEventResourceFactory— BuildsCorev1Eventwith a SHA-512 hashed name for Kubernetes naming compliance.KubeOpsWebhookConfigurationFactory— Constructs webhook configs with virtualCreateMutatingWebhook()/CreateValidatingWebhook()methods for per-webhook customization.Other changes
IWebhookAttributeinterface added toKubeOps.Operator.Web— implemented byMutationWebhookAttribute,ValidationWebhookAttribute, andConversionWebhookAttribute.WebhookServiceBase.GetWebHookUri()falls back toRouteAttribute.Templatefor backward compatibility with custom attributes.CrdInstallerrefactored to acceptICrdResourceFactoryvia DI and includes retry logic with exponential backoff for transient API server errors.KubeOpsEventPublisherFactorysimplified — event construction delegated toIEventResourceFactory; the publisher only handles get-or-create, count increment, and save.originalNameannotation logic moved from the publisher intoKubeOpsEventResourceFactory.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.