Skip to content

Add YARP container support #8856

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 9 commits 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
31 changes: 15 additions & 16 deletions Aspire.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
Expand All @@ -21,8 +20,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CatalogService", "playgroun
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasketService", "playground\TestShop\BasketService\BasketService.csproj", "{3FC74EA6-D554-4A87-AED5-A08FE407BBF4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiGateway", "playground\TestShop\ApiGateway\ApiGateway.csproj", "{934625C7-243F-4659-9421-515301CF0263}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.StackExchange.Redis", "src\Components\Aspire.StackExchange.Redis\Aspire.StackExchange.Redis.csproj", "{7D4A7A84-B297-4777-9E2A-D8B1C427CAC7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Components", "Components", "{27381127-6C45-4B4C-8F18-41FF48DFE4B2}"
Expand Down Expand Up @@ -675,6 +672,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.Azure.Npgsql.EntityF
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.Components.Common.Tests", "tests\Aspire.Components.Common.Tests\Aspire.Components.Common.Tests.csproj", "{30950CEB-2232-F9FC-04FF-ADDCB8AC30A7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.Hosting.Yarp", "src\Aspire.Hosting.Yarp\Aspire.Hosting.Yarp.csproj", "{A3399DE9-AAB0-43EA-B99B-6A62ABBDD7BF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -745,18 +744,6 @@ Global
{3FC74EA6-D554-4A87-AED5-A08FE407BBF4}.Release|x64.Build.0 = Release|Any CPU
{3FC74EA6-D554-4A87-AED5-A08FE407BBF4}.Release|x86.ActiveCfg = Release|Any CPU
{3FC74EA6-D554-4A87-AED5-A08FE407BBF4}.Release|x86.Build.0 = Release|Any CPU
{934625C7-243F-4659-9421-515301CF0263}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{934625C7-243F-4659-9421-515301CF0263}.Debug|Any CPU.Build.0 = Debug|Any CPU
{934625C7-243F-4659-9421-515301CF0263}.Debug|x64.ActiveCfg = Debug|Any CPU
{934625C7-243F-4659-9421-515301CF0263}.Debug|x64.Build.0 = Debug|Any CPU
{934625C7-243F-4659-9421-515301CF0263}.Debug|x86.ActiveCfg = Debug|Any CPU
{934625C7-243F-4659-9421-515301CF0263}.Debug|x86.Build.0 = Debug|Any CPU
{934625C7-243F-4659-9421-515301CF0263}.Release|Any CPU.ActiveCfg = Release|Any CPU
{934625C7-243F-4659-9421-515301CF0263}.Release|Any CPU.Build.0 = Release|Any CPU
{934625C7-243F-4659-9421-515301CF0263}.Release|x64.ActiveCfg = Release|Any CPU
{934625C7-243F-4659-9421-515301CF0263}.Release|x64.Build.0 = Release|Any CPU
{934625C7-243F-4659-9421-515301CF0263}.Release|x86.ActiveCfg = Release|Any CPU
{934625C7-243F-4659-9421-515301CF0263}.Release|x86.Build.0 = Release|Any CPU
{7D4A7A84-B297-4777-9E2A-D8B1C427CAC7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7D4A7A84-B297-4777-9E2A-D8B1C427CAC7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D4A7A84-B297-4777-9E2A-D8B1C427CAC7}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -3961,6 +3948,18 @@ Global
{30950CEB-2232-F9FC-04FF-ADDCB8AC30A7}.Release|x64.Build.0 = Release|Any CPU
{30950CEB-2232-F9FC-04FF-ADDCB8AC30A7}.Release|x86.ActiveCfg = Release|Any CPU
{30950CEB-2232-F9FC-04FF-ADDCB8AC30A7}.Release|x86.Build.0 = Release|Any CPU
{A3399DE9-AAB0-43EA-B99B-6A62ABBDD7BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A3399DE9-AAB0-43EA-B99B-6A62ABBDD7BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3399DE9-AAB0-43EA-B99B-6A62ABBDD7BF}.Debug|x64.ActiveCfg = Debug|Any CPU
{A3399DE9-AAB0-43EA-B99B-6A62ABBDD7BF}.Debug|x64.Build.0 = Debug|Any CPU
{A3399DE9-AAB0-43EA-B99B-6A62ABBDD7BF}.Debug|x86.ActiveCfg = Debug|Any CPU
{A3399DE9-AAB0-43EA-B99B-6A62ABBDD7BF}.Debug|x86.Build.0 = Debug|Any CPU
{A3399DE9-AAB0-43EA-B99B-6A62ABBDD7BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3399DE9-AAB0-43EA-B99B-6A62ABBDD7BF}.Release|Any CPU.Build.0 = Release|Any CPU
{A3399DE9-AAB0-43EA-B99B-6A62ABBDD7BF}.Release|x64.ActiveCfg = Release|Any CPU
{A3399DE9-AAB0-43EA-B99B-6A62ABBDD7BF}.Release|x64.Build.0 = Release|Any CPU
{A3399DE9-AAB0-43EA-B99B-6A62ABBDD7BF}.Release|x86.ActiveCfg = Release|Any CPU
{A3399DE9-AAB0-43EA-B99B-6A62ABBDD7BF}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -3971,7 +3970,6 @@ Global
{C7B2309C-073A-4552-A508-A69768B64C6F} = {A68BA1A5-1604-433D-9778-DC0199831C2A}
{6D04BB34-1CC6-4FF3-A02A-1FFAC2A7A4F3} = {A68BA1A5-1604-433D-9778-DC0199831C2A}
{3FC74EA6-D554-4A87-AED5-A08FE407BBF4} = {A68BA1A5-1604-433D-9778-DC0199831C2A}
{934625C7-243F-4659-9421-515301CF0263} = {A68BA1A5-1604-433D-9778-DC0199831C2A}
{7D4A7A84-B297-4777-9E2A-D8B1C427CAC7} = {27381127-6C45-4B4C-8F18-41FF48DFE4B2}
{145E569B-633F-4FF0-B273-CB1D3B49B9F2} = {63BB22D3-0FAC-4E87-BF9D-9FA2441684C9}
{7570F683-EB48-467F-B1B1-70FF0153F52B} = {63BB22D3-0FAC-4E87-BF9D-9FA2441684C9}
Expand Down Expand Up @@ -4285,6 +4283,7 @@ Global
{192747A2-9338-DECF-5C8C-28EB8E13829B} = {27381127-6C45-4B4C-8F18-41FF48DFE4B2}
{8FCA0CFA-7823-6A2F-342A-107A994915B0} = {C424395C-1235-41A4-BF55-07880A04368C}
{30950CEB-2232-F9FC-04FF-ADDCB8AC30A7} = {C424395C-1235-41A4-BF55-07880A04368C}
{A3399DE9-AAB0-43EA-B99B-6A62ABBDD7BF} = {B80354C7-BE58-43F6-8928-9F3A74AB7F47}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {47DCFECF-5631-4BDE-A1EC-BE41E90F60C4}
Expand Down
3 changes: 2 additions & 1 deletion playground/TestShop/TestShop.AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
builder.AddProject<Projects.OrderProcessor>("orderprocessor", launchProfileName: "OrderProcessor")
.WithReference(messaging).WaitFor(messaging);

builder.AddProject<Projects.ApiGateway>("apigateway")
builder.AddYarp("apigateway")
.WithConfigFile("yarp.json")
.WithReference(basketService)
.WithReference(catalogService);

Expand Down
3 changes: 1 addition & 2 deletions playground/TestShop/TestShop.AppHost/TestShop.AppHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
<AspireProjectOrPackageReference Include="Aspire.Hosting.RabbitMQ" />
<AspireProjectOrPackageReference Include="Aspire.Hosting.Redis" />
<AspireProjectOrPackageReference Include="Aspire.Hosting.PostgreSQL" />

<ProjectReference Include="..\ApiGateway\ApiGateway.csproj" />
<AspireProjectOrPackageReference Include="Aspire.Hosting.Yarp" />
<ProjectReference Include="..\BasketService\BasketService.csproj" />
<ProjectReference Include="..\CatalogDb\CatalogDb.csproj" />
<ProjectReference Include="..\CatalogService\CatalogService.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
49 changes: 49 additions & 0 deletions playground/TestShop/TestShop.AppHost/yarp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Information"
}
},
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"catalog": {
"ClusterId": "catalog",
"Match": {
"Path": "/catalog/{**catch-all}"
},
"Transforms": [
{ "PathRemovePrefix": "/catalog" }
]
},
"basket": {
"ClusterId": "basket",
"Match": {
"Path": "/basket/{**catch-all}"
},
"Transforms": [
{ "PathRemovePrefix": "/basket" }
]
}
},
"Clusters": {
"catalog": {
"Destinations": {
"catalog": {
"Address": "http://catalogservice",
"Health": "http://catalogservice/readiness"
}
}
},
"basket": {
"Destinations": {
"basket": {
"Address": "http://basketservice",
"Health": "http://basketservice/readiness"
}
}
}
}
}
}
1 change: 1 addition & 0 deletions spelling.dic
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ urls
kubernetes
Pgweb
elasticsearch
Yarp
19 changes: 19 additions & 0 deletions src/Aspire.Hosting.Yarp/Aspire.Hosting.Yarp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(DefaultTargetFramework)</TargetFramework>
<IsPackable>true</IsPackable>
<PackageTags>aspire integration hosting yarp reverse-proxy</PackageTags>
<Description>YARP support for .NET Aspire.</Description>
<EnablePackageValidation>false</EnablePackageValidation>
</PropertyGroup>

<PropertyGroup>
<MinCodeCoverage>0</MinCodeCoverage>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Aspire.Hosting\Aspire.Hosting.csproj" />
</ItemGroup>

</Project>
92 changes: 92 additions & 0 deletions src/Aspire.Hosting.Yarp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Aspire.Hosting.Yarp library

Provides extension methods and resource definitions for a .NET Aspire AppHost to configure a YARP instance.

## Getting started

### Install the package

In your AppHost project, install the .NET Aspire YARP Hosting library with [NuGet](https://www.nuget.org):

```dotnetcli
dotnet add package Aspire.Hosting.Yarp
```

## Usage example

Then, in the _Program.cs_ file of `AppHost`, add a YARP resource and provide the configuration file using the following methods:

```csharp
var catalogService = builder.AddProject<Projects.CatalogService>("catalogservice")
[...];
var basketService = builder.AddProject<Projects.BasketService>("basketservice")
[...];

builder.AddYarp("apigateway")
.WithConfigFile("yarp.json")
.WithReference(basketService)
.WithReference(catalogService);
```

The `yarp.json` configuration file can use the referenced service like this:

```json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Information"
}
},
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"catalog": {
"ClusterId": "catalog",
"Match": {
"Path": "/catalog/{**catch-all}"
},
"Transforms": [
{ "PathRemovePrefix": "/catalog" }
]
},
"basket": {
"ClusterId": "basket",
"Match": {
"Path": "/basket/{**catch-all}"
},
"Transforms": [
{ "PathRemovePrefix": "/basket" }
]
}
},
"Clusters": {
"catalog": {
"Destinations": {
"catalog": {
"Address": "http://catalogservice",
}
}
},
"basket": {
"Destinations": {
"basket": {
"Address": "http://basketservice",
}
}
}
}
}
}

```

## Additional documentation

* https://learn.microsoft.com/dotnet/aspire/caching/stackexchange-redis-component
* https://learn.microsoft.com/dotnet/aspire/caching/stackexchange-redis-output-caching-component
* https://learn.microsoft.com/dotnet/aspire/caching/stackexchange-redis-distributed-caching-component

## Feedback & contributing

https://github.com/dotnet/aspire
13 changes: 13 additions & 0 deletions src/Aspire.Hosting.Yarp/YarpContainerImageTags.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.

namespace Aspire.Hosting.Yarp;

internal static class YarpContainerImageTags
Copy link
Member

Choose a reason for hiding this comment

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

Seems like the other ones all have tags for each of these.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure to understand?

{
public const string Registry = "mcr.microsoft.com";

public const string Image = "dotnet/nightly/yarp";

public const string Tag = "2-preview";
}
18 changes: 18 additions & 0 deletions src/Aspire.Hosting.Yarp/YarpResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Hosting.ApplicationModel;

namespace Aspire.Hosting.Yarp;

/// <summary>
/// A resource that represents a YARP resource independent of the hosting model.
/// </summary>
/// <param name="name">The name of the resource.</param>
public class YarpResource(string name) : ContainerResource(name)
{
/// <summary>
/// File path of the config file for this YARP resource.
/// </summary>
public string? ConfigFilePath { get; set; }
}
92 changes: 92 additions & 0 deletions src/Aspire.Hosting.Yarp/YarpServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Yarp;
using Microsoft.Extensions.Hosting;

namespace Aspire.Hosting;

/// <summary>
/// Provides extension methods for adding YARP resources to the application model.
/// </summary>
public static class YarpServiceExtensions
{
private const int Port = 5000;

private const string ConfigDirectory = "/etc";

private const string ConfigFileName = "yarp.config";

/// <summary>
/// Adds a YARP container to the application model.
/// </summary>
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param>
/// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<YarpResource> AddYarp(
this IDistributedApplicationBuilder builder,
[ResourceName] string name)
{
var resource = new YarpResource(name);

var yarpBuilder = builder.AddResource(resource)
.WithHttpEndpoint(targetPort: Port)
.WithImage(YarpContainerImageTags.Image)
.WithImageRegistry(YarpContainerImageTags.Registry)
.WithEnvironment("ASPNETCORE_ENVIRONMENT", builder.Environment.EnvironmentName)
.WithOtlpExporter();

if (builder.Environment.IsDevelopment())
{
// YARP will not trust the cert used by Aspire otlp endpoint when running locally
// The Aspire otlp endpoint uses the dev cert, only valid for localhost, but from the container
// perspective, the url will be something like https://docker.host.internal, so it will NOT be valid.
yarpBuilder.WithEnvironment("YARP_UNSAFE_OLTP_CERT_ACCEPT_ANY_SERVER_CERTIFICATE", "true");
}

// Map the configuration file
yarpBuilder.WithContainerFiles(ConfigDirectory, async (context, ct) =>
{
string contents;
if (yarpBuilder.Resource.ConfigFilePath != null)
{
try
{
contents = await File.ReadAllTextAsync(yarpBuilder.Resource.ConfigFilePath, ct).ConfigureAwait(false);
}
catch (Exception ex)
{
throw new DistributedApplicationException($"Error when reading the YARP config file '{yarpBuilder.Resource.ConfigFilePath}'", ex);
}
}
else
{
// TODO: build dynamically the config file if none provided.
throw new DistributedApplicationException($"No configuration provided for YARP instance \"{yarpBuilder.Resource.Name}\"");
}

var configFile = new ContainerFile
{
Name = ConfigFileName,
Contents = contents
};

return [configFile];
});

return yarpBuilder;
}

/// <summary>
/// Set explicitly the config file to use for YARP.
/// </summary>
/// <param name="builder">The YARP resource to configure.</param>
/// <param name="configFilePath">The path to the YARP config file.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<YarpResource> WithConfigFile(this IResourceBuilder<YarpResource> builder, string configFilePath)
{
builder.Resource.ConfigFilePath = configFilePath;
return builder;
}
}
Loading