-
Notifications
You must be signed in to change notification settings - Fork 841
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?
Add Kubernetes based Resource Monitoring #6748
Conversation
# Conflicts: # src/Shared/Instruments/ResourceUtilizationInstruments.cs
...aries/Microsoft.Extensions.ClusterMetadata.Kubernetes/KubernetesClusterMetadataExtensions.cs
Outdated
Show resolved
Hide resolved
...Extensions.ClusterMetadata.Kubernetes/Microsoft.Extensions.ClusterMetadata.Kubernetes.csproj
Outdated
Show resolved
Hide resolved
...raries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Linux/LinuxUtilizationProvider.cs
Outdated
Show resolved
Hide resolved
...rosoft.Extensions.Diagnostics.ResourceMonitoring/Windows/WindowsContainerSnapshotProvider.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/IResourceQuotasProvider.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/ResourceQuotas.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/IResourceQuotasProvider.cs
Outdated
Show resolved
Hide resolved
...ostics.ResourceMonitoring.Kubernetes/KubernetesResourceQuotasServiceCollectionsExtensions.cs
Outdated
Show resolved
Hide resolved
….com/amadeuszl/extensions into users/alechniak/kuberenetes-metadata
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.
Pull Request Overview
This PR refactors resource monitoring to extract quota calculation logic into dedicated ResourceQuotaProvider implementations and adds Kubernetes support for resource monitoring. The changes separate concerns by moving resource limit/request determination out of snapshot providers into reusable quota providers.
Key changes:
- Introduces
ResourceQuotamodel andResourceQuotaProviderabstract base class for platform-agnostic quota retrieval - Creates
WindowsContainerResourceQuotaProviderandLinuxResourceQuotaProviderimplementations for existing platforms - Adds new
Microsoft.Extensions.Diagnostics.ResourceMonitoring.Kuberneteslibrary with Kubernetes-specific quota provider that reads from environment variables
Reviewed Changes
Copilot reviewed 26 out of 27 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| ResourceQuota.cs | New model class defining CPU and memory quota properties (max and baseline) |
| ResourceQuotaProvider.cs | New abstract base class for quota provider implementations |
| WindowsContainerResourceQuotaProvider.cs | Extracts Windows quota logic from WindowsContainerSnapshotProvider |
| WindowsContainerSnapshotProvider.cs | Refactored to use ResourceQuotaProvider dependency |
| LinuxResourceQuotaProvider.cs | Extracts Linux quota logic from LinuxUtilizationProvider |
| LinuxUtilizationProvider.cs | Refactored to use ResourceQuotaProvider dependency |
| ILinuxUtilizationParser.cs | Adds GetMinMemoryInBytes() method for reading memory requests |
| LinuxUtilizationParserCgroupV1.cs | Stub implementation returning 0 (no request support in v1) |
| LinuxUtilizationParserCgroupV2.cs | Implements GetMinMemoryInBytes() reading memory.min/memory.low files |
| KubernetesMetadata.cs | Reads Kubernetes resource limits/requests from environment variables |
| KubernetesResourceQuotaProvider.cs | Kubernetes-specific quota provider converting millicores to cores |
| KubernetesResourceQuotaServiceCollectionExtensions.cs | DI registration for Kubernetes resource monitoring |
| ResourceMonitoringServiceCollectionExtensions.cs | Registers appropriate ResourceQuotaProvider for each platform |
| Test files | Updates all tests to create ResourceQuotaProvider instances; adds comprehensive Kubernetes test coverage |
| public static class KubernetesResourceQuotaServiceCollectionExtensions | ||
| { | ||
| /// <summary> | ||
| /// Configures and adds an Kubernetes resource monitoring components to a service collection alltoghter with necessary basic resource monitoring components. |
Copilot
AI
Nov 6, 2025
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.
Corrected spelling of 'alltoghter' to 'altogether'.
| /// Configures and adds an Kubernetes resource monitoring components to a service collection alltoghter with necessary basic resource monitoring components. | |
| /// Configures and adds an Kubernetes resource monitoring components to a service collection altogether with necessary basic resource monitoring components. |
| public ulong LimitsMemory { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the resource CPU limit the container is allowed to use in milicores. |
Copilot
AI
Nov 6, 2025
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.
Corrected spelling of 'milicores' to 'millicores'.
| public ulong RequestsMemory { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the resource CPU request the container is allowed to use in milicores. |
Copilot
AI
Nov 6, 2025
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.
Corrected spelling of 'milicores' to 'millicores'.
| /// <summary> | ||
| /// For CgroupV2 only. Reads the file /sys/fs/cgroup/memory.min, if 0 reads the file /sys/fs/cgroup/memory.low. | ||
| /// </summary> | ||
| /// <returns>memory.</returns> |
Copilot
AI
Nov 6, 2025
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.
The return value documentation is incomplete. Should provide a more descriptive summary such as 'The minimum memory allocated in bytes, or 0 if not available.'.
| /// <returns>memory.</returns> | |
| /// <returns>The minimum memory allocated in bytes, or 0 if not available.</returns> |
|
|
||
| var snapshotProvider = new LinuxUtilizationProvider(options, parser, meterFactoryMock.Object, logger, TimeProvider.System); | ||
| var resourceQuotaProvider = new LinuxResourceQuotaProvider(parser, options); | ||
| var snapshotProvider = new LinuxUtilizationProvider(options, parser, meterFactoryMock.Object, resourceQuotaProvider,logger, TimeProvider.System); |
Copilot
AI
Nov 6, 2025
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.
Missing space after comma before 'logger' parameter. Should be 'resourceQuotaProvider, logger'.
| var snapshotProvider = new LinuxUtilizationProvider(options, parser, meterFactoryMock.Object, resourceQuotaProvider,logger, TimeProvider.System); | |
| var snapshotProvider = new LinuxUtilizationProvider(options, parser, meterFactoryMock.Object, resourceQuotaProvider, logger, TimeProvider.System); |
...gnostics.ResourceMonitoring.Kubernetes/KubernetesResourceQuotaServiceCollectionExtensions.cs
Show resolved
Hide resolved
...gnostics.ResourceMonitoring.Kubernetes/KubernetesResourceQuotaServiceCollectionExtensions.cs
Show resolved
Hide resolved
| /// Maximum values define the upper limits of resource usage, while baseline values specify | ||
| /// the minimum assured resource allocations. |
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.
how does it translate into K8s request and limits?
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.
max are limits, and baselines are requests
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.
| if (quota.BaselineCpuInCores <= 0.0) | ||
| { | ||
| quota.BaselineCpuInCores = quota.MaxCpuInCores; | ||
| } | ||
|
|
||
| if (quota.BaselineMemoryInBytes == 0) | ||
| { | ||
| quota.BaselineMemoryInBytes = quota.MaxMemoryInBytes; | ||
| } | ||
|
|
||
| return quota; |
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.
Normally we would register IOptions<KubernetesMetadata> and validate those options with Validation API, so that whatever gets injected via constructor is either null or valid (nothing in between). Is there a rationale to avoid that here?
Fix #6496
Microsoft Reviewers: Open in CodeFlow