Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions YARP.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
</Folder>
<Folder Name="/testassets/">
<Project Path="testassets/BenchmarkApp/BenchmarkApp.csproj" />
<Project Path="testassets/Kubernetes.AotCompatibility.TestApp/Yarp.Kubernetes.AotCompatibility.TestApp.csproj" />
<Project Path="testassets/ReverseProxy.Code/ReverseProxy.Code.csproj" />
<Project Path="testassets/ReverseProxy.Config/ReverseProxy.Config.csproj" />
<Project Path="testassets/ReverseProxy.Direct/ReverseProxy.Direct.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
services.AddHostedService<IngressController>();
services.AddSingleton<ICache, IngressCache>();
services.AddTransient<IReconciler, Reconciler>();
services.Configure<YarpOptions>(config.GetSection("Yarp"));
services.Configure<YarpOptions>(o => config.GetSection("Yarp").Bind(o));

Check failure on line 83 in src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)

src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs#L83

src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs(83,46): error IL3050: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration, Object)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types.

Check failure on line 83 in src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)

src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs#L83

src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs(83,46): error IL2026: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration, Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Cannot statically analyze the type of instance so its members may be trimmed.

Check failure on line 83 in src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)

src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs#L83

src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs(83,46): error IL3050: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration, Object)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types.

Check failure on line 83 in src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)

src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs#L83

src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs(83,46): error IL2026: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration, Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Cannot statically analyze the type of instance so its members may be trimmed.

Check failure on line 83 in src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci

src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs#L83

src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs(83,46): error IL3050: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration, Object)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types.

// Register the necessary Kubernetes resource informers
services.RegisterResourceInformer<V1Ingress, V1IngressResourceInformer>();
Expand Down Expand Up @@ -112,7 +112,7 @@
where TResource : class, IKubernetesObject<V1ObjectMeta>, new()
where TService : IResourceInformer<TResource>
{
return services.RegisterResourceInformer<TResource, TService>(null);

Check failure on line 115 in src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)

src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs#L115

src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs(115,16): error IL2091: (NETCORE_ENGINEERING_TELEMETRY=Build) 'TService' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in 'Microsoft.Extensions.DependencyInjection.KubernetesReverseProxyServiceCollectionExtensions.RegisterResourceInformer<TResource, TService>(IServiceCollection, String)'. The generic parameter 'TService' of 'Microsoft.Extensions.DependencyInjection.KubernetesReverseProxyServiceCollectionExtensions.RegisterResourceInformer<TResource, TService>(IServiceCollection)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Check failure on line 115 in src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)

src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs#L115

src/Kubernetes.Controller/Management/KubernetesReverseProxyServiceCollectionExtensions.cs(115,16): error IL2091: (NETCORE_ENGINEERING_TELEMETRY=Build) 'TService' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in 'Microsoft.Extensions.DependencyInjection.KubernetesReverseProxyServiceCollectionExtensions.RegisterResourceInformer<TResource, TService>(IServiceCollection, String)'. The generic parameter 'TService' of 'Microsoft.Extensions.DependencyInjection.KubernetesReverseProxyServiceCollectionExtensions.RegisterResourceInformer<TResource, TService>(IServiceCollection)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Yarp.Kubernetes.Controller.Protocol;
using Yarp.Kubernetes.Protocol;

namespace Yarp.Kubernetes.Controller.Dispatching;
Expand Down Expand Up @@ -54,7 +55,7 @@ public async Task ExecuteResultAsync(ActionContext context)
var utf8Bytes = JsonSerializer.SerializeToUtf8Bytes(new Message
{
MessageType = MessageType.Heartbeat
});
}, KubernetesJsonSerializerContext.Default.Message);

while (!cancellationToken.IsCancellationRequested)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task UpdateAsync(IReadOnlyList<RouteConfig> routes, IReadOnlyList<C
Routes = routes.ToList(),
};

var bytes = JsonSerializer.SerializeToUtf8Bytes(message);
var bytes = JsonSerializer.SerializeToUtf8Bytes(message, KubernetesJsonSerializerContext.Default.Message);

await _dispatcher.SendAsync(bytes, cancellationToken).ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Text.Json.Serialization;
using Yarp.Kubernetes.Protocol;
using Yarp.ReverseProxy.Configuration;

namespace Yarp.Kubernetes.Controller.Protocol;

[JsonSerializable(typeof(Message))]
[JsonSerializable(typeof(List<RouteConfig>))]
[JsonSerializable(typeof(List<ClusterConfig>))]
internal sealed partial class KubernetesJsonSerializerContext : JsonSerializerContext
{
}
2 changes: 1 addition & 1 deletion src/Kubernetes.Controller/Protocol/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public enum MessageType

public struct Message
{
[JsonConverter(typeof(JsonStringEnumConverter))]
[JsonConverter(typeof(JsonStringEnumConverter<MessageType>))]
public MessageType MessageType { get; set; }

public string Key { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion src/Kubernetes.Controller/Protocol/Receiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Extensions.Options;
using Yarp.Kubernetes.Controller.Configuration;
using Yarp.Kubernetes.Controller.Hosting;
using Yarp.Kubernetes.Controller.Protocol;
using Yarp.Kubernetes.Controller.Rate;

namespace Yarp.Kubernetes.Protocol;
Expand Down Expand Up @@ -65,7 +66,7 @@ public override async Task RunAsync(CancellationToken cancellationToken)
break;
}

var message = System.Text.Json.JsonSerializer.Deserialize<Message>(json);
var message = System.Text.Json.JsonSerializer.Deserialize<Message>(json, KubernetesJsonSerializerContext.Default.Message);
Logger.LogInformation("Received {MessageType} for {MessageKey}", message.MessageType, message.Key);

Logger.LogInformation(json);
Expand Down
5 changes: 3 additions & 2 deletions src/Kubernetes.Controller/Services/Reconciler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Yarp.Kubernetes.Controller.Client;
using Yarp.Kubernetes.Controller.Configuration;
using Yarp.Kubernetes.Controller.Converters;
using Yarp.Kubernetes.Controller.Protocol;

namespace Yarp.Kubernetes.Controller.Services;

Expand Down Expand Up @@ -63,8 +64,8 @@ public async Task ProcessAsync(CancellationToken cancellationToken)

var clusters = configContext.BuildClusterConfig();

_logger.LogInformation(JsonSerializer.Serialize(configContext.Routes));
_logger.LogInformation(JsonSerializer.Serialize(clusters));
_logger.LogInformation(JsonSerializer.Serialize(configContext.Routes, KubernetesJsonSerializerContext.Default.ListRouteConfig));
_logger.LogInformation(JsonSerializer.Serialize(clusters, KubernetesJsonSerializerContext.Default.ListClusterConfig));

await _updateConfig.UpdateAsync(configContext.Routes, clusters, cancellationToken).ConfigureAwait(false);
await _ingressResourceStatusUpdater.UpdateStatusAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<NoWarn>$(NoWarn);CS8002</NoWarn>
<IsPackable>true</IsPackable>
<IsShipping>false</IsShipping>
<IsAotCompatible>true</IsAotCompatible>
<PackageTags>yarp;dotnet;reverse-proxy;aspnetcore;kubernetes</PackageTags>
</PropertyGroup>

Expand Down
50 changes: 50 additions & 0 deletions testassets/Kubernetes.AotCompatibility.TestApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// This app is used to test AOT compatibility of all AOT-compatible YARP assemblies.
// It exercises the main public API surface to ensure no AOT/trimming warnings
// are emitted at build or publish time.

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Yarp.Kubernetes.Protocol;

// Combined controller scenario (ingress controller + YARP reverse proxy)
{
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseKubernetesReverseProxyCertificateSelector();
builder.Services.AddKubernetesReverseProxy(builder.Configuration);
_ = builder.Build();
}

// Monitor-only scenario (ingress monitor + dispatch controller)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddKubernetesIngressMonitor(builder.Configuration);
builder.Services.AddControllers().AddKubernetesDispatchController();
_ = builder.Build();
}

// Receiver scenario (side-car that receives config from ingress monitor)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<ReceiverOptions>(builder.Configuration.Bind);
builder.Services.AddHostedService<Receiver>();
builder.Services.AddReverseProxy().LoadFromMessages();
_ = builder.Build();
}

// ReverseProxy standalone scenario
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
_ = builder.Build();
}

// TelemetryConsumption scenario
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddTelemetryListeners();
_ = builder.Build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<OutputType>Exe</OutputType>
<PublishAot>true</PublishAot>
<Nullable>enable</Nullable>
</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().

<ProjectReference Include="..\..\src\ReverseProxy\Yarp.ReverseProxy.csproj" />
<ProjectReference Include="..\..\src\TelemetryConsumption\Yarp.Telemetry.Consumption.csproj" />
</ItemGroup>

<ItemGroup>
<TrimmerRootAssembly Include="Yarp.Kubernetes.Controller" />
<TrimmerRootAssembly Include="Yarp.ReverseProxy" />
<TrimmerRootAssembly Include="Yarp.Telemetry.Consumption" />
</ItemGroup>

</Project>
Loading