Skip to content

Enable AOT compatibility for Yarp.Kubernetes.Controller#3010

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/enable-aot-compatibility
Draft

Enable AOT compatibility for Yarp.Kubernetes.Controller#3010
Copilot wants to merge 5 commits intomainfrom
copilot/enable-aot-compatibility

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 8, 2026

Yarp.Kubernetes.Controller emits IL2026/IL3050 warnings due to reflection-based JSON serialization and configuration binding, blocking use in Native AOT/trimmed applications.

Changes

JSON Source Generator (System.Text.Json)

  • New KubernetesJsonSerializerContextJsonSerializerContext with [JsonSerializable] for Message, List<RouteConfig>, and List<ClusterConfig>; transitive types are handled automatically
  • Message.cs — switched [JsonConverter(typeof(JsonStringEnumConverter))] to the generic JsonStringEnumConverter<MessageType> (introduced in .NET 8, required for source-gen compatibility)
  • DispatchConfigProvider, DispatchActionResult, Receiver, Reconciler — all JsonSerializer.* calls now pass the generated JsonTypeInfo<T> from KubernetesJsonSerializerContext.Default:
// Before
var bytes = JsonSerializer.SerializeToUtf8Bytes(message);
var message = JsonSerializer.Deserialize<Message>(json);

// After
var bytes = JsonSerializer.SerializeToUtf8Bytes(message, KubernetesJsonSerializerContext.Default.Message);
var message = JsonSerializer.Deserialize<Message>(json, KubernetesJsonSerializerContext.Default.Message);

ConfigurationBinder Source Generator

  • KubernetesReverseProxyServiceCollectionExtensions — replaced services.Configure<YarpOptions>(config.GetSection("Yarp")) with an explicit Bind call so the Microsoft.Extensions.Configuration.Binder source generator can intercept and emit AOT-safe binding code:
// Before — hides the Bind call inside the extension, generator can't intercept
services.Configure<YarpOptions>(config.GetSection("Yarp"));

// After — explicit Bind call is intercepted by the source generator
services.Configure<YarpOptions>(o => config.GetSection("Yarp").Bind(o));

AOT Compatibility Test

  • <IsAotCompatible>true</IsAotCompatible> added to Yarp.Kubernetes.Controller.csproj — enables build-time Roslyn trim/AOT static analysis on the library, surfacing any future violations as build errors.

  • New testassets/Kubernetes.AotCompatibility.TestApp — a PublishAot=true console app targeting net10.0 (following the dotnet/extensions pattern) that references all three AOT-compatible YARP assemblies with TrimmerRootAssembly for each:

    • Yarp.Kubernetes.Controller: Combined (AddKubernetesReverseProxy + UseKubernetesReverseProxyCertificateSelector), Monitor (AddKubernetesIngressMonitor + AddKubernetesDispatchController), and Receiver (Configure<ReceiverOptions> + Receiver + LoadFromMessages) scenarios
    • Yarp.ReverseProxy: Standalone reverse proxy scenario (AddReverseProxy().LoadFromConfig(...))
    • Yarp.Telemetry.Consumption: Telemetry listener scenario (AddTelemetryListeners())

    Publishing this app with AOT will flag any trim/AOT regressions introduced in the future across all AOT-compatible YARP libraries.

Copilot AI and others added 2 commits April 8, 2026 16:11
Agent-Logs-Url: https://github.com/dotnet/yarp/sessions/b1d776b4-b2b5-4a50-b364-4aec6b74df35

Co-authored-by: eerhardt <8291187+eerhardt@users.noreply.github.com>
- Use JsonStringEnumConverter<MessageType> (generic) in Message.cs for source generator compatibility
- Create KubernetesJsonSerializerContext with JsonSerializerContext for AOT-safe JSON serialization
- Update DispatchConfigProvider, DispatchActionResult, Receiver to use source-generated JsonSerializer methods
- Update Reconciler to use source-generated serialization for logging
- Update KubernetesReverseProxyServiceCollectionExtensions to use ConfigurationBinder source generator-compatible Bind call

Agent-Logs-Url: https://github.com/dotnet/yarp/sessions/b1d776b4-b2b5-4a50-b364-4aec6b74df35

Co-authored-by: eerhardt <8291187+eerhardt@users.noreply.github.com>
Copilot AI changed the title [WIP] Address trimming and AOT warnings in Yarp.Kubernetes.Controller Enable AOT compatibility for Yarp.Kubernetes.Controller Apr 8, 2026
Copilot AI requested a review from eerhardt April 8, 2026 16:23
@eerhardt
Copy link
Copy Markdown
Member

eerhardt commented Apr 8, 2026

- Add IsAotCompatible=true to Yarp.Kubernetes.Controller.csproj to enable build-time trim/AOT analysis
- Create testassets/Kubernetes.AotCompatibility.TestApp exercising the three main usage scenarios:
  combined (AddKubernetesReverseProxy), monitor (AddKubernetesIngressMonitor + AddKubernetesDispatchController),
  and receiver (Configure<ReceiverOptions> + Receiver + LoadFromMessages)
- Add test app to YARP.slnx solution

Agent-Logs-Url: https://github.com/dotnet/yarp/sessions/24caf1e2-cfc2-4e24-ae5b-76759b3c709c

Co-authored-by: eerhardt <8291187+eerhardt@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 8, 2026

@copilot - also add an AOT compatibility test like described in

https://devblogs.microsoft.com/dotnet/creating-aot-compatible-libraries/#analyzing-.net-libraries
...

Done in dd0c957. Added two things:

  1. <IsAotCompatible>true</IsAotCompatible> to Yarp.Kubernetes.Controller.csproj — enables build-time Roslyn trim/AOT analyzers on the library itself, surfacing any future violations as build errors.

  2. testassets/Kubernetes.AotCompatibility.TestApp/ — a PublishAot=true console app (patterned after the dotnet/extensions example) that exercises the three main usage scenarios: combined (AddKubernetesReverseProxy), monitor (AddKubernetesIngressMonitor + AddKubernetesDispatchController), and receiver (Configure<ReceiverOptions> + Receiver + LoadFromMessages). Publishing this app with AOT will flag any trim/AOT regressions.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • devblogs.microsoft.com
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>$(ReleaseTFMs)</TargetFrameworks>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot Just make this the latest TFM - net10.0.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in b5bf7f6 — changed to <TargetFramework>net10.0</TargetFramework> (singular).

</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Kubernetes.Controller\Yarp.Kubernetes.Controller.csproj" />
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot - this should reference all the projects that are AOT compatible in teh repo.

Also, it needs TrimmerRootAssembly for each one. see https://github.com/dotnet/extensions/blob/b3f2f408fe70f0d8e1f8fb2385bcfc8e9e81158d/test/Libraries/Microsoft.Extensions.AotCompatibility.TestApp/Microsoft.Extensions.AotCompatibility.TestApp.csproj for an example

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in b5bf7f6. The csproj now references all three AOT-compatible YARP assemblies with TrimmerRootAssembly for each, and Program.cs adds scenarios for AddReverseProxy().LoadFromConfig(...) and AddTelemetryListeners().

…d TrimmerRootAssembly

- Change TargetFrameworks to net10.0 (single TFM as requested)
- Add project references for all three AOT-compatible YARP assemblies:
  Yarp.Kubernetes.Controller, Yarp.ReverseProxy, Yarp.Telemetry.Consumption
- Add TrimmerRootAssembly items for each assembly to ensure trimmer roots them
- Extend Program.cs with ReverseProxy and TelemetryConsumption usage scenarios

Agent-Logs-Url: https://github.com/dotnet/yarp/sessions/9f2472df-913f-4feb-82f7-9e5b65f7d1c3

Co-authored-by: eerhardt <8291187+eerhardt@users.noreply.github.com>
Copilot AI requested a review from eerhardt April 8, 2026 18:02
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.

Enable AOT compatibility for Yarp.Kubernetes.Controller

2 participants