Skip to content

Commit 5411bb6

Browse files
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
1 parent b5f5681 commit 5411bb6

File tree

10 files changed

+357
-0
lines changed

10 files changed

+357
-0
lines changed

examples/metrics/Metrics.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using k8s;
2+
using System;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace metrics
7+
{
8+
class Program
9+
{
10+
static async Task NodesMetrics(IKubernetes client)
11+
{
12+
var nodesMetrics = await client.GetKubernetesNodesMetricsAsync().ConfigureAwait(false);
13+
14+
foreach (var item in nodesMetrics.Items)
15+
{
16+
Console.WriteLine(item.Metadata.Name);
17+
18+
foreach (var metric in item.Usage)
19+
{
20+
Console.WriteLine($"{metric.Key}: {metric.Value}");
21+
}
22+
}
23+
}
24+
25+
static async Task PodsMetrics(IKubernetes client)
26+
{
27+
var podsMetrics = await client.GetKubernetesPodsMetricsAsync().ConfigureAwait(false);
28+
29+
if (!podsMetrics.Items.Any())
30+
{
31+
Console.WriteLine("Empty");
32+
}
33+
34+
foreach (var item in podsMetrics.Items)
35+
{
36+
foreach (var container in item.Containers)
37+
{
38+
Console.WriteLine(container.Name);
39+
40+
foreach (var metric in container.Usage)
41+
{
42+
Console.WriteLine($"{metric.Key}: {metric.Value}");
43+
}
44+
}
45+
Console.Write(Environment.NewLine);
46+
}
47+
}
48+
49+
static async Task Main(string[] args)
50+
{
51+
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
52+
var client = new Kubernetes(config);
53+
54+
await NodesMetrics(client).ConfigureAwait(false);
55+
Console.WriteLine(Environment.NewLine);
56+
await PodsMetrics(client).ConfigureAwait(false);
57+
}
58+
}
59+
}

examples/metrics/metrics.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\src\KubernetesClient\KubernetesClient.csproj" />
10+
</ItemGroup>
11+
12+
13+
</Project>

kubernetes-client.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "patch", "examples\patch\pat
3535
EndProject
3636
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "httpClientFactory", "examples\httpClientFactory\httpClientFactory.csproj", "{A07314A0-02E8-4F36-B233-726D59D28F08}"
3737
EndProject
38+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "metrics", "examples\metrics\metrics.csproj", "{B9647AD4-F6B0-406F-8B79-6781E31600EC}"
39+
EndProject
3840
Global
3941
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4042
Debug|Any CPU = Debug|Any CPU
@@ -189,6 +191,18 @@ Global
189191
{A07314A0-02E8-4F36-B233-726D59D28F08}.Release|x64.Build.0 = Release|Any CPU
190192
{A07314A0-02E8-4F36-B233-726D59D28F08}.Release|x86.ActiveCfg = Release|Any CPU
191193
{A07314A0-02E8-4F36-B233-726D59D28F08}.Release|x86.Build.0 = Release|Any CPU
194+
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
195+
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
196+
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Debug|x64.ActiveCfg = Debug|Any CPU
197+
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Debug|x64.Build.0 = Debug|Any CPU
198+
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Debug|x86.ActiveCfg = Debug|Any CPU
199+
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Debug|x86.Build.0 = Debug|Any CPU
200+
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
201+
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Release|Any CPU.Build.0 = Release|Any CPU
202+
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Release|x64.ActiveCfg = Release|Any CPU
203+
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Release|x64.Build.0 = Release|Any CPU
204+
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Release|x86.ActiveCfg = Release|Any CPU
205+
{B9647AD4-F6B0-406F-8B79-6781E31600EC}.Release|x86.Build.0 = Release|Any CPU
192206
EndGlobalSection
193207
GlobalSection(SolutionProperties) = preSolution
194208
HideSolutionNode = FALSE
@@ -206,6 +220,7 @@ Global
206220
{542DC30E-FDF7-4A35-B026-6C21F435E8B1} = {879F8787-C3BB-43F3-A92D-6D4C7D3A5285}
207221
{04DE2C84-117D-4E21-8B45-B7AE627697BD} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
208222
{A07314A0-02E8-4F36-B233-726D59D28F08} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
223+
{B9647AD4-F6B0-406F-8B79-6781E31600EC} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
209224
EndGlobalSection
210225
GlobalSection(ExtensibilityGlobals) = postSolution
211226
SolutionGuid = {049A763A-C891-4E8D-80CF-89DD3E22ADC7}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Newtonsoft.Json;
2+
using System.Collections.Generic;
3+
4+
namespace k8s.Models
5+
{
6+
/// <summary>
7+
/// Describes the resource usage metrics of a container pull from metrics server API.
8+
/// </summary>
9+
public class ContainerMetrics
10+
{
11+
/// <summary>
12+
/// Defines container name corresponding to the one from pod.spec.containers.
13+
/// </summary>
14+
[JsonProperty(PropertyName = "name")]
15+
public string Name { get; set; }
16+
17+
/// <summary>
18+
/// The resource usage.
19+
/// </summary>
20+
[JsonProperty(PropertyName = "usage")]
21+
public IDictionary<string, ResourceQuantity> Usage { get; set; }
22+
}
23+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using k8s.Models;
2+
using Newtonsoft.Json.Linq;
3+
using System.Threading.Tasks;
4+
5+
namespace k8s
6+
{
7+
/// <summary>
8+
/// Extension methods for Kubernetes metrics.
9+
/// </summary>
10+
public static class KubernetesMetricsExtensions
11+
{
12+
/// <summary>
13+
/// Get nodes metrics pull from metrics server API.
14+
/// </summary>
15+
public static async Task<NodeMetricsList> GetKubernetesNodesMetricsAsync(this IKubernetes operations)
16+
{
17+
JObject customObject = (JObject)await operations.GetClusterCustomObjectAsync("metrics.k8s.io", "v1beta1", "nodes", string.Empty).ConfigureAwait(false);
18+
return customObject.ToObject<NodeMetricsList>();
19+
}
20+
21+
/// <summary>
22+
/// Get pods metrics pull from metrics server API.
23+
/// </summary>
24+
public static async Task<PodMetricsList> GetKubernetesPodsMetricsAsync(this IKubernetes operations)
25+
{
26+
JObject customObject = (JObject)await operations.GetClusterCustomObjectAsync("metrics.k8s.io", "v1beta1", "pods", string.Empty).ConfigureAwait(false);
27+
return customObject.ToObject<PodMetricsList>();
28+
}
29+
30+
/// <summary>
31+
/// Get pods metrics by namespace pull from metrics server API.
32+
/// </summary>
33+
public static async Task<PodMetricsList> GetKubernetesPodsMetricsByNamespaceAsync(this IKubernetes operations, string namespaceParameter)
34+
{
35+
JObject customObject = (JObject)await operations.GetNamespacedCustomObjectAsync("metrics.k8s.io", "v1beta1", namespaceParameter, "pods", string.Empty).ConfigureAwait(false);
36+
return customObject.ToObject<PodMetricsList>();
37+
}
38+
}
39+
}

src/KubernetesClient/NodeMetrics.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace k8s.Models
6+
{
7+
/// <summary>
8+
/// Describes the resource usage metrics of a node pull from metrics server API.
9+
/// </summary>
10+
public class NodeMetrics
11+
{
12+
/// <summary>
13+
/// The kubernetes standard object's metadata.
14+
/// </summary>
15+
[JsonProperty(PropertyName = "metadata")]
16+
public V1ObjectMeta Metadata { get; set; }
17+
18+
/// <summary>
19+
/// The timestamp when metrics were collected.
20+
/// </summary>
21+
[JsonProperty(PropertyName = "timestamp")]
22+
public DateTime Timestamp { get; set; }
23+
24+
/// <summary>
25+
/// The interval from which metrics were collected.
26+
/// </summary>
27+
[JsonProperty(PropertyName = "window")]
28+
public string Window { get; set; }
29+
30+
/// <summary>
31+
/// The resource usage.
32+
/// </summary>
33+
[JsonProperty(PropertyName = "usage")]
34+
public IDictionary<string, ResourceQuantity> Usage { get; set; }
35+
}
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Newtonsoft.Json;
2+
using System.Collections.Generic;
3+
4+
namespace k8s.Models
5+
{
6+
public class NodeMetricsList
7+
{
8+
/// <summary>
9+
/// Defines the versioned schema of this representation of an object.
10+
/// </summary>
11+
[JsonProperty(PropertyName = "apiVersion")]
12+
public string ApiVersion { get; set; }
13+
14+
/// <summary>
15+
/// Defines the REST resource this object represents.
16+
/// </summary>
17+
[JsonProperty(PropertyName = "kind")]
18+
public string Kind { get; set; }
19+
20+
/// <summary>
21+
/// The kubernetes standard object's metadata.
22+
/// </summary>
23+
[JsonProperty(PropertyName = "metadata")]
24+
public V1ObjectMeta Metadata { get; set; }
25+
26+
/// <summary>
27+
/// The list of node metrics.
28+
/// </summary>
29+
[JsonProperty(PropertyName = "items")]
30+
public IEnumerable<NodeMetrics> Items { get; set; }
31+
}
32+
}

src/KubernetesClient/PodMetrics.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace k8s.Models
6+
{
7+
/// <summary>
8+
/// Describes the resource usage metrics of a pod pull from metrics server API.
9+
/// </summary>
10+
public class PodMetrics
11+
{
12+
/// <summary>
13+
/// The kubernetes standard object's metadata.
14+
/// </summary>
15+
[JsonProperty(PropertyName = "metadata")]
16+
public V1ObjectMeta Metadata { get; set; }
17+
18+
/// <summary>
19+
/// The timestamp when metrics were collected.
20+
/// </summary>
21+
[JsonProperty(PropertyName = "timestamp")]
22+
public DateTime Timestamp { get; set; }
23+
24+
/// <summary>
25+
/// The interval from which metrics were collected.
26+
/// </summary>
27+
[JsonProperty(PropertyName = "window")]
28+
public string Window { get; set; }
29+
30+
/// <summary>
31+
/// The list of containers metrics.
32+
/// </summary>
33+
[JsonProperty(PropertyName = "containers")]
34+
public List<ContainerMetrics> Containers { get; set; }
35+
}
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Newtonsoft.Json;
2+
using System.Collections.Generic;
3+
4+
namespace k8s.Models
5+
{
6+
public class PodMetricsList
7+
{
8+
/// <summary>
9+
/// Defines the versioned schema of this representation of an object.
10+
/// </summary>
11+
[JsonProperty(PropertyName = "apiVersion")]
12+
public string ApiVersion { get; set; }
13+
14+
/// <summary>
15+
/// Defines the REST resource this object represents.
16+
/// </summary>
17+
[JsonProperty(PropertyName = "kind")]
18+
public string Kind { get; set; }
19+
20+
/// <summary>
21+
/// The kubernetes standard object's metadata.
22+
/// </summary>
23+
[JsonProperty(PropertyName = "metadata")]
24+
public V1ObjectMeta Metadata { get; set; }
25+
26+
/// <summary>
27+
/// The list of pod metrics.
28+
/// </summary>
29+
[JsonProperty(PropertyName = "items")]
30+
public IEnumerable<PodMetrics> Items { get; set; }
31+
}
32+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using k8s.Tests.Mock;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Xunit;
5+
using Xunit.Abstractions;
6+
7+
namespace k8s.Tests
8+
{
9+
public class KubernetesMetricsTests
10+
{
11+
private readonly ITestOutputHelper testOutput;
12+
13+
// Copy / Paste from metrics server on minikube
14+
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}";
15+
// Copy / Paste from metrics server minikube
16+
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}";
17+
18+
public const string DefaultNodeName = "minikube";
19+
public const string DefaultPodName = "dotnet-test";
20+
public const string DefaultCpuKey = "cpu";
21+
public const string DefaultMemoryKey = "memory";
22+
23+
public KubernetesMetricsTests(ITestOutputHelper testOutput)
24+
{
25+
this.testOutput = testOutput;
26+
}
27+
28+
[Fact(DisplayName = "Node metrics")]
29+
public async Task NodesMetrics()
30+
{
31+
using (var server = new MockKubeApiServer(testOutput, resp: NodeMetricsResponse))
32+
{
33+
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() });
34+
35+
var nodesMetricsList = await client.GetKubernetesNodesMetricsAsync().ConfigureAwait(false);
36+
37+
Assert.Single(nodesMetricsList.Items);
38+
39+
var nodeMetrics = nodesMetricsList.Items.First();
40+
Assert.Equal(DefaultNodeName, nodeMetrics.Metadata.Name);
41+
42+
Assert.Equal(2, nodeMetrics.Usage.Count);
43+
Assert.True(nodeMetrics.Usage.ContainsKey(DefaultCpuKey));
44+
Assert.True(nodeMetrics.Usage.ContainsKey(DefaultMemoryKey));
45+
}
46+
}
47+
48+
[Fact(DisplayName = "Pod metrics")]
49+
public async Task PodsMetrics()
50+
{
51+
using (var server = new MockKubeApiServer(testOutput, resp: PodMetricsResponse))
52+
{
53+
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() });
54+
55+
var podsMetricsList = await client.GetKubernetesPodsMetricsAsync().ConfigureAwait(false);
56+
57+
Assert.Single(podsMetricsList.Items);
58+
59+
var podMetrics = podsMetricsList.Items.First();
60+
61+
Assert.Single(podMetrics.Containers);
62+
63+
var containerMetrics = podMetrics.Containers.First();
64+
Assert.Equal(DefaultPodName, containerMetrics.Name);
65+
66+
Assert.Equal(2, containerMetrics.Usage.Count);
67+
Assert.True(containerMetrics.Usage.ContainsKey(DefaultCpuKey));
68+
Assert.True(containerMetrics.Usage.ContainsKey(DefaultMemoryKey));
69+
}
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)