Skip to content

Implement richer extensibility mechanisms for routes and clusters in … #2682

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions YARP.sln
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpLoadApp", "samples\Prom
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yarp.Application", "src\Application\Yarp.Application.csproj", "{B63DDC4F-DA04-4B03-BF82-302FB3A954D7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReverseProxy.ConfigExtensions.Sample", "samples\ReverseProxy.ConfigExtensions.Sample\ReverseProxy.ConfigExtensions.Sample.csproj", "{B784E2E6-1A42-499F-9F59-BC938FBC6D4E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -363,6 +365,14 @@ Global
{B63DDC4F-DA04-4B03-BF82-302FB3A954D7}.Release|Any CPU.Build.0 = Release|Any CPU
{B63DDC4F-DA04-4B03-BF82-302FB3A954D7}.Release|x64.ActiveCfg = Release|Any CPU
{B63DDC4F-DA04-4B03-BF82-302FB3A954D7}.Release|x64.Build.0 = Release|Any CPU
{B784E2E6-1A42-499F-9F59-BC938FBC6D4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B784E2E6-1A42-499F-9F59-BC938FBC6D4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B784E2E6-1A42-499F-9F59-BC938FBC6D4E}.Debug|x64.ActiveCfg = Debug|Any CPU
{B784E2E6-1A42-499F-9F59-BC938FBC6D4E}.Debug|x64.Build.0 = Debug|Any CPU
{B784E2E6-1A42-499F-9F59-BC938FBC6D4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B784E2E6-1A42-499F-9F59-BC938FBC6D4E}.Release|Any CPU.Build.0 = Release|Any CPU
{B784E2E6-1A42-499F-9F59-BC938FBC6D4E}.Release|x64.ActiveCfg = Release|Any CPU
{B784E2E6-1A42-499F-9F59-BC938FBC6D4E}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -406,6 +416,7 @@ Global
{78A83196-53F3-444B-84BF-F0FDC2CD0466} = {149C61A2-D9F8-49B9-9F9B-3C953FEF53AA}
{BD73A038-8F3D-4BB2-A5C4-C8D077969DED} = {78A83196-53F3-444B-84BF-F0FDC2CD0466}
{B63DDC4F-DA04-4B03-BF82-302FB3A954D7} = {6CBE18D4-64E9-492B-BB02-58CD57126C10}
{B784E2E6-1A42-499F-9F59-BC938FBC6D4E} = {149C61A2-D9F8-49B9-9F9B-3C953FEF53AA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {31F6924A-E427-4830-96E9-B47CEB7BFE78}
Expand Down
67 changes: 67 additions & 0 deletions samples/ReverseProxy.ConfigExtensions.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Text.Json;
using Yarp.ReverseProxy.Configuration;
using Yarp.ReverseProxy.Model;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"))
.AddRouteExtensions<UserModel>("User")
.AddRouteExtensions<ABTest>("ABTest")
.AddClusterExtensions<Service>("Service")
//Error type
.AddRouteExtensions<More>("More");

var app = builder.Build();

app.MapReverseProxy();

app.UseRouting();
app.UseEndpoints(endpoints =>
{

endpoints.MapReverseProxy(proxyPipeline =>
{
proxyPipeline.Use((context, next) =>
{
var proxyFeature = context.Features.Get<IReverseProxyFeature>();

var user = proxyFeature?.Route.Config.GetExtension<UserModel>();
Console.WriteLine(user?.Name);

var ab = proxyFeature?.Route.Config.GetExtension<ABTest>();
Console.WriteLine(JsonSerializer.Serialize(ab));

var more = proxyFeature?.Route.Config.GetExtension<More>();
Console.WriteLine(more?.Information);

var service = proxyFeature?.Cluster.Config.GetExtension<Service>();
Console.WriteLine(service?.State);

return next();
});
});
});

app.Run();


public class UserModel : IConfigExtension
{
public string Name { get; set; }
}

public class More : IConfigExtension
{
public string Information { get; set; }
}

public class ABTest : IConfigExtension
{
public Dictionary<string, double> ABTests { get; set; }
}

public class Service : IConfigExtension
{
public string State { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:40411",
"sslPort": 44304
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5088",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7010;http://localhost:5088",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>$(ReleaseTFMs)</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

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

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
45 changes: 45 additions & 0 deletions samples/ReverseProxy.ConfigExtensions.Sample/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"route1": {
"ClusterId": "cluster1",
"Match": {
"Path": "{**catch-all}"
},
"Extensions": {
"User": {
"Name": "admin"
},
"More": "string",
"ABTest": {
"ABTests": {
"C1": 1,
"C2": 2
}
}
}
}
},
"Clusters": {
"cluster1": {
"Destinations": {
"destination1": {
"Address": "https://example.com/"
}
},
"Extensions": {
"Service": {
"State": "Error"
}
}
}
}
}
}
53 changes: 36 additions & 17 deletions src/ReverseProxy/Configuration/ClusterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ public sealed record ClusterConfig
/// </summary>
public IReadOnlyDictionary<string, string>? Metadata { get; init; }

/// <summary>
/// Arbitrary type-value pairs that further extend this cluster.
/// </summary>
public IReadOnlyDictionary<Type, IConfigExtension>? Extensions { get; init; }

public T? GetExtension<T>() where T : IConfigExtension
{
if (Extensions?.TryGetValue(typeof(T), out var extension) ?? false)
{
return (T)extension;
}

return default;
}

public bool Equals(ClusterConfig? other)
{
if (other is null)
Expand All @@ -62,7 +77,7 @@ public bool Equals(ClusterConfig? other)
}

return EqualsExcludingDestinations(other)
&& CollectionEqualityHelper.Equals(Destinations, other.Destinations);
&& CollectionEqualityHelper.Equals(Destinations, other.Destinations);
}

internal bool EqualsExcludingDestinations(ClusterConfig other)
Expand All @@ -73,25 +88,29 @@ internal bool EqualsExcludingDestinations(ClusterConfig other)
}

return string.Equals(ClusterId, other.ClusterId, StringComparison.OrdinalIgnoreCase)
&& string.Equals(LoadBalancingPolicy, other.LoadBalancingPolicy, StringComparison.OrdinalIgnoreCase)
// CS0252 warning only shows up in VS https://github.com/dotnet/roslyn/issues/49302
&& SessionAffinity == other.SessionAffinity
&& HealthCheck == other.HealthCheck
&& HttpClient == other.HttpClient
&& HttpRequest == other.HttpRequest
&& CaseSensitiveEqualHelper.Equals(Metadata, other.Metadata);
&& string.Equals(LoadBalancingPolicy, other.LoadBalancingPolicy, StringComparison.OrdinalIgnoreCase)
// CS0252 warning only shows up in VS https://github.com/dotnet/roslyn/issues/49302
&& SessionAffinity == other.SessionAffinity
&& HealthCheck == other.HealthCheck
&& HttpClient == other.HttpClient
&& HttpRequest == other.HttpRequest
&& CaseSensitiveEqualHelper.Equals(Metadata, other.Metadata)
&& CollectionEqualityHelper.Equals(Extensions, other.Extensions);
}

public override int GetHashCode()
{
return HashCode.Combine(
ClusterId?.GetHashCode(StringComparison.OrdinalIgnoreCase),
LoadBalancingPolicy?.GetHashCode(StringComparison.OrdinalIgnoreCase),
SessionAffinity,
HealthCheck,
HttpClient,
HttpRequest,
CollectionEqualityHelper.GetHashCode(Destinations),
CaseSensitiveEqualHelper.GetHashCode(Metadata));
// HashCode.Combine(...) takes only 8 arguments
var hash = new HashCode();
hash.Add(ClusterId?.GetHashCode(StringComparison.OrdinalIgnoreCase));
hash.Add(LoadBalancingPolicy?.GetHashCode(StringComparison.OrdinalIgnoreCase));
hash.Add(SessionAffinity);
hash.Add(HealthCheck);
hash.Add(HttpClient);
hash.Add(HttpRequest);
hash.Add(CollectionEqualityHelper.GetHashCode(Destinations));
hash.Add(CaseSensitiveEqualHelper.GetHashCode(Metadata));
hash.Add(CollectionEqualityHelper.GetHashCode(Extensions));
return hash.ToHashCode();
}
}
13 changes: 13 additions & 0 deletions src/ReverseProxy/Configuration/ConfigExtensionsOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;

namespace Yarp.ReverseProxy.Configuration;

public class ConfigExtensionsOptions
{
public Dictionary<string, Type> RouteExtensions { get; } = new();
public Dictionary<string, Type> ClusterExtensions { get; } = new();
}
Loading
Loading