-
Notifications
You must be signed in to change notification settings - Fork 842
Add Kubernetes based Resource Monitoring #6748
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
base: main
Are you sure you want to change the base?
Changes from 10 commits
be94825
bb36737
6024134
785a2f0
95e1a3c
cfc4861
27fd482
632636f
4faae5b
d49ab90
727159a
02517e4
8057769
ced4716
66bf3ca
67c3553
87b81cf
cd8bf74
296f896
01f2f4d
fb94614
d9158c0
9e4eb2b
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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Globalization; | ||
|
|
||
| namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Kubernetes; | ||
|
|
||
| public class KubernetesMetadata | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the resource memory limit the container is allowed to use. | ||
| /// </summary> | ||
| public ulong LimitsMemory { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the resource CPU limit the container is allowed to use. | ||
| /// </summary> | ||
| public ulong LimitsCpu { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the resource memory request the container is allowed to use. | ||
| /// </summary> | ||
| public ulong RequestsMemory { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the resource CPU request the container is allowed to use. | ||
| /// </summary> | ||
| public ulong RequestsCpu { get; set; } | ||
|
|
||
| private string _environmentVariablePrefix; | ||
|
|
||
| public KubernetesMetadata(string environmentVariablePrefix) | ||
| { | ||
| _environmentVariablePrefix = environmentVariablePrefix; | ||
| } | ||
|
|
||
| public void Build() | ||
| { | ||
| LimitsMemory = Convert.ToUInt64(Environment.GetEnvironmentVariable($"{_environmentVariablePrefix}LIMITS_MEMORY"), CultureInfo.InvariantCulture); | ||
| LimitsCpu = Convert.ToUInt64(Environment.GetEnvironmentVariable($"{_environmentVariablePrefix}LIMITS_CPU"), CultureInfo.InvariantCulture); | ||
| RequestsMemory = Convert.ToUInt64(Environment.GetEnvironmentVariable($"{_environmentVariablePrefix}REQUESTS_MEMORY"), CultureInfo.InvariantCulture); | ||
| RequestsCpu = Convert.ToUInt64(Environment.GetEnvironmentVariable($"{_environmentVariablePrefix}REQUESTS_CPU"), CultureInfo.InvariantCulture); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Kubernetes; | ||
|
|
||
| internal class KubernetesResourceQuotasProvider : IResourceQuotasProvider | ||
| { | ||
| private KubernetesMetadata _cosmicMetadata; | ||
|
|
||
| public KubernetesResourceQuotasProvider(KubernetesMetadata cosmicMetadata) // we could inject ClusterMetadataHere in the future instead of that thingy | ||
| { | ||
| _cosmicMetadata = cosmicMetadata; | ||
| } | ||
|
|
||
| public ResourceQuotas GetResourceQuotas() | ||
| { | ||
| // extract relevant cluster metadata | ||
| return new ResourceQuotas | ||
| { | ||
| RequestsCpu = ConvertMillicoreToUnit(_cosmicMetadata.RequestsCpu), | ||
| LimitsCpu = ConvertMillicoreToUnit(_cosmicMetadata.LimitsCpu), | ||
| RequestsMemory = _cosmicMetadata.RequestsMemory, | ||
| LimitsMemory = _cosmicMetadata.LimitsMemory, | ||
| }; | ||
| } | ||
|
|
||
| private static double ConvertMillicoreToUnit(ulong millicores) | ||
| { | ||
| return millicores / 1000.0; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
|
||
| namespace Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public static class KubernetesResourceQuotasServiceCollectionsExtensions | ||
| { | ||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| /// <param name="services"></param> | ||
| /// <param name="configureOptions"></param> | ||
| /// <returns></returns> | ||
| public static IServiceCollection AddKubernetesResourceQuotas( | ||
| this IServiceCollection services, | ||
| Action<KubernetesMetadata>? configureOptions = null) | ||
| { | ||
| // Simple - just register before AddResourceMonitoring() is called | ||
| services.TryAddSingleton<IResourceQuotasProvider, KubernetesResourceQuotasProvider>(); | ||
|
|
||
| if (configureOptions != null) | ||
| { | ||
| _ = services.Configure(configureOptions); | ||
| } | ||
|
|
||
| services.AddResourceMonitoring(); | ||
|
|
||
| return services; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,56 @@ | ||||
| <Project Sdk="Microsoft.NET.Sdk"> | ||||
| <PropertyGroup> | ||||
| <RootNamespace>Microsoft.Extensions.Diagnostics.ResourceMonitoring.Kubernetes</RootNamespace> | ||||
| <Description>Measures processor and memory usage.</Description> | ||||
| <Workstream>ResourceMonitoring</Workstream> | ||||
| <NoWarn Condition="'$(TargetFramework)' == 'net462'">$(NoWarn);CS0436</NoWarn> | ||||
| </PropertyGroup> | ||||
|
|
||||
| <PropertyGroup> | ||||
| <AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||||
| <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator> | ||||
| <UseLoggingGenerator>true</UseLoggingGenerator> | ||||
| <InjectSharedDataValidation>true</InjectSharedDataValidation> | ||||
| <InjectSharedRentedSpan>true</InjectSharedRentedSpan> | ||||
| <InjectExperimentalAttributeOnLegacy>true</InjectExperimentalAttributeOnLegacy> | ||||
| <InjectObsoleteAttributeOnLegacy>true</InjectObsoleteAttributeOnLegacy> | ||||
| <InjectPlatformAttributesOnLegacy>true</InjectPlatformAttributesOnLegacy> | ||||
| <InjectSharedBufferWriterPool>true</InjectSharedBufferWriterPool> | ||||
| <InjectSharedDiagnosticIds>true</InjectSharedDiagnosticIds> | ||||
| <InjectStringSplitExtensions>true</InjectStringSplitExtensions> | ||||
| <InjectSharedInstruments>true</InjectSharedInstruments> | ||||
| </PropertyGroup> | ||||
|
|
||||
| <PropertyGroup> | ||||
| <Stage>normal</Stage> | ||||
|
Member
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. I think it should be extensions/eng/MSBuild/Packaging.targets Line 40 in 7ddf87e
Contributor
Author
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. Let me verify that. I would prefer to release it already in December as normal. |
||||
| <MinCodeCoverage>99</MinCodeCoverage> | ||||
| <MinMutationScore>90</MinMutationScore> | ||||
| </PropertyGroup> | ||||
|
|
||||
| <ItemGroup Condition="'$(TargetFramework)' == 'net462'"> | ||||
| <Compile Remove="Linux\**\*.cs" /> | ||||
| </ItemGroup> | ||||
amadeuszl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
|
|
||||
| <ItemGroup> | ||||
| <ProjectReference Include="..\Microsoft.Extensions.ClusterMetadata.Kubernetes\Microsoft.Extensions.ClusterMetadata.Kubernetes.csproj" /> | ||||
| <ProjectReference Include="..\Microsoft.Extensions.Telemetry.Abstractions\Microsoft.Extensions.Telemetry.Abstractions.csproj" /> | ||||
| <ProjectReference Include="..\Microsoft.Extensions.Diagnostics.ResourceMonitoring\Microsoft.Extensions.Diagnostics.ResourceMonitoring.csproj" /> | ||||
| <ProjectReference Include="..\Microsoft.Extensions.DependencyInjection.AutoActivation\Microsoft.Extensions.DependencyInjection.AutoActivation.csproj" /> | ||||
| </ItemGroup> | ||||
|
|
||||
| <ItemGroup> | ||||
| <PackageReference Include="Microsoft.Bcl.HashCode" Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))" /> | ||||
| <PackageReference Include="Microsoft.Bcl.TimeProvider" /> | ||||
| <PackageReference Include="Microsoft.Extensions.Configuration.Binder" /> | ||||
| <PackageReference Include="Microsoft.Extensions.Diagnostics" /> | ||||
| <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" /> | ||||
| <PackageReference Include="System.Collections.Immutable" Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))" /> | ||||
| <PackageReference Include="System.Diagnostics.PerformanceCounter" /> | ||||
| <PackageReference Include="System.Threading.Tasks.Extensions" Condition="'$(TargetFramework)' == 'net462'" /> | ||||
| </ItemGroup> | ||||
|
|
||||
| <ItemGroup> | ||||
| <InternalsVisibleToDynamicProxyGenAssembly2 Include="*" /> | ||||
| <InternalsVisibleToTest Include="$(AssemblyName).Tests" /> | ||||
| </ItemGroup> | ||||
| </Project> | ||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring; | ||
|
|
||
| public interface IResourceQuotasProvider | ||
amadeuszl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| public ResourceQuotas GetResourceQuotas(); | ||
| } | ||
|
|
||
amadeuszl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring; | ||
|
|
||
| internal class LinuxResourceQuotasProvider : IResourceQuotasProvider | ||
|
|
||
| { | ||
| public ResourceQuotas GetResourceQuotas() | ||
| { | ||
| // bring logic from LinuxUtilizationProvider for limits and requests | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring; | ||
|
|
||
| public class ResourceQuotas | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the resource memory limit the container is allowed to use. | ||
| /// </summary> | ||
| public ulong LimitsMemory { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the resource CPU limit the container is allowed to use. | ||
| /// </summary> | ||
| public ulong LimitsCpu { get; set; } | ||
amadeuszl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /// <summary> | ||
| /// Gets or sets the resource memory request the container is allowed to use. | ||
| /// </summary> | ||
| public ulong RequestsMemory { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the resource CPU request the container is allowed to use. | ||
| /// </summary> | ||
| public ulong RequestsCpu { get; set; } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.