-
Notifications
You must be signed in to change notification settings - Fork 715
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
Changes from 1 commit
16e8774
20421f3
f4dad75
928f461
e776f80
81c8853
a524d72
c23cba8
1e303f3
289499f
01b7e54
ad87b8d
685fda5
cec8dbb
632c960
7a11cf2
db654f7
a23dc9b
23f5c65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
| 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" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,3 +65,4 @@ urls | |
| kubernetes | ||
| Pgweb | ||
| elasticsearch | ||
| Yarp | ||
benjaminpetit marked this conversation as resolved.
Show resolved
Hide resolved
|
| 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> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Aspire.Hosting.Yarp library | ||
|
|
||
benjaminpetit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like the other ones all have tags for each of these. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; | ||
|
|
||
| public const int Port = 5000; | ||
|
|
||
| public const string ConfigFilePath = "/etc/yarp.config"; | ||
| } | ||
| 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; } | ||
benjaminpetit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // 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 | ||
| { | ||
| /// <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: YarpContainerImageTags.Port) | ||
| .WithImage(YarpContainerImageTags.Image) | ||
| .WithImageRegistry(YarpContainerImageTags.Registry) | ||
| .WithEnvironment("ASPNETCORE_ENVIRONMENT", builder.Environment.EnvironmentName) | ||
| .WithOtlpExporter(); | ||
|
|
||
| if (builder.Environment.IsDevelopment()) | ||
benjaminpetit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| // YARP will not trust the cert used by aspire otlp endpoint when running locally | ||
benjaminpetit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| yarpBuilder.WithEnvironment("YARP_UNSAFE_OLTP_CERT_ACCEPT_ANY_SERVER_CERTIFICATE", "true"); | ||
| } | ||
|
|
||
| // Map the configuration file | ||
| yarpBuilder.ApplicationBuilder.Eventing.Subscribe<AfterEndpointsAllocatedEvent>((e, ct) => | ||
benjaminpetit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| if (yarpBuilder.Resource.ConfigFilePath != null) | ||
| { | ||
| yarpBuilder.WithBindMount(yarpBuilder.Resource.ConfigFilePath, YarpContainerImageTags.ConfigFilePath, isReadOnly: true); | ||
benjaminpetit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| else | ||
| { | ||
| // TODO: build dynamically the config file if none provided. | ||
| throw new DistributedApplicationException($"No configuration provided for YARP instance \"{yarpBuilder.Resource.Name}\""); | ||
benjaminpetit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return Task.CompletedTask; | ||
| }); | ||
|
|
||
| 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this was suggested elsewhere, but would it make sense to be able to define the config in C#? That way someone doesn't have to jump out to a JSON file. But instead something like: builder.AddYarp("apigateway")
.WithReference(basketService)
.WithReference(catalogService)
.WithConfig(context =>
{
context.AddRoute(basketService, "{**catch-all}");
});There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea but not as a gate for this initial PR |
||
| { | ||
| builder.Resource.ConfigFilePath = configFilePath; | ||
| return builder; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.