Nodes and pods metrics (#466)
* Add models for node metrics * Add models for pod metrics * Add extension method for node metrics * Add extension method for pods metrics * dotnet format * fix type: use of interface type * Add metrics sample * Add tests for node and pod metrics
This commit is contained in:
59
examples/metrics/Metrics.cs
Normal file
59
examples/metrics/Metrics.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using k8s;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace metrics
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static async Task NodesMetrics(IKubernetes client)
|
||||
{
|
||||
var nodesMetrics = await client.GetKubernetesNodesMetricsAsync().ConfigureAwait(false);
|
||||
|
||||
foreach (var item in nodesMetrics.Items)
|
||||
{
|
||||
Console.WriteLine(item.Metadata.Name);
|
||||
|
||||
foreach (var metric in item.Usage)
|
||||
{
|
||||
Console.WriteLine($"{metric.Key}: {metric.Value}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static async Task PodsMetrics(IKubernetes client)
|
||||
{
|
||||
var podsMetrics = await client.GetKubernetesPodsMetricsAsync().ConfigureAwait(false);
|
||||
|
||||
if (!podsMetrics.Items.Any())
|
||||
{
|
||||
Console.WriteLine("Empty");
|
||||
}
|
||||
|
||||
foreach (var item in podsMetrics.Items)
|
||||
{
|
||||
foreach (var container in item.Containers)
|
||||
{
|
||||
Console.WriteLine(container.Name);
|
||||
|
||||
foreach (var metric in container.Usage)
|
||||
{
|
||||
Console.WriteLine($"{metric.Key}: {metric.Value}");
|
||||
}
|
||||
}
|
||||
Console.Write(Environment.NewLine);
|
||||
}
|
||||
}
|
||||
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
||||
var client = new Kubernetes(config);
|
||||
|
||||
await NodesMetrics(client).ConfigureAwait(false);
|
||||
Console.WriteLine(Environment.NewLine);
|
||||
await PodsMetrics(client).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
13
examples/metrics/metrics.csproj
Normal file
13
examples/metrics/metrics.csproj
Normal file
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\KubernetesClient\KubernetesClient.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</Project>
|
||||
@@ -35,6 +35,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "patch", "examples\patch\pat
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "httpClientFactory", "examples\httpClientFactory\httpClientFactory.csproj", "{A07314A0-02E8-4F36-B233-726D59D28F08}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "metrics", "examples\metrics\metrics.csproj", "{B9647AD4-F6B0-406F-8B79-6781E31600EC}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -189,6 +191,18 @@ Global
|
||||
{A07314A0-02E8-4F36-B233-726D59D28F08}.Release|x64.Build.0 = Release|Any CPU
|
||||
{A07314A0-02E8-4F36-B233-726D59D28F08}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{A07314A0-02E8-4F36-B233-726D59D28F08}.Release|x86.Build.0 = Release|Any CPU
|
||||
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Release|x64.Build.0 = Release|Any CPU
|
||||
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@@ -206,6 +220,7 @@ Global
|
||||
{542DC30E-FDF7-4A35-B026-6C21F435E8B1} = {879F8787-C3BB-43F3-A92D-6D4C7D3A5285}
|
||||
{04DE2C84-117D-4E21-8B45-B7AE627697BD} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
|
||||
{A07314A0-02E8-4F36-B233-726D59D28F08} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
|
||||
{B9647AD4-F6B0-406F-8B79-6781E31600EC} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {049A763A-C891-4E8D-80CF-89DD3E22ADC7}
|
||||
|
||||
23
src/KubernetesClient/ContainerMetrics.cs
Normal file
23
src/KubernetesClient/ContainerMetrics.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace k8s.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Describes the resource usage metrics of a container pull from metrics server API.
|
||||
/// </summary>
|
||||
public class ContainerMetrics
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines container name corresponding to the one from pod.spec.containers.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The resource usage.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "usage")]
|
||||
public IDictionary<string, ResourceQuantity> Usage { get; set; }
|
||||
}
|
||||
}
|
||||
39
src/KubernetesClient/KubernetesMetricsExtensions.cs
Normal file
39
src/KubernetesClient/KubernetesMetricsExtensions.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using k8s.Models;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace k8s
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for Kubernetes metrics.
|
||||
/// </summary>
|
||||
public static class KubernetesMetricsExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Get nodes metrics pull from metrics server API.
|
||||
/// </summary>
|
||||
public static async Task<NodeMetricsList> GetKubernetesNodesMetricsAsync(this IKubernetes operations)
|
||||
{
|
||||
JObject customObject = (JObject)await operations.GetClusterCustomObjectAsync("metrics.k8s.io", "v1beta1", "nodes", string.Empty).ConfigureAwait(false);
|
||||
return customObject.ToObject<NodeMetricsList>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get pods metrics pull from metrics server API.
|
||||
/// </summary>
|
||||
public static async Task<PodMetricsList> GetKubernetesPodsMetricsAsync(this IKubernetes operations)
|
||||
{
|
||||
JObject customObject = (JObject)await operations.GetClusterCustomObjectAsync("metrics.k8s.io", "v1beta1", "pods", string.Empty).ConfigureAwait(false);
|
||||
return customObject.ToObject<PodMetricsList>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get pods metrics by namespace pull from metrics server API.
|
||||
/// </summary>
|
||||
public static async Task<PodMetricsList> GetKubernetesPodsMetricsByNamespaceAsync(this IKubernetes operations, string namespaceParameter)
|
||||
{
|
||||
JObject customObject = (JObject)await operations.GetNamespacedCustomObjectAsync("metrics.k8s.io", "v1beta1", namespaceParameter, "pods", string.Empty).ConfigureAwait(false);
|
||||
return customObject.ToObject<PodMetricsList>();
|
||||
}
|
||||
}
|
||||
}
|
||||
36
src/KubernetesClient/NodeMetrics.cs
Normal file
36
src/KubernetesClient/NodeMetrics.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace k8s.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Describes the resource usage metrics of a node pull from metrics server API.
|
||||
/// </summary>
|
||||
public class NodeMetrics
|
||||
{
|
||||
/// <summary>
|
||||
/// The kubernetes standard object's metadata.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "metadata")]
|
||||
public V1ObjectMeta Metadata { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The timestamp when metrics were collected.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "timestamp")]
|
||||
public DateTime Timestamp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The interval from which metrics were collected.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "window")]
|
||||
public string Window { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The resource usage.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "usage")]
|
||||
public IDictionary<string, ResourceQuantity> Usage { get; set; }
|
||||
}
|
||||
}
|
||||
32
src/KubernetesClient/NodeMetricsList.cs
Normal file
32
src/KubernetesClient/NodeMetricsList.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace k8s.Models
|
||||
{
|
||||
public class NodeMetricsList
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the versioned schema of this representation of an object.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "apiVersion")]
|
||||
public string ApiVersion { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Defines the REST resource this object represents.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "kind")]
|
||||
public string Kind { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The kubernetes standard object's metadata.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "metadata")]
|
||||
public V1ObjectMeta Metadata { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The list of node metrics.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "items")]
|
||||
public IEnumerable<NodeMetrics> Items { get; set; }
|
||||
}
|
||||
}
|
||||
36
src/KubernetesClient/PodMetrics.cs
Normal file
36
src/KubernetesClient/PodMetrics.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace k8s.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Describes the resource usage metrics of a pod pull from metrics server API.
|
||||
/// </summary>
|
||||
public class PodMetrics
|
||||
{
|
||||
/// <summary>
|
||||
/// The kubernetes standard object's metadata.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "metadata")]
|
||||
public V1ObjectMeta Metadata { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The timestamp when metrics were collected.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "timestamp")]
|
||||
public DateTime Timestamp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The interval from which metrics were collected.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "window")]
|
||||
public string Window { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The list of containers metrics.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "containers")]
|
||||
public List<ContainerMetrics> Containers { get; set; }
|
||||
}
|
||||
}
|
||||
32
src/KubernetesClient/PodMetricsList.cs
Normal file
32
src/KubernetesClient/PodMetricsList.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace k8s.Models
|
||||
{
|
||||
public class PodMetricsList
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the versioned schema of this representation of an object.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "apiVersion")]
|
||||
public string ApiVersion { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Defines the REST resource this object represents.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "kind")]
|
||||
public string Kind { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The kubernetes standard object's metadata.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "metadata")]
|
||||
public V1ObjectMeta Metadata { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The list of pod metrics.
|
||||
/// </summary>
|
||||
[JsonProperty(PropertyName = "items")]
|
||||
public IEnumerable<PodMetrics> Items { get; set; }
|
||||
}
|
||||
}
|
||||
72
tests/KubernetesClient.Tests/Kubernetes.Metrics.Tests.cs
Normal file
72
tests/KubernetesClient.Tests/Kubernetes.Metrics.Tests.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using k8s.Tests.Mock;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace k8s.Tests
|
||||
{
|
||||
public class KubernetesMetricsTests
|
||||
{
|
||||
private readonly ITestOutputHelper testOutput;
|
||||
|
||||
// Copy / Paste from metrics server on minikube
|
||||
public const string NodeMetricsResponse = "{\n \"kind\": \"NodeMetricsList\",\n \"apiVersion\": \"metrics.k8s.io/v1beta1\",\n \"metadata\": {\n \"selfLink\": \"/apis/metrics.k8s.io/v1beta1/nodes/\"\n },\n \"items\": [\n {\n \"metadata\": {\n \"name\": \"minikube\",\n \"selfLink\": \"/apis/metrics.k8s.io/v1beta1/nodes/minikube\",\n \"creationTimestamp\": \"2020-07-28T20:01:05Z\"\n },\n \"timestamp\": \"2020-07-28T20:01:00Z\",\n \"window\": \"1m0s\",\n \"usage\": {\n \"cpu\": \"394m\",\n \"memory\": \"1948140Ki\"\n }\n }\n ]\n}";
|
||||
// Copy / Paste from metrics server minikube
|
||||
public const string PodMetricsResponse = "{\n \"kind\": \"PodMetricsList\",\n \"apiVersion\": \"metrics.k8s.io/v1beta1\",\n \"metadata\": {\n \"selfLink\": \"/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/\"\n },\n \"items\": [\n {\n \"metadata\": {\n \"name\": \"dotnet-test-d4894bfbd-2q2dw\",\n \"namespace\": \"default\",\n \"selfLink\": \"/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/dotnet-test-d4894bfbd-2q2dw\",\n \"creationTimestamp\": \"2020-08-01T07:40:05Z\"\n },\n \"timestamp\": \"2020-08-01T07:40:00Z\",\n \"window\": \"1m0s\",\n \"containers\": [\n {\n \"name\": \"dotnet-test\",\n \"usage\": {\n \"cpu\": \"0\",\n \"memory\": \"14512Ki\"\n }\n }\n ]\n }\n ]\n}";
|
||||
|
||||
public const string DefaultNodeName = "minikube";
|
||||
public const string DefaultPodName = "dotnet-test";
|
||||
public const string DefaultCpuKey = "cpu";
|
||||
public const string DefaultMemoryKey = "memory";
|
||||
|
||||
public KubernetesMetricsTests(ITestOutputHelper testOutput)
|
||||
{
|
||||
this.testOutput = testOutput;
|
||||
}
|
||||
|
||||
[Fact(DisplayName = "Node metrics")]
|
||||
public async Task NodesMetrics()
|
||||
{
|
||||
using (var server = new MockKubeApiServer(testOutput, resp: NodeMetricsResponse))
|
||||
{
|
||||
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() });
|
||||
|
||||
var nodesMetricsList = await client.GetKubernetesNodesMetricsAsync().ConfigureAwait(false);
|
||||
|
||||
Assert.Single(nodesMetricsList.Items);
|
||||
|
||||
var nodeMetrics = nodesMetricsList.Items.First();
|
||||
Assert.Equal(DefaultNodeName, nodeMetrics.Metadata.Name);
|
||||
|
||||
Assert.Equal(2, nodeMetrics.Usage.Count);
|
||||
Assert.True(nodeMetrics.Usage.ContainsKey(DefaultCpuKey));
|
||||
Assert.True(nodeMetrics.Usage.ContainsKey(DefaultMemoryKey));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact(DisplayName = "Pod metrics")]
|
||||
public async Task PodsMetrics()
|
||||
{
|
||||
using (var server = new MockKubeApiServer(testOutput, resp: PodMetricsResponse))
|
||||
{
|
||||
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() });
|
||||
|
||||
var podsMetricsList = await client.GetKubernetesPodsMetricsAsync().ConfigureAwait(false);
|
||||
|
||||
Assert.Single(podsMetricsList.Items);
|
||||
|
||||
var podMetrics = podsMetricsList.Items.First();
|
||||
|
||||
Assert.Single(podMetrics.Containers);
|
||||
|
||||
var containerMetrics = podMetrics.Containers.First();
|
||||
Assert.Equal(DefaultPodName, containerMetrics.Name);
|
||||
|
||||
Assert.Equal(2, containerMetrics.Usage.Count);
|
||||
Assert.True(containerMetrics.Usage.ContainsKey(DefaultCpuKey));
|
||||
Assert.True(containerMetrics.Usage.ContainsKey(DefaultMemoryKey));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user