Skip to content

Commit

Permalink
Skeleton app for containerization (#2710)
Browse files Browse the repository at this point in the history
* Skeleton app for containerization

* Add AddServiceDiscovery

* Fixes
  • Loading branch information
benjaminpetit authored Jan 10, 2025
1 parent f9f2236 commit 093ed77
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 0 deletions.
1 change: 1 addition & 0 deletions eng/Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<ProjectToBuild Include="$(RepoRoot)test\**\*.csproj" />
<ProjectToBuild Include="$(RepoRoot)samples\**\*.csproj" />
<ProjectToBuild Include="$(RepoRoot)testassets\**\*.csproj" />
<ProjectToBuild Include="$(RepoRoot)eng\yarpapppack\**\*.csproj" />
<ProjectToBuild Include="$(RepoRoot)docs\docfx\docfx.csproj" Condition="'$(OS)' == 'Windows_NT'" />
</ItemGroup>
</Project>
44 changes: 44 additions & 0 deletions eng/Publishing.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,48 @@
<PropertyGroup>
<PublishingVersion>3</PublishingVersion>
</PropertyGroup>

<PropertyGroup>
<_UploadPathRoot>reverse-proxy</_UploadPathRoot>
</PropertyGroup>

<PropertyGroup>
<PublishDependsOnTargets>$(PublishDependsOnTargets);_PublishBlobItems</PublishDependsOnTargets>

<!-- NOTE: This property is also defined on the root-level Directory.Build.props, but that file is not imported by the Publishing project.
Pulling it in here will cause different issues as that file will conflict with Arcade's publishing logic, so as a workaround we define it here.
If you are editing this property, make sure to also edit the one in Directory.Build.props. -->
<YarpAppArtifactsOutputDir>$([MSBuild]::NormalizeDirectory('$(ArtifactsDir)',
'YarpAppArtifacts', '$(Configuration)'))</YarpAppArtifactsOutputDir>
</PropertyGroup>

<ItemGroup>
<_YarpAppFilesToPublish Include="$(YarpAppArtifactsOutputDir)\**\*.zip" />
</ItemGroup>

<Target Name="_PublishBlobItems">
<!--
For blob items, we want to make sure that the version we get back is not stable, even when the repo
is producing stable versions.
This is because we want to be able to re-spin the build if necessary without hitting issues of blob
items clashing with each other. For this reason,
We will pass SuppressFinalPackageVersion as true when fetching the package version so that we get
back a version with a prerelease suffix.
-->
<MSBuild Projects="$(RepoRoot)src\Application\Yarp.Application.csproj"
Targets="ReturnPackageVersion"
SkipNonexistentProjects="false"
Properties="SuppressFinalPackageVersion=true">
<Output TaskParameter="TargetOutputs" PropertyName="_PackageVersion" />
</MSBuild>

<ItemGroup>
<ItemsToPushToBlobFeed Include="@(_YarpAppFilesToPublish)">
<IsShipping>true</IsShipping>
<PublishFlatContainer>true</PublishFlatContainer>
<RelativeBlobPath>$(_UploadPathRoot)/$(_PackageVersion)/%(Filename)%(Extension)</RelativeBlobPath>
</ItemsToPushToBlobFeed>
</ItemGroup>
</Target>

</Project>
5 changes: 5 additions & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<SystemCollectionsImmutableVersion>8.0.0</SystemCollectionsImmutableVersion>
<SystemIOHashingVersion>8.0.0</SystemIOHashingVersion>
<MicrosoftBclTimeProviderVersion>8.0.0</MicrosoftBclTimeProviderVersion>
<MicrosoftDotNetXUnitExtensionsPackageVersion>10.0.0-beta.24613.2</MicrosoftDotNetXUnitExtensionsPackageVersion>
<MicrosoftAspNetCoreTestHostVersion>6.0.36</MicrosoftAspNetCoreTestHostVersion>
<MicrosoftCrankEventSourcesVersion>0.2.0-alpha.24576.2</MicrosoftCrankEventSourcesVersion>
<MicrosoftDotNetXUnitExtensionsPackageVersion>10.0.0-beta.25056.1</MicrosoftDotNetXUnitExtensionsPackageVersion>
Expand All @@ -33,5 +34,9 @@
<DocfxConsoleVersion>2.59.4</DocfxConsoleVersion>
<YamlDotNetVersion>16.2.1</YamlDotNetVersion>
<KubernetesClientVersion>15.0.1</KubernetesClientVersion>
<!-- Container app dependencies -->
<YarpNugetVersion>2.2.0</YarpNugetVersion>
<MicrosoftExtensionsServiceDiscovery>8.2.2</MicrosoftExtensionsServiceDiscovery>
<MicrosoftExtensionsServiceDiscoveryYarp>8.2.2</MicrosoftExtensionsServiceDiscoveryYarp>
</PropertyGroup>
</Project>
43 changes: 43 additions & 0 deletions eng/yarpapppack/Common.projitems
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<Project>
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.props" />

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<IsPackable>true</IsPackable>
<SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IncludeBuildOutput>false</IncludeBuildOutput>
<PackageOutputPath Condition=" '$(PackageOutputPath)' == '' ">$(ArtifactsShippingPackagesDir)</PackageOutputPath>
<YarpAppArtifactsOutputDir>$([MSBuild]::NormalizeDirectory('$(ArtifactsDir)', 'YarpAppArtifacts', '$(Configuration)'))</YarpAppArtifactsOutputDir>

</PropertyGroup>

<PropertyGroup>
<PackageId>Yarp.Application.$(YarpAppRuntime)</PackageId>
<Description>Reverse proxy</Description>
</PropertyGroup>

<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.targets" />

<Target Name="Build" />

<Target Name="BeforeBuild" BeforeTargets="Build">
<MSBuild Projects="../../src/Application/Yarp.Application.csproj" Targets="publish" Properties="Configuration=$(Configuration);Platform=$(Platform);TargetFramework=$(TargetFramework);RuntimeIdentifier=$(YarpAppRuntime)" />

<!-- After publishing the project, we ensure that the published assets get packed in the nuspec. -->
<ItemGroup>
<_PublishItems Include="$(ArtifactsBinDir)/Yarp.Application/$(Configuration)/$(TargetFramework)/$(YarpAppRuntime)/publish/**/*" />
<None Include="@(_PublishItems)" Pack="true" PackagePath="tools/" />
</ItemGroup>

<MakeDir Directories="$(YarpAppArtifactsOutputDir)/$(YarpAppRuntime)" />
<ZipDirectory
SourceDirectory="$(ArtifactsBinDir)/Yarp.Application/$(Configuration)/$(TargetFramework)/$(YarpAppRuntime)/publish"
DestinationFile="$(YarpAppArtifactsOutputDir)/$(YarpAppRuntime)/reverse-proxy-$(YarpAppRuntime).zip"
Overwrite="true" />

<!-- Throw an error if _PublishItems is empty. -->
<Error Condition="'@(_PublishItems)' == ''" Text="No files were found to pack. Ensure that the project being packed has a publish target defined." />
</Target>

</Project>
10 changes: 10 additions & 0 deletions eng/yarpapppack/yarpapppack-linux-arm64.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project>

<PropertyGroup>
<YarpAppRuntime>linux-arm64</YarpAppRuntime>
<YarpAppPlatformType>Unix</YarpAppPlatformType>
</PropertyGroup>

<Import Project="Common.projitems" />

</Project>
10 changes: 10 additions & 0 deletions eng/yarpapppack/yarpapppack-linux-x64.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project>

<PropertyGroup>
<YarpAppRuntime>linux-x64</YarpAppRuntime>
<YarpAppPlatformType>Unix</YarpAppPlatformType>
</PropertyGroup>

<Import Project="Common.projitems" />

</Project>
11 changes: 11 additions & 0 deletions reverse-proxy.sln
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Prometheus", "Prometheus",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpLoadApp", "samples\Prometheus\HttpLoadApp\HttpLoadApp.csproj", "{BD73A038-8F3D-4BB2-A5C4-C8D077969DED}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yarp.Application", "src\Application\Yarp.Application.csproj", "{B63DDC4F-DA04-4B03-BF82-302FB3A954D7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -364,6 +366,14 @@ Global
{BD73A038-8F3D-4BB2-A5C4-C8D077969DED}.Release|Any CPU.Build.0 = Release|Any CPU
{BD73A038-8F3D-4BB2-A5C4-C8D077969DED}.Release|x64.ActiveCfg = Release|Any CPU
{BD73A038-8F3D-4BB2-A5C4-C8D077969DED}.Release|x64.Build.0 = Release|Any CPU
{B63DDC4F-DA04-4B03-BF82-302FB3A954D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B63DDC4F-DA04-4B03-BF82-302FB3A954D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B63DDC4F-DA04-4B03-BF82-302FB3A954D7}.Debug|x64.ActiveCfg = Debug|Any CPU
{B63DDC4F-DA04-4B03-BF82-302FB3A954D7}.Debug|x64.Build.0 = Debug|Any CPU
{B63DDC4F-DA04-4B03-BF82-302FB3A954D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -407,6 +417,7 @@ Global
{AC0EF892-7D32-4EAF-BE99-3696181E889F} = {149C61A2-D9F8-49B9-9F9B-3C953FEF53AA}
{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}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {31F6924A-E427-4830-96E9-B47CEB7BFE78}
Expand Down
36 changes: 36 additions & 0 deletions src/Application/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

// Load configuration
if (args.Length != 1)
{
Console.Error.WriteLine("Usage: yarp.exe <config_file>");
return 1;
}
var configFile = args[0];
var fileInfo = new FileInfo(configFile);
if (!fileInfo.Exists)
{
Console.Error.WriteLine($"Could not find '{configFile}'.");
return 2;
}

var builder = WebApplication.CreateBuilder();
builder.Configuration.AddJsonFile(fileInfo.FullName, optional: false, reloadOnChange: true);

// Configure YARP
builder.Services.AddServiceDiscovery();
builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"))
.AddServiceDiscoveryDestinationResolver();

Console.WriteLine(builder.Configuration.GetSection("ReverseProxy").Value);

var app = builder.Build();
app.MapReverseProxy();

await app.RunAsync();

return 0;
21 changes: 21 additions & 0 deletions src/Application/Yarp.Application.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<RuntimeIdentifiers>win-x64;win-arm64;linux-x64;linux-arm64;</RuntimeIdentifiers>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>yarp</AssemblyName>
</PropertyGroup>

<!-- Used by publishing infrastructure to get the version to use for blob publishing -->
<Target Name="ReturnPackageVersion" Returns="$(PackageVersion)" />

<ItemGroup>
<PackageReference Include="Yarp.ReverseProxy" Version="$(YarpNugetVersion)" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="$(MicrosoftExtensionsServiceDiscovery)" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery.Yarp" Version="$(MicrosoftExtensionsServiceDiscoveryYarp)" />
</ItemGroup>

</Project>

0 comments on commit 093ed77

Please sign in to comment.