From c43a2823c9a0781e05421863c30e0db0e2b27397 Mon Sep 17 00:00:00 2001 From: Yuuki Wesp Date: Fri, 15 Nov 2024 03:02:44 +0300 Subject: [PATCH 1/2] add environment determinism (kube or classic machine) --- Argon.Server.sln.DotSettings | 1 + src/Argon.Api/Argon.Api.csproj | 2 +- .../Features/Env/EnvironmentExtensions.cs | 35 +++++++++++++++++++ .../Features/Orleanse/BalanceRule.cs | 5 +-- .../Features/Orleanse/OrleansExtension.cs | 7 ++-- src/Argon.Api/Program.cs | 5 +-- 6 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 src/Argon.Api/Features/Env/EnvironmentExtensions.cs diff --git a/Argon.Server.sln.DotSettings b/Argon.Server.sln.DotSettings index af424ce..8229e12 100644 --- a/Argon.Server.sln.DotSettings +++ b/Argon.Server.sln.DotSettings @@ -86,6 +86,7 @@ True True True + True True True True diff --git a/src/Argon.Api/Argon.Api.csproj b/src/Argon.Api/Argon.Api.csproj index aa71854..3fcc6d1 100644 --- a/src/Argon.Api/Argon.Api.csproj +++ b/src/Argon.Api/Argon.Api.csproj @@ -1,4 +1,4 @@ - + net8.0 diff --git a/src/Argon.Api/Features/Env/EnvironmentExtensions.cs b/src/Argon.Api/Features/Env/EnvironmentExtensions.cs new file mode 100644 index 0000000..3c398c8 --- /dev/null +++ b/src/Argon.Api/Features/Env/EnvironmentExtensions.cs @@ -0,0 +1,35 @@ +namespace Argon.Api.Features.Env; + +using static File; + +public static class EnvironmentExtensions +{ + public static bool IsKube(this IHostEnvironment env) + => env.Determine() == ArgonEnvironmentKind.Kubernetes; + + public static bool IsDocker(this IHostEnvironment env) + => env.Determine() == ArgonEnvironmentKind.Docker; + + public static bool IsClassicHost(this IHostEnvironment env) + => env.Determine() == ArgonEnvironmentKind.HostMachine; + + public static bool IsManaged(this IHostEnvironment env) + => env.IsDocker() || env.IsKube(); + + public static ArgonEnvironmentKind Determine(this IHostEnvironment _) + { + if (Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_HOST") != null) + return ArgonEnvironmentKind.Kubernetes; + if (Exists("/.dockerenv") || Directory.Exists("/proc/self/cgroup") && + ReadAllText("/proc/self/cgroup").Contains("docker")) + return ArgonEnvironmentKind.Docker; + return ArgonEnvironmentKind.HostMachine; + } +} + +public enum ArgonEnvironmentKind +{ + HostMachine, + Docker, + Kubernetes, +} \ No newline at end of file diff --git a/src/Argon.Api/Features/Orleanse/BalanceRule.cs b/src/Argon.Api/Features/Orleanse/BalanceRule.cs index 0294804..8936d26 100644 --- a/src/Argon.Api/Features/Orleanse/BalanceRule.cs +++ b/src/Argon.Api/Features/Orleanse/BalanceRule.cs @@ -2,6 +2,7 @@ namespace Argon.Api.Features; using System.Collections.Concurrent; using System.Globalization; +using Env; using k8s; using Orleans.Placement.Repartitioning; using static Math; @@ -13,7 +14,7 @@ public static IServiceCollection AddKubeResources(this WebApplicationBuilder bui { var services = builder.Services; - if (builder.Environment.IsProduction()) + if (builder.Environment.IsKube()) { var config = KubernetesClientConfiguration.InClusterConfig(); services.AddSingleton(config); @@ -49,7 +50,7 @@ public async ValueTask FetchAsync() private async Task GetAvgCpu() { - if (!env.IsProduction()) + if (!env.IsManaged()) return 10; await using var scope = serviceProvider.CreateAsyncScope(); diff --git a/src/Argon.Api/Features/Orleanse/OrleansExtension.cs b/src/Argon.Api/Features/Orleanse/OrleansExtension.cs index 23b189d..9552e65 100644 --- a/src/Argon.Api/Features/Orleanse/OrleansExtension.cs +++ b/src/Argon.Api/Features/Orleanse/OrleansExtension.cs @@ -1,6 +1,7 @@ namespace Argon.Api.Features; using Contracts; +using Env; using Orleans.Clustering.Kubernetes; using Orleans.Configuration; using Orleans.Serialization; @@ -39,10 +40,10 @@ public static WebApplicationBuilder AddOrleans(this WebApplicationBuilder builde #pragma warning restore ORLEANSEXP001 .AddMemoryGrainStorage("CacheStorage") .UseDashboard(o => o.Port = 22832); - if (builder.Environment.IsDevelopment()) - siloBuilder.UseLocalhostClustering(); - else + if (builder.Environment.IsKube()) siloBuilder.UseKubeMembership(); + else + siloBuilder.UseLocalhostClustering(); }); return builder; diff --git a/src/Argon.Api/Program.cs b/src/Argon.Api/Program.cs index d26098b..a14169b 100644 --- a/src/Argon.Api/Program.cs +++ b/src/Argon.Api/Program.cs @@ -6,6 +6,7 @@ using Argon.Api.Extensions; using Argon.Api.Features; using Argon.Api.Features.EmailForms; +using Argon.Api.Features.Env; using Argon.Api.Features.Jwt; using Argon.Api.Features.Otp; using Argon.Api.Grains.Interfaces; @@ -24,7 +25,7 @@ builder.AddRabbitMQClient("rmq"); builder.AddNpgsqlDbContext("DefaultConnection"); builder.Services.AddSingleton(); -if (!builder.Environment.IsProduction()) +if (!builder.Environment.IsManaged()) { builder.AddJwt(); builder.Services.AddControllers().AddNewtonsoftJson(); @@ -47,7 +48,7 @@ builder.Services.AddAutoMapper(typeof(User).Assembly); // TODO var app = builder.Build(); -if (!builder.Environment.IsProduction()) +if (!builder.Environment.IsManaged()) { app.UseWebSockets(); app.MapRpcWebSocketServer(); From 25398cefc17cba2d687a253beadf9e1005a147c4 Mon Sep 17 00:00:00 2001 From: Yuuki Wesp Date: Fri, 15 Nov 2024 03:04:14 +0300 Subject: [PATCH 2/2] fix --- src/Argon.Api/Features/Orleanse/OrleansExtension.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Argon.Api/Features/Orleanse/OrleansExtension.cs b/src/Argon.Api/Features/Orleanse/OrleansExtension.cs index 997d1ec..d46caed 100644 --- a/src/Argon.Api/Features/Orleanse/OrleansExtension.cs +++ b/src/Argon.Api/Features/Orleanse/OrleansExtension.cs @@ -1,6 +1,7 @@ namespace Argon.Api.Features; using Contracts; +using Env; using Extensions; using Orleans.Clustering.Kubernetes; using Orleans.Configuration; @@ -32,7 +33,9 @@ public static WebApplicationBuilder AddOrleans(this WebApplicationBuilder builde .AddPersistentStreams(IArgonEvent.ProviderId, NatsAdapterFactory.Create, options => { }).UseDashboard(o => o.Port = 22832); #pragma warning restore ORLEANSEXP001 - if (builder.Environment.IsDevelopment()) + if (builder.Environment.IsKube()) + siloBuilder.UseKubeMembership(); + else siloBuilder.UseLocalhostClustering(); });