Skip to content
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

Simplify Library Structure for Kubernetes #1642

Merged
merged 2 commits into from
May 9, 2022
Merged
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
149 changes: 22 additions & 127 deletions reverse-proxy.sln

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,25 @@ FROM mcr.microsoft.com/dotnet/sdk:6.0.100 AS publish
WORKDIR /src

# Copy csproj files and other files needed for restoring (to build a nuget cache layer to speed up rebuilds)
COPY ["src/OperatorFramework/src/Controller/Microsoft.Kubernetes.Controller.csproj", "src/OperatorFramework/src/Controller/"]
COPY ["src/OperatorFramework/src/Core/Microsoft.Kubernetes.Core.csproj", "src/OperatorFramework/src/Core/"]
COPY ["src/Kubernetes.Controller/Yarp.Kubernetes.Controller.csproj", "src/Kubernetes.Controller/"]
COPY ["src/Kubernetes.Protocol/Yarp.Kubernetes.Protocol.csproj", "src/Kubernetes.Protocol/"]
COPY ["samples/KuberenetesIngress.Sample/Ingress/Yarp.Kubernetes.Ingress.csproj", "samples/KuberenetesIngress.Sample/Ingress/"]
COPY ["src/ReverseProxy/Yarp.ReverseProxy.csproj", "src/ReverseProxy/"]
COPY ["src/Kubernetes.Controller/Yarp.Kubernetes.Controller.csproj", "src/Kubernetes.Controller/"]
COPY ["src/Directory.Build.props", "src/"]
COPY ["Directory.Build.*", "./"]
COPY ["global.json", ""]
COPY ["NuGet.config", ""]

# Build a cache layer with all of the nuget packages
RUN dotnet restore src/Kubernetes.Controller/Yarp.Kubernetes.Controller.csproj
RUN dotnet restore samples/KuberenetesIngress.Sample/Ingress/Yarp.Kubernetes.Ingress.csproj

# Copy the remaining source files
WORKDIR /src
COPY . .

WORKDIR /src/src/Kubernetes.Controller
WORKDIR /src/samples/KuberenetesIngress.Sample/Ingress/
RUN dotnet publish -c Release --no-restore -o /app/publish -f net6.0

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Yarp.Kubernetes.Controller.dll"]
ENTRYPOINT ["dotnet", "Yarp.Kubernetes.Ingress.dll"]
42 changes: 42 additions & 0 deletions samples/KuberenetesIngress.Sample/Combined/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Rest;
using Serilog;
using Serilog.Sinks.SystemConsole.Themes;

namespace Yarp.Kubernetes.Ingress
{
public static class Program
{
public static void Main(string[] args)
{
using var serilog = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.Console(theme: AnsiConsoleTheme.Code)
.CreateLogger();

ServiceClientTracing.IsEnabled = true;

Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(config =>
{
config.AddJsonFile("/app/config/yarp.json", optional: true);
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddSerilog(serilog, dispose: false);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
}).Build().Run();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
{
"profiles": {
"Yarp.Kubernetes.Controller": {
"Ingress": {
"commandName": "Project",
"launchUrl": "api/dispatch",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5011;http://localhost:5010"
"applicationUrl": "https://localhost:5021;http://localhost:5020"
},
"Docker": {
"commandName": "Docker",
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/api/dispatch",
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"publishAllPorts": true,
"useSSL": true
}
Expand Down
21 changes: 21 additions & 0 deletions samples/KuberenetesIngress.Sample/Combined/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Yarp Ingress Controller

This directory contains a sample ingress as well as the definition for the Kubernetes manifests for the ingress controller.

The sample ingress controller is a single deployable.

## Building the Docker Image

From the base directory for this repo (where the .sln file is), run the command:

```
docker build -t yarp-combined:latest -f .\samples\KuberenetesIngress.Sample\Combined\Dockerfile .
```

## Deploying the Sample Ingress Controller

1. Open the [ingress-controller.yaml](./ingress-controller.yaml) file
2. Modify the container image to match the name used when building the image, e.g. change `<REGISTRY_NAME>/yarp-combined:<TAG>` to `yarp-combined:latest`
3. From the root of this repo. run the command `kubectl apply -f .\samples\KuberenetesIngress.Sample\Combined\ingress-controller.yaml`

To undeploy the ingress controller, run the command `kubectl delete -f .\samples\KuberenetesIngress.Sample\Combined\ingress-controller.yaml`
46 changes: 46 additions & 0 deletions samples/KuberenetesIngress.Sample/Combined/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Yarp.Kubernetes.Ingress
{
public class Startup
{
private readonly IConfiguration _configuration;

public Startup(IConfiguration configuration)
{
_configuration = configuration;
}

// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddKubernetesReverseProxy(_configuration);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
#pragma warning disable CA1822 // Mark members as static
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
#pragma warning restore CA1822 // Mark members as static
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapReverseProxy();
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<UserSecretsId>78d1f3b4-abce-4c5a-b914-3321fab1f8d0</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<IsPackable>$('System.TeamProject') != 'internal'</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nerdbank.GitVersioning" Version="3.3.37" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
</ItemGroup>

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

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
}
11 changes: 11 additions & 0 deletions samples/KuberenetesIngress.Sample/Combined/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug"
}
},
"AllowedHosts": "*",
"Yarp": {
"ControllerClass": "microsoft.com/ingress-yarp"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,28 @@ annotations:
apiVersion: v1
kind: Service
metadata:
name: yarp-controller
name: ingress-yarp-controller
namespace: yarp
spec:
ports:
- name: api
port: 8000
- name: proxy
port: 80
protocol: TCP
targetPort: 8000
- name: proxy-ssl
port: 443
protocol: TCP
targetPort: 8443
selector:
app: ingress-yarp-controller
type: ClusterIP
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: ingress-yarp-controller
name: yarp-controller
name: ingress-yarp
namespace: yarp
spec:
replicas: 1
Expand All @@ -137,10 +141,13 @@ spec:
containers:
- name: yarp-controller
imagePullPolicy: IfNotPresent
image: <REGISTRY_NAME>/yarp-controller:<TAG>
image: <REGISTRY_NAME>/yarp-combined:<TAG>
ports:
- containerPort: 8000
name: api
name: proxy
protocol: TCP
- containerPort: 8443
name: proxy-ssl
protocol: TCP
env:
- name: ASPNETCORE_URLS
Expand Down
4 changes: 1 addition & 3 deletions samples/KuberenetesIngress.Sample/Ingress/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ WORKDIR /src

# Copy csproj files and other files needed for restoring (to build a nuget cache layer to speed up rebuilds)
COPY ["samples/KuberenetesIngress.Sample/Ingress/Yarp.Kubernetes.Ingress.csproj", "samples/KuberenetesIngress.Sample/Ingress/"]
COPY ["src/OperatorFramework/src/Controller/Microsoft.Kubernetes.Controller.csproj", "src/OperatorFramework/src/Controller/"]
COPY ["src/OperatorFramework/src/Core/Microsoft.Kubernetes.Core.csproj", "src/OperatorFramework/src/Core/"]
COPY ["src/ReverseProxy/Yarp.ReverseProxy.csproj", "src/ReverseProxy/"]
COPY ["src/Kubernetes.Protocol/Yarp.Kubernetes.Protocol.csproj", "src/Kubernetes.Protocol/"]
COPY ["src/Kubernetes.Controller/Yarp.Kubernetes.Controller.csproj", "src/Kubernetes.Controller/"]
COPY ["src/Directory.Build.props", "src/"]
COPY ["Directory.Build.*", "./"]
COPY ["global.json", ""]
Expand Down
34 changes: 32 additions & 2 deletions samples/KuberenetesIngress.Sample/Ingress/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
# Yarp Ingress and Ingress Controller
# Yarp Ingress Controller

This directory contains a sample ingress as well as the definition for the Kubernetes manifests for the ingress and ingress controller.
This directory contains a sample ingress as well as the definition for the Kubernetes manifests for the ingress controller.

This sample requires two applications to be deployed:
* An Ingress (this application)
* A Kubernetes Ingress Monitor (a process listening for changes in k8s and dispatching the Yarp configuration to ingress instances)

NOTE: Yarp Kubernetes can also be configured as a combined (single) deployable. See the combined [README.md](../Combined/README.md) for more information.

## Building the Docker Images

From the base directory for this repo (where the .sln file is), run the commands:

```
docker build -t yarp-monitor:latest -f .\samples\KuberenetesIngress.Sample\Monitor\Dockerfile .
docker build -t yarp-ingress:latest -f .\samples\KuberenetesIngress.Sample\Ingress\Dockerfile .
```

## Deploying the Sample Ingress Controller

1. Open the [ingress-monitor.yaml](../Monitor/ingress-monitor.yaml) file
1. Modify the container image to match the name used when building the image, e.g. change `<REGISTRY_NAME>/yarp-monitor:<TAG>` to `yarp-monitor:latest`
1. Run the command `kubectl apply -f .\samples\KuberenetesIngress.Sample\Monitor\ingress-monitor.yaml`
1. Open the [ingress.yaml](./ingress.yaml) file
1. Modify the container image to match the name used when building the image, e.g. change `<REGISTRY_NAME>/yarp-ingress:<TAG>` to `yarp-ingress:latest`
1. Run the command `kubectl apply -f .\samples\KuberenetesIngress.Sample\Ingress\ingress.yaml`

To undeploy the ingress, run the commands
```
kubectl delete -f .\samples\KuberenetesIngress.Sample\Ingress\ingress.yaml
kubectl delete -f .\samples\KuberenetesIngress.Sample\Monitor\ingress-monitor.yaml
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\OperatorFramework\src\Controller\Microsoft.Kubernetes.Controller.csproj" />
<ProjectReference Include="..\..\..\src\Kubernetes.Protocol\Yarp.Kubernetes.Protocol.csproj" />
<ProjectReference Include="..\..\..\src\ReverseProxy\Yarp.ReverseProxy.csproj" />
<ProjectReference Include="..\..\..\src\Kubernetes.Controller\Yarp.Kubernetes.Controller.csproj" />
</ItemGroup>

</Project>
9 changes: 1 addition & 8 deletions samples/KuberenetesIngress.Sample/Ingress/ingress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,15 @@ spec:
containers:
- name: yarp-proxy
imagePullPolicy: IfNotPresent
image: <REGISTRY_NAME>/yarp:<TAG>
image: <REGISTRY_NAME>/yarp-ingress:<TAG>
ports:
- containerPort: 8000
name: proxy
protocol: TCP
- containerPort: 8443
name: proxy-ssl
protocol: TCP
volumeMounts:
- mountPath: /config
name: yarp-config
env:
- name: ASPNETCORE_URLS
value: http://*:8000
volumes:
- configMap:
name: yarp-config
name: yarp-config
---
33 changes: 33 additions & 0 deletions samples/KuberenetesIngress.Sample/Monitor/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0.100 AS publish
WORKDIR /src

# Copy csproj files and other files needed for restoring (to build a nuget cache layer to speed up rebuilds)
COPY ["samples/KuberenetesIngress.Sample/Monitor/Yarp.Kubernetes.Monitor.csproj", "samples/KuberenetesIngress.Sample/Monitor/"]
COPY ["src/ReverseProxy/Yarp.ReverseProxy.csproj", "src/ReverseProxy/"]
COPY ["src/Kubernetes.Controller/Yarp.Kubernetes.Controller.csproj", "src/Kubernetes.Controller/"]
COPY ["src/Directory.Build.props", "src/"]
COPY ["Directory.Build.*", "./"]
COPY ["global.json", ""]
COPY ["NuGet.config", ""]

# Build a cache layer with all of the nuget packages
RUN dotnet restore samples/KuberenetesIngress.Sample/Monitor/Yarp.Kubernetes.Monitor.csproj

# Copy the remaining source files
WORKDIR /src
COPY . .

WORKDIR /src/samples/KuberenetesIngress.Sample/Monitor/
RUN dotnet publish -c Release --no-restore -o /app/publish -f net6.0

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Yarp.Kubernetes.Monitor.dll"]
Loading