-
Notifications
You must be signed in to change notification settings - Fork 90
fix(operator-web): compose conversions through the storage version #1194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kimpenhaus
merged 3 commits into
dotnet:main
from
alethic:fix/conversion-webhook-multi-hop
Jul 12, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
246 changes: 246 additions & 0 deletions
246
test/KubeOps.Operator.Web.Test/Webhooks/Conversion/ConversionWebhook.Test.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,246 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Runtime.Versioning; | ||
| using System.Text.Json.Nodes; | ||
|
|
||
| using FluentAssertions; | ||
|
|
||
| using k8s.Models; | ||
|
|
||
| using KubeOps.Abstractions.Entities; | ||
| using KubeOps.Operator.Web.Webhooks.Conversion; | ||
|
|
||
| namespace KubeOps.Operator.Web.Test.Webhooks.Conversion; | ||
|
|
||
| [Trait("Area", "ConversionWebhook")] | ||
| [RequiresPreviewFeatures] | ||
| public sealed class ConversionWebhookTest | ||
| { | ||
| private const string Group = "kubeops.test"; | ||
|
|
||
| [Fact(DisplayName = "Converts between two non-storage versions by composing through the storage version")] | ||
| public void Convert_ComposesThroughStorageVersion_ForTwoNonStorageVersions() | ||
| { | ||
| // v2 has no direct converter to v1; the only registered converters are v1<->v3 and v2<->v3 (v3 = storage). | ||
| var result = Convert(Request( | ||
| desired: $"{Group}/v1", | ||
| @object: """{"apiVersion":"kubeops.test/v2","kind":"Subject","metadata":{"name":"s"},"spec":{"firstName":"Jane","lastName":"Doe"}}""")); | ||
|
|
||
| var converted = result.Response.ConvertedObjects.Should().ContainSingle().Subject; | ||
| converted.Should().BeOfType<V1Subject>().Which.Spec.FullName.Should().Be("Jane Doe"); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Composes through the storage version in the reverse direction")] | ||
| public void Convert_ComposesThroughStorageVersion_ReverseDirection() | ||
| { | ||
| var result = Convert(Request( | ||
| desired: $"{Group}/v2", | ||
| @object: """{"apiVersion":"kubeops.test/v1","kind":"Subject","metadata":{"name":"s"},"spec":{"fullName":"Jane Doe"}}""")); | ||
|
|
||
| var converted = result.Response.ConvertedObjects.Should().ContainSingle().Subject; | ||
| var spec = converted.Should().BeOfType<V2Subject>().Which.Spec; | ||
| spec.FirstName.Should().Be("Jane"); | ||
| spec.LastName.Should().Be("Doe"); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Direct conversion to the storage version still works")] | ||
| public void Convert_DirectHopToStorageVersion_StillWorks() | ||
| { | ||
| var result = Convert(Request( | ||
| desired: $"{Group}/v3", | ||
| @object: """{"apiVersion":"kubeops.test/v1","kind":"Subject","metadata":{"name":"s"},"spec":{"fullName":"Jane Doe"}}""")); | ||
|
|
||
| var converted = result.Response.ConvertedObjects.Should().ContainSingle().Subject; | ||
| var spec = converted.Should().BeOfType<V3Subject>().Which.Spec; | ||
| spec.FirstName.Should().Be("Jane"); | ||
| spec.LastName.Should().Be("Doe"); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Direct conversion from the storage version still works")] | ||
| public void Convert_DirectHopFromStorageVersion_StillWorks() | ||
| { | ||
| var result = Convert(Request( | ||
| desired: $"{Group}/v1", | ||
| @object: """{"apiVersion":"kubeops.test/v3","kind":"Subject","metadata":{"name":"s"},"spec":{"firstName":"Jane","lastName":"Doe"}}""")); | ||
|
|
||
| var converted = result.Response.ConvertedObjects.Should().ContainSingle().Subject; | ||
| converted.Should().BeOfType<V1Subject>().Which.Spec.FullName.Should().Be("Jane Doe"); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Returns an error when no conversion path exists")] | ||
| public void Convert_ReturnsError_WhenSourceVersionIsUnroutable() | ||
| { | ||
| var result = Convert(Request( | ||
| desired: $"{Group}/v1", | ||
| @object: """{"apiVersion":"kubeops.test/v9","kind":"Subject","metadata":{"name":"s"},"spec":{}}""")); | ||
|
|
||
| result.Response.ConvertedObjects.Should().BeEmpty(); | ||
| result.Response.Result.Message.Should().Contain($"{Group}/v9"); | ||
| result.Response.Result.Message.Should().Contain($"{Group}/v1"); | ||
| result.Response.Result.Message.Should().Contain("index 0"); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Returns an error when an object has no apiVersion")] | ||
| public void Convert_ReturnsError_WhenApiVersionIsMissing() | ||
| { | ||
| var result = Convert(Request( | ||
| desired: $"{Group}/v1", | ||
| @object: """{"kind":"Subject","metadata":{"name":"s"},"spec":{}}""")); | ||
|
|
||
| result.Response.ConvertedObjects.Should().BeEmpty(); | ||
| result.Response.Result.Message.Should().Contain("apiVersion"); | ||
| result.Response.Result.Message.Should().Contain("index 0"); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Converts every routable object in a batch")] | ||
| public void Convert_Batch_ConvertsRoutableObjects() | ||
| { | ||
| // A LIST response can carry many objects that repeat the same source version, exercising the per-request path | ||
| // cache. | ||
| var result = Convert(new ConversionRequest | ||
| { | ||
| Request = new ConversionRequest.ConversionRequestData | ||
| { | ||
| Uid = "test-uid", | ||
| DesiredApiVersion = $"{Group}/v1", | ||
| Objects = | ||
| [ | ||
| JsonNode.Parse("""{"apiVersion":"kubeops.test/v2","kind":"Subject","metadata":{"name":"a"},"spec":{"firstName":"Jane","lastName":"Doe"}}""")!, | ||
| JsonNode.Parse("""{"apiVersion":"kubeops.test/v2","kind":"Subject","metadata":{"name":"b"},"spec":{"firstName":"John","lastName":"Roe"}}""")!, | ||
| JsonNode.Parse("""{"apiVersion":"kubeops.test/v3","kind":"Subject","metadata":{"name":"c"},"spec":{"firstName":"Mary","lastName":"Sue"}}""")!, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| result.Response.ConvertedObjects.Should().HaveCount(3); | ||
| result.Response.ConvertedObjects[0].Should().BeOfType<V1Subject>().Which.Spec.FullName.Should().Be("Jane Doe"); | ||
| result.Response.ConvertedObjects[1].Should().BeOfType<V1Subject>().Which.Spec.FullName.Should().Be("John Roe"); | ||
| result.Response.ConvertedObjects[2].Should().BeOfType<V1Subject>().Which.Spec.FullName.Should().Be("Mary Sue"); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Rejects the complete batch when one object is unroutable")] | ||
| public void Convert_Batch_ReturnsError_WhenOneObjectIsUnroutable() | ||
| { | ||
| var result = Convert(new ConversionRequest | ||
| { | ||
| Request = new ConversionRequest.ConversionRequestData | ||
| { | ||
| Uid = "test-uid", | ||
| DesiredApiVersion = $"{Group}/v1", | ||
| Objects = | ||
| [ | ||
| JsonNode.Parse("""{"apiVersion":"kubeops.test/v2","kind":"Subject","metadata":{"name":"a"},"spec":{"firstName":"Jane","lastName":"Doe"}}""")!, | ||
| JsonNode.Parse("""{"apiVersion":"kubeops.test/v9","kind":"Subject","metadata":{"name":"b"},"spec":{}}""")!, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| result.Response.ConvertedObjects.Should().BeEmpty(); | ||
| result.Response.Result.Message.Should().Contain($"{Group}/v9"); | ||
| result.Response.Result.Message.Should().Contain("index 1"); | ||
| } | ||
|
|
||
| private static ConversionResponse Convert(ConversionRequest request) => | ||
| (ConversionResponse)new TestConversionWebhook().Convert(request); | ||
|
|
||
| private static ConversionRequest Request(string desired, string @object) => new() | ||
| { | ||
| Request = new ConversionRequest.ConversionRequestData | ||
| { | ||
| Uid = "test-uid", | ||
| DesiredApiVersion = desired, | ||
| Objects = [JsonNode.Parse(@object)!], | ||
| }, | ||
| }; | ||
|
|
||
| // Hub-and-spoke converter set exactly as the documentation prescribes: every served version converts to and from | ||
| // the storage version (v3). There is deliberately no direct v1<->v2 converter. | ||
| [RequiresPreviewFeatures] | ||
| private sealed class TestConversionWebhook : ConversionWebhook<V3Subject> | ||
| { | ||
| protected override IEnumerable<IEntityConverter<V3Subject>> Converters => [new V1ToV3(), new V2ToV3()]; | ||
| } | ||
|
|
||
| [RequiresPreviewFeatures] | ||
| private sealed class V1ToV3 : IEntityConverter<V3Subject> | ||
| { | ||
| public Type FromType => typeof(V1Subject); | ||
|
|
||
| public Type ToType => typeof(V3Subject); | ||
|
|
||
| public string FromGroupVersion => $"{Group}/v1"; | ||
|
|
||
| public string ToGroupVersion => $"{Group}/v3"; | ||
|
|
||
| public V3Subject Convert(object from) | ||
| { | ||
| var source = (V1Subject)from; | ||
| var parts = source.Spec.FullName.Split(' ', 2); | ||
| return new V3Subject | ||
| { | ||
| Metadata = source.Metadata, | ||
| Spec = { FirstName = parts[0], LastName = parts.Length > 1 ? parts[1] : string.Empty }, | ||
| }; | ||
| } | ||
|
|
||
| public object Revert(V3Subject to) => new V1Subject | ||
| { | ||
| Metadata = to.Metadata, | ||
| Spec = { FullName = $"{to.Spec.FirstName} {to.Spec.LastName}".Trim() }, | ||
| }; | ||
| } | ||
|
|
||
| [RequiresPreviewFeatures] | ||
| private sealed class V2ToV3 : IEntityConverter<V3Subject> | ||
| { | ||
| public Type FromType => typeof(V2Subject); | ||
|
|
||
| public Type ToType => typeof(V3Subject); | ||
|
|
||
| public string FromGroupVersion => $"{Group}/v2"; | ||
|
|
||
| public string ToGroupVersion => $"{Group}/v3"; | ||
|
|
||
| public V3Subject Convert(object from) | ||
| { | ||
| var source = (V2Subject)from; | ||
| return new V3Subject { Metadata = source.Metadata, Spec = { FirstName = source.Spec.FirstName, LastName = source.Spec.LastName } }; | ||
| } | ||
|
|
||
| public object Revert(V3Subject to) => | ||
| new V2Subject { Metadata = to.Metadata, Spec = { FirstName = to.Spec.FirstName, LastName = to.Spec.LastName } }; | ||
| } | ||
|
|
||
| [KubernetesEntity(Group = Group, ApiVersion = "v1", Kind = "Subject")] | ||
| private sealed class V1Subject : CustomKubernetesEntity<V1Subject.SpecDef> | ||
| { | ||
| public sealed class SpecDef | ||
| { | ||
| public string FullName { get; set; } = string.Empty; | ||
| } | ||
| } | ||
|
|
||
| [KubernetesEntity(Group = Group, ApiVersion = "v2", Kind = "Subject")] | ||
| private sealed class V2Subject : CustomKubernetesEntity<V2Subject.SpecDef> | ||
| { | ||
| public sealed class SpecDef | ||
| { | ||
| public string FirstName { get; set; } = string.Empty; | ||
|
|
||
| public string LastName { get; set; } = string.Empty; | ||
| } | ||
| } | ||
|
|
||
| [KubernetesEntity(Group = Group, ApiVersion = "v3", Kind = "Subject")] | ||
| private sealed class V3Subject : CustomKubernetesEntity<V3Subject.SpecDef> | ||
| { | ||
| public sealed class SpecDef | ||
| { | ||
| public string FirstName { get; set; } = string.Empty; | ||
|
|
||
| public string LastName { get; set; } = string.Empty; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.