@@ -137,7 +137,7 @@ private static RuntimeInfo GetRuntimeInfo()
137
137
#else
138
138
var currentProcess = System . Diagnostics . Process . GetCurrentProcess ( ) ;
139
139
#endif
140
- return new RuntimeInfo ( )
140
+ var runtimeInfo = new RuntimeInfo ( )
141
141
{
142
142
Version = Environment . Version . ToString ( ) ,
143
143
ProcessorCount = Environment . ProcessorCount ,
@@ -160,30 +160,41 @@ private static RuntimeInfo GetRuntimeInfo()
160
160
161
161
IsInContainer = IsInContainer ( ) ,
162
162
IsInKubernetes = IsInKubernetesCluster ( ) ,
163
+ KubernetesNamespace = GetKubernetesNamespace ( ) ,
163
164
164
165
LibraryVersion = libInfo . LibraryVersion ,
165
166
LibraryHash = libInfo . LibraryHash ,
166
167
RepositoryUrl = libInfo . RepositoryUrl ,
167
168
} ;
169
+ return runtimeInfo ;
168
170
}
169
171
170
172
#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>
171
179
private static bool IsInContainer ( )
172
180
{
173
- // https://github.com/dotnet/dotnet-docker/blob/9b731e901dd4a343fc30da7b8b3ab7d305a4aff9/src/runtime-deps/7.0/cbl-mariner2.0/amd64/Dockerfile#L18
174
181
return "true" . Equals ( Environment . GetEnvironmentVariable ( "DOTNET_RUNNING_IN_CONTAINER" ) ,
175
182
StringComparison . OrdinalIgnoreCase ) ;
176
183
}
177
184
185
+ // Kubernetes environment
186
+ // https://github.com/kubernetes-client/csharp/blob/36a02046439d01f1256aed4e5071cb7f1b57d6eb/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs#L41
178
187
private static readonly string ServiceAccountPath =
179
188
Path . Combine (
180
189
[
181
190
$ "{ Path . DirectorySeparatorChar } var", "run" , "secrets" , "kubernetes.io" , "serviceaccount" ,
182
191
] ) ;
183
192
private const string ServiceAccountTokenKeyFileName = "token" ;
184
193
private const string ServiceAccountRootCAKeyFileName = "ca.crt" ;
194
+ private const string ServiceAccountNamespaceFileName = "namespace" ;
195
+
185
196
/// <summary>
186
- /// Whether running in k8s cluster
197
+ /// Whether running inside a k8s cluster
187
198
/// </summary>
188
199
/// <returns></returns>
189
200
private static bool IsInKubernetesCluster ( )
@@ -204,8 +215,17 @@ private static bool IsInKubernetesCluster()
204
215
var certPath = Path . Combine ( ServiceAccountPath , ServiceAccountRootCAKeyFileName ) ;
205
216
return File . Exists ( certPath ) ;
206
217
}
207
- #endregion ContainerEnvironment
208
218
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
209
229
}
210
230
211
231
public class LibraryInfo
@@ -243,4 +263,9 @@ public sealed class RuntimeInfo : LibraryInfo
243
263
/// Is running in a Kubernetes cluster
244
264
/// </summary>
245
265
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 ; }
246
271
}
0 commit comments