dotnet / dotnet/yarp

Enable AOT compatibility for Yarp.Kubernetes.Controller

Open
#2,145 3 comments 3 reactions 2 assignees Claimed by @eerhardt View on GitHub
Kubernetes Ingress Controller Type: Idea
Dominant language
C#
Stars
9.6k
Forks
933
Avg merge
12d 18h
Merged PRs (30d)
2

Description

### What should we add or change to make your life better?

Address all trimming and AOT warnings in Yarp.Kubernetes.Controller.

The current warnings are from 2 API usages:

1. Using System.Text.Json Serialization
2. Using ConfigurationBinder

Both of these APIs use unbounded runtime Reflection. There are 2 general approaches to resolving these warnings:

1. Use the Source Generators for each. Note that the ConfigurationBinder source generator is new in .NET 8 - https://github.com/dotnet/runtime/issues/44493.
2. Manually writing the code the source generator/Reflection is doing. For example, using Utf8JsonReader/Writer.

I would suggest using the Source Generators.

Note that using the JSON Source Generator right now isn't possible because of https://github.com/dotnet/runtime/issues/79311. We need this functionality to serialize a `Message`:

https://github.com/microsoft/reverse-proxy/blob/7ef816467bb5bd0e82f08898a800e546ca0f86b9/src/Kubernetes.Controller/Protocol/Message.cs#L18-L22

So we will have to wait until that bug is fixed.

Here are the current warnings from this project:

```
1>C:\git\reverse-proxy\src\Kubernetes.Controller\Services\Reconciler.cs(62,36,62,82): warning IL3050: Using member 'System.Text.Json.JsonSerializer.Serialize(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.
1>C:\git\reverse-proxy\src\Kubernetes.Controller\Services\Reconciler.cs(63,36,63,70): warning IL3050: Using member 'System.Text.Json.JsonSerializer.Serialize(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.
1>C:\git\reverse-proxy\src\Kubernetes.Controller\Services\Reconciler.cs(62,36,62,82): warning IL2026: Using member 'System.Text.Json.JsonSerializer.Serialize(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.
1>C:\git\reverse-proxy\src\Kubernetes.Controller\Services\Reconciler.cs(63,36,63,70): warning IL2026: Using member 'System.Text.Json.JsonSerializer.Serialize(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.
1>C:\git\reverse-proxy\src\Kubernetes.Controller\Protocol\DispatchConfigProvider.cs(33,21,33,65): warning IL3050: Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.
1>C:\git\reverse-proxy\src\Kubernetes.Controller\Protocol\DispatchConfigProvider.cs(33,21,33,65): warning IL2026: Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.
1>C:\git\reverse-proxy\src\Kubernetes.Controller\Management\KubernetesReverseProxyServiceCollectionExtensions.cs(83,9,83,67): warning IL3050: Using member 'Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure(IServiceCollection, IConfiguration)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Binding strongly typed objects to configuration values may require generating dynamic code at runtime.
1>C:\git\reverse-proxy\src\Kubernetes.Controller\Management\KubernetesReverseProxyServiceCollectionExtensions.cs(83,9,83,67): warning IL2026: Using member 'Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure(IServiceCollection, IConfiguration)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. TOptions's dependent types may have their members trimmed. Ensure all required members are preserved.
1>C:\git\reverse-proxy\src\Kubernetes.Controller\Protocol\DispatchActionResult.cs(57,29,60,15): warning IL3050: Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.
1>C:\git\reverse-proxy\src\Kubernetes.Controller\Protocol\DispatchActionResult.cs(57,29,60,15): warning IL2026: Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.
1>C:\git\reverse-proxy\src\Kubernetes.Controller\Protocol\Receiver.cs(75,35,75,93): warning IL3050: Using member 'System.Text.Json.JsonSerializer.Deserialize(String, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.
1>C:\git\reverse-proxy\src\Kubernetes.Controller\Protocol\Receiver.cs(75,35,75,93): warning IL2026: Using member 'System.Text.Json.JsonSerializer.Deserialize(String, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.
```

### Why is this important to you?

This allows Yarp.Kubernetes.Controller to be used in a Native AOT app, which has smaller disk footprint, faster start up, and less memory consumption.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.