Skip to content

Commit cf26db8

Browse files
committed
feat: update ApplicationHelper
1 parent f76cfa2 commit cf26db8

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/WeihanLi.Common/Helpers/ApplicationHelper.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private static RuntimeInfo GetRuntimeInfo()
137137
#else
138138
var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
139139
#endif
140-
return new RuntimeInfo()
140+
var runtimeInfo = new RuntimeInfo()
141141
{
142142
Version = Environment.Version.ToString(),
143143
ProcessorCount = Environment.ProcessorCount,
@@ -160,30 +160,41 @@ private static RuntimeInfo GetRuntimeInfo()
160160

161161
IsInContainer = IsInContainer(),
162162
IsInKubernetes = IsInKubernetesCluster(),
163+
KubernetesNamespace = GetKubernetesNamespace(),
163164

164165
LibraryVersion = libInfo.LibraryVersion,
165166
LibraryHash = libInfo.LibraryHash,
166167
RepositoryUrl = libInfo.RepositoryUrl,
167168
};
169+
return runtimeInfo;
168170
}
169171

170172
#region ContainerEnvironment
173+
// container environment
174+
// https://github.com/dotnet/dotnet-docker/blob/d90d458deada9057d7889f76d58fc0a7194a0c06/src/runtime-deps/6.0/alpine3.20/amd64/Dockerfile#L7
175+
176+
/// <summary>
177+
/// Whether running inside a container
178+
/// </summary>
171179
private static bool IsInContainer()
172180
{
173-
// https://github.com/dotnet/dotnet-docker/blob/9b731e901dd4a343fc30da7b8b3ab7d305a4aff9/src/runtime-deps/7.0/cbl-mariner2.0/amd64/Dockerfile#L18
174181
return "true".Equals(Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER"),
175182
StringComparison.OrdinalIgnoreCase);
176183
}
177184

185+
// Kubernetes environment
186+
// https://github.com/kubernetes-client/csharp/blob/36a02046439d01f1256aed4e5071cb7f1b57d6eb/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs#L41
178187
private static readonly string ServiceAccountPath =
179188
Path.Combine(
180189
[
181190
$"{Path.DirectorySeparatorChar}var", "run", "secrets", "kubernetes.io", "serviceaccount",
182191
]);
183192
private const string ServiceAccountTokenKeyFileName = "token";
184193
private const string ServiceAccountRootCAKeyFileName = "ca.crt";
194+
private const string ServiceAccountNamespaceFileName = "namespace";
195+
185196
/// <summary>
186-
/// Whether running in k8s cluster
197+
/// Whether running inside a k8s cluster
187198
/// </summary>
188199
/// <returns></returns>
189200
private static bool IsInKubernetesCluster()
@@ -204,8 +215,17 @@ private static bool IsInKubernetesCluster()
204215
var certPath = Path.Combine(ServiceAccountPath, ServiceAccountRootCAKeyFileName);
205216
return File.Exists(certPath);
206217
}
207-
#endregion ContainerEnvironment
208218

219+
/// <summary>
220+
/// Get Kubernetes namespace
221+
/// </summary>
222+
/// <returns>The namespace current workload in</returns>
223+
private static string? GetKubernetesNamespace()
224+
{
225+
var namespaceFilePath = Path.Combine(ServiceAccountPath, ServiceAccountNamespaceFileName);
226+
return File.Exists(namespaceFilePath) ? File.ReadAllText(namespaceFilePath).Trim() : null;
227+
}
228+
#endregion ContainerEnvironment
209229
}
210230

211231
public class LibraryInfo
@@ -243,4 +263,9 @@ public sealed class RuntimeInfo : LibraryInfo
243263
/// Is running in a Kubernetes cluster
244264
/// </summary>
245265
public required bool IsInKubernetes { get; init; }
266+
267+
/// <summary>
268+
/// Kubernetes namespace when running in a Kubernetes cluster
269+
/// </summary>
270+
public string? KubernetesNamespace { get; init; }
246271
}

0 commit comments

Comments
 (0)