-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feature/environment #43
Changes from all commits
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
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. ?????????? |
||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||||||||||||
namespace Argon.Api.Features.Env; | ||||||||||||||||
|
||||||||||||||||
using static File; | ||||||||||||||||
|
||||||||||||||||
Comment on lines
+1
to
+4
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. Add missing imports The code is using Add these imports: namespace Argon.Api.Features.Env;
+ using Microsoft.Extensions.Hosting;
- using static File;
+ using static System.IO.File; 📝 Committable suggestion
Suggested change
|
||||||||||||||||
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; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
Comment on lines
+19
to
+28
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. 🛠️ Refactor suggestion Consider improving environment detection robustness and performance The current implementation has several areas for improvement:
Consider this improved implementation: - public static ArgonEnvironmentKind Determine(this IHostEnvironment _)
+ public static ArgonEnvironmentKind Determine(this IHostEnvironment env)
+ {
+ // Cache the result in a static field
+ private static ArgonEnvironmentKind? _cachedKind;
+
+ if (_cachedKind.HasValue)
+ return _cachedKind.Value;
+
+ _cachedKind = DetermineInternal();
+ return _cachedKind.Value;
+ }
+
+ private static ArgonEnvironmentKind DetermineInternal()
{
if (Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_HOST") != null)
return ArgonEnvironmentKind.Kubernetes;
- if (Exists("/.dockerenv") || Directory.Exists("/proc/self/cgroup") &&
- ReadAllText("/proc/self/cgroup").Contains("docker"))
+ try
+ {
+ // Check multiple indicators for Docker
+ if (Exists("/.dockerenv"))
+ return ArgonEnvironmentKind.Docker;
+
+ if (Directory.Exists("/proc/self/cgroup"))
+ {
+ var cgroupContent = ReadAllText("/proc/self/cgroup");
+ if (cgroupContent.Contains("docker") || cgroupContent.Contains("containerd"))
+ return ArgonEnvironmentKind.Docker;
+ }
+ }
+ catch (IOException)
+ {
+ // Silently fall through to default if file operations fail
+ }
return ArgonEnvironmentKind.HostMachine;
}
|
||||||||||||||||
|
||||||||||||||||
public enum ArgonEnvironmentKind | ||||||||||||||||
{ | ||||||||||||||||
HostMachine, | ||||||||||||||||
Docker, | ||||||||||||||||
Kubernetes, | ||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
???