-
Notifications
You must be signed in to change notification settings - Fork 839
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
Draft
amadeuszl
wants to merge
13
commits into
dotnet:main
Choose a base branch
from
amadeuszl:users/alechniak/kuberenetes-metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+705
−91
Draft
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
be94825
Draft
amadeuszl bb36737
Draft
amadeuszl 6024134
Merge branch 'main' into users/alechniak/kuberenetes-metadata
amadeuszl 785a2f0
Add ResourceQuotasProvider
amadeuszl 95e1a3c
Fix public API surface, remove Quotas provider component
amadeuszl cfc4861
Switch layering of the abstractions to support Kubernetes metadata
amadeuszl 27fd482
Merge branch 'main' into users/alechniak/kuberenetes-metadata
amadeuszl 632636f
Fix namespace
amadeuszl 4faae5b
Remove ClusterMetadata
amadeuszl d49ab90
Add Linux changes
amadeuszl 727159a
Add abstractions project
amadeuszl 02517e4
Merge branch 'dotnet:main' into users/alechniak/kuberenetes-metadata
amadeuszl 8057769
Merge branch 'users/alechniak/kuberenetes-metadata' of https://github…
amadeuszl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...ries/Microsoft.Extensions.Diagnostics.ResourceMonitoring.Kubernetes/KubernetesMetadata.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
....Extensions.Diagnostics.ResourceMonitoring.Kubernetes/KubernetesResourceQuotasProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ics.ResourceMonitoring.Kubernetes/KubernetesResourceQuotasServiceCollectionsExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...nitoring.Kubernetes/Microsoft.Extensions.Diagnostics.ResourceMonitoring.Kubernetes.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
<MinCodeCoverage>99</MinCodeCoverage> | ||
<MinMutationScore>90</MinMutationScore> | ||
</PropertyGroup> | ||
|
||
<ItemGroup Condition="'$(TargetFramework)' == 'net462'"> | ||
<Compile Remove="Linux\**\*.cs" /> | ||
</ItemGroup> | ||
|
||
<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> |
Empty file.
10 changes: 10 additions & 0 deletions
10
src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/IResourceQuotasProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...raries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/LinuxResourceQuotasProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/ResourceQuotas.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.