Skip to content
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

Enable AOT compatibility for Yarp.Kubernetes.Controller #2145

Open
eerhardt opened this issue May 24, 2023 · 0 comments
Open

Enable AOT compatibility for Yarp.Kubernetes.Controller #2145

eerhardt opened this issue May 24, 2023 · 0 comments
Labels
Kubernetes Ingress Controller Type: Idea This issue is a high-level idea for discussion.
Milestone

Comments

@eerhardt
Copy link
Member

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 - Developers can safely trim apps which need Configuration Binder dotnet/runtime#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 dotnet/runtime#79311. We need this functionality to serialize a Message:

public struct Message
#pragma warning restore CA1815 // Override equals and operator equals on value types
{
[JsonConverter(typeof(JsonStringEnumConverter))]
public MessageType MessageType { get; set; }

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>(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>(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>(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>(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>(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>(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<TOptions>(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<TOptions>(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>(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>(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<TValue>(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<TValue>(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.

@eerhardt eerhardt added the Type: Idea This issue is a high-level idea for discussion. label May 24, 2023
eerhardt added a commit to eerhardt/reverse-proxy that referenced this issue May 24, 2023
Also fix a simple warning in Yarp.Kubernetes.Controller. The rest of the warnings in this project will be addressed in microsoft#2145.
MihaZupan pushed a commit that referenced this issue May 24, 2023
* Enable AOT compatibility for Yarp.Telemetry.Consumption

Also fix a simple warning in Yarp.Kubernetes.Controller. The rest of the warnings in this project will be addressed in #2145.

* Fix TryAddSingleton warning that isn't caught by the Roslyn analyzer.

See https://github.com/dotnet/runtime/issues/86720 for why the warning isn't being raised.
@MihaZupan MihaZupan added this to the Backlog milestone May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Kubernetes Ingress Controller Type: Idea This issue is a high-level idea for discussion.
Projects
None yet
Development

No branches or pull requests

2 participants