forked from asynkron/protoactor-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for an Advertised Host other than PodIP when using Kubern…
…etes cluster provider (asynkron#2097) * Add Host to k8s pod labels * Removed logging of k8s workload kind * Added new Public API to get K8s Pod FQDN hardened GetKubeNamespace() so it will not read in more data then kuberneties allows for a namespace, it will also now throw a with a descriptive error exception message vs FileNotFound.
- Loading branch information
1 parent
824c0cc
commit af1a612
Showing
27 changed files
with
796 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Messages", "Messages\Messages.csproj", "{A5E3B3E1-5517-4E08-9E50-50EF17215AB5}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Node2", "Node2\Node2.csproj", "{95685292-A5C9-48DA-871A-EB00A78A39A5}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Node1", "Node1\Node1.csproj", "{B8D93ECE-0203-41C2-B845-7BBDDBA222DE}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{A5E3B3E1-5517-4E08-9E50-50EF17215AB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{A5E3B3E1-5517-4E08-9E50-50EF17215AB5}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{A5E3B3E1-5517-4E08-9E50-50EF17215AB5}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{A5E3B3E1-5517-4E08-9E50-50EF17215AB5}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{95685292-A5C9-48DA-871A-EB00A78A39A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{95685292-A5C9-48DA-871A-EB00A78A39A5}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{95685292-A5C9-48DA-871A-EB00A78A39A5}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{95685292-A5C9-48DA-871A-EB00A78A39A5}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{B8D93ECE-0203-41C2-B845-7BBDDBA222DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{B8D93ECE-0203-41C2-B845-7BBDDBA222DE}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{B8D93ECE-0203-41C2-B845-7BBDDBA222DE}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{B8D93ECE-0203-41C2-B845-7BBDDBA222DE}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<LangVersion>11</LangVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Google.Protobuf" Version="3.22.0" /> | ||
<PackageReference Include="Grpc.Tools" Version="2.51.0" PrivateAssets="All" /> | ||
<PackageReference Include="Proto.Cluster.CodeGen" Version="1.0.0-rc3.17" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Protobuf Include="Protos.proto" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProtoGrain Include="Protos.proto" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Proto.Cluster\Proto.Cluster.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
syntax = "proto3"; | ||
package HelloHelloWorld; | ||
option csharp_namespace = "ClusterHelloWorld.Messages"; | ||
|
||
message HelloRequest {} | ||
message HelloResponse { | ||
string Message=1; | ||
} | ||
|
||
service HelloGrain { | ||
rpc SayHello(HelloRequest) returns (HelloResponse) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base | ||
WORKDIR /app | ||
|
||
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build | ||
ARG BUILD_CONFIGURATION=Release | ||
WORKDIR /src | ||
COPY ["examples/ClusterK8sGrains/Node1/Node1.csproj", "examples/ClusterK8sGrains/Node1/"] | ||
COPY ["examples/ClusterK8sGrains/Node2/Node2.csproj", "examples/ClusterK8sGrains/Node2/"] | ||
COPY ["src/Proto.Cluster.Kubernetes/Proto.Cluster.Kubernetes.csproj", "src/Proto.Cluster.Kubernetes/"] | ||
COPY ["src/Proto.Cluster/Proto.Cluster.csproj", "src/Proto.Cluster/"] | ||
COPY ["src/Proto.Remote/Proto.Remote.csproj", "src/Proto.Remote/"] | ||
COPY ["src/Proto.Actor/Proto.Actor.csproj", "src/Proto.Actor/"] | ||
COPY ["examples/ClusterK8sGrains/Messages/Messages.csproj", "examples/ClusterK8sGrains/Messages/"] | ||
RUN dotnet restore "examples/ClusterK8sGrains/Node1/Node1.csproj" | ||
RUN dotnet restore "examples/ClusterK8sGrains/Node2/Node2.csproj" | ||
COPY . . | ||
WORKDIR "/src/examples/ClusterK8sGrains/Node1" | ||
RUN dotnet build "Node1.csproj" -c $BUILD_CONFIGURATION -o /app/build | ||
|
||
FROM build AS publish | ||
ARG BUILD_CONFIGURATION=Release | ||
RUN dotnet publish "Node1.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false --no-restore | ||
|
||
FROM base AS final | ||
WORKDIR /app | ||
COPY --from=publish /app/publish . | ||
ENTRYPOINT ["dotnet", "Node1.dll"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ServerGarbageCollection>true</ServerGarbageCollection> | ||
<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection> | ||
<LangVersion>10</LangVersion> | ||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> | ||
<Company>proto.actor example</Company> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Proto.Cluster.Kubernetes\Proto.Cluster.Kubernetes.csproj" /> | ||
<ProjectReference Include="..\..\..\src\Proto.Cluster\Proto.Cluster.csproj"/> | ||
<ProjectReference Include="..\..\..\src\Proto.Remote\Proto.Remote.csproj"/> | ||
<ProjectReference Include="..\Messages\Messages.csproj"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Content Include="..\..\..\.dockerignore"> | ||
<Link>.dockerignore</Link> | ||
</Content> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="Program.cs" company="Asynkron AB"> | ||
// Copyright (C) 2015-2022 Asynkron AB All rights reserved | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using ClusterHelloWorld.Messages; | ||
using Microsoft.Extensions.Logging; | ||
using Proto; | ||
using Proto.Cluster; | ||
using Proto.Cluster.Kubernetes; | ||
using Proto.Cluster.PartitionActivator; | ||
using Proto.Remote; | ||
using Proto.Remote.GrpcNet; | ||
using static Proto.CancellationTokens; | ||
using ProtosReflection = ClusterHelloWorld.Messages.ProtosReflection; | ||
using System.Runtime.Loader; | ||
using Microsoft.Extensions.Configuration; | ||
using Extensions = Proto.Remote.GrpcNet.Extensions; | ||
|
||
// Hook SIGTERM to a cancel token to know when k8s is shutting us down | ||
// hostBuilder should be used in production | ||
var cts = new CancellationTokenSource(); | ||
AssemblyLoadContext.Default.Unloading += ctx => cts.Cancel(); | ||
|
||
Log.SetLoggerFactory( | ||
LoggerFactory.Create(l => l.AddConsole(options => | ||
{ | ||
//options.FormatterName = "json"; // Use the JSON formatter | ||
}).SetMinimumLevel(LogLevel.Debug) | ||
.AddFilter("Proto.Cluster.Gossip", LogLevel.Information) | ||
.AddFilter("Proto.Context.ActorContext", LogLevel.Information))); | ||
|
||
// Required to allow unencrypted GrpcNet connections | ||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); | ||
|
||
var kubernetesProvider = new KubernetesProvider(); | ||
var advertisedHost = await kubernetesProvider.GetPodFqdn(); | ||
|
||
var system = new ActorSystem() | ||
.WithRemote(GrpcNetRemoteConfig | ||
.BindToAllInterfaces(advertisedHost: advertisedHost, port: 4020) | ||
.WithProtoMessages(ProtosReflection.Descriptor)) | ||
.WithCluster(ClusterConfig | ||
.Setup("MyCluster", | ||
kubernetesProvider, | ||
new PartitionActivatorLookup()) | ||
); | ||
|
||
system.EventStream.Subscribe<ClusterTopology>( | ||
e => { Console.WriteLine($"{DateTime.Now:O} My members {e.TopologyHash}"); } | ||
); | ||
|
||
await system | ||
.Cluster() | ||
.StartMemberAsync(); | ||
|
||
Console.WriteLine("Started"); | ||
|
||
try | ||
{ | ||
var helloGrain = system.Cluster().GetHelloGrain("MyGrain"); | ||
|
||
var res = await helloGrain.SayHello(new HelloRequest(), FromSeconds(15)); | ||
Console.WriteLine(res?.Message ?? "RES IS NULL"); | ||
|
||
res = await helloGrain.SayHello(new HelloRequest(), FromSeconds(5)); | ||
Console.WriteLine(res?.Message ?? "RES IS NULL"); | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.CreateLogger("Program").LogError(e, "Error sending messages"); | ||
} | ||
|
||
Console.WriteLine("Press CTRL-C to exit"); | ||
Console.CancelKeyPress += (_, e) => | ||
{ | ||
e.Cancel = true; // prevent the process from terminating. | ||
cts.Cancel(); | ||
}; | ||
|
||
await Task.Delay(Timeout.Infinite, cts.Token); | ||
|
||
Console.WriteLine("Shutting Down..."); | ||
await system.Cluster().ShutdownAsync(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base | ||
WORKDIR /app | ||
|
||
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build | ||
ARG BUILD_CONFIGURATION=Release | ||
WORKDIR /src | ||
COPY ["examples/ClusterK8sGrains/Node1/Node1.csproj", "examples/ClusterK8sGrains/Node1/"] | ||
COPY ["examples/ClusterK8sGrains/Node2/Node2.csproj", "examples/ClusterK8sGrains/Node2/"] | ||
COPY ["src/Proto.Cluster.Kubernetes/Proto.Cluster.Kubernetes.csproj", "src/Proto.Cluster.Kubernetes/"] | ||
COPY ["src/Proto.Cluster/Proto.Cluster.csproj", "src/Proto.Cluster/"] | ||
COPY ["src/Proto.Remote/Proto.Remote.csproj", "src/Proto.Remote/"] | ||
COPY ["src/Proto.Actor/Proto.Actor.csproj", "src/Proto.Actor/"] | ||
COPY ["examples/ClusterK8sGrains/Messages/Messages.csproj", "examples/ClusterK8sGrains/Messages/"] | ||
RUN dotnet restore "examples/ClusterK8sGrains/Node1/Node1.csproj" | ||
RUN dotnet restore "examples/ClusterK8sGrains/Node2/Node2.csproj" | ||
COPY . . | ||
WORKDIR "/src/examples/ClusterK8sGrains/Node2" | ||
RUN dotnet build "Node2.csproj" -c $BUILD_CONFIGURATION -o /app/build | ||
|
||
FROM build AS publish | ||
ARG BUILD_CONFIGURATION=Release | ||
RUN dotnet publish "Node2.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false --no-restore | ||
|
||
FROM base AS final | ||
WORKDIR /app | ||
COPY --from=publish /app/publish . | ||
ENTRYPOINT ["dotnet", "Node2.dll"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ServerGarbageCollection>true</ServerGarbageCollection> | ||
<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection> | ||
<LangVersion>11</LangVersion> | ||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> | ||
<Company>proto.actor example</Company> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Proto.Cluster.Kubernetes\Proto.Cluster.Kubernetes.csproj" /> | ||
<ProjectReference Include="..\..\..\src\Proto.Cluster\Proto.Cluster.csproj"/> | ||
<ProjectReference Include="..\..\..\src\Proto.Remote\Proto.Remote.csproj"/> | ||
<ProjectReference Include="..\Messages\Messages.csproj"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Content Include="..\..\..\.dockerignore"> | ||
<Link>.dockerignore</Link> | ||
</Content> | ||
</ItemGroup> | ||
</Project> |
Oops, something went wrong.