Skip to content

Commit 2d5c7c0

Browse files
Metrics optional property (#474)
* fix: timespan can be optional * use one comment on properties
1 parent 4ea7c26 commit 2d5c7c0

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

src/KubernetesClient/NodeMetrics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class NodeMetrics
1919
/// The timestamp when metrics were collected.
2020
/// </summary>
2121
[JsonProperty(PropertyName = "timestamp")]
22-
public DateTime Timestamp { get; set; }
22+
public DateTime? Timestamp { get; set; }
2323

2424
/// <summary>
2525
/// The interval from which metrics were collected.

src/KubernetesClient/PodMetrics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class PodMetrics
1919
/// The timestamp when metrics were collected.
2020
/// </summary>
2121
[JsonProperty(PropertyName = "timestamp")]
22-
public DateTime Timestamp { get; set; }
22+
public DateTime? Timestamp { get; set; }
2323

2424
/// <summary>
2525
/// The interval from which metrics were collected.

tests/KubernetesClient.Tests/Kubernetes.Metrics.Tests.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ public class KubernetesMetricsTests
1010
{
1111
private readonly ITestOutputHelper testOutput;
1212

13-
// Copy / Paste from metrics server on minikube
13+
// All payloads' response are Copy / Paste from metrics server on minikube
1414
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
15+
public const string OptionalPropertyNodeMetricsResponse = "{\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\": null,\n \"window\": \"1m0s\",\n \"usage\": {\n \"cpu\": \"394m\",\n \"memory\": \"1948140Ki\"\n }\n }\n ]\n}";
1616
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-
// Copy / Paste from metrics server minikube
17+
public const string OptionalPropertyPodMetricsResponse = "{\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\": null,\n \"window\": \"1m0s\",\n \"containers\": [\n {\n \"name\": \"dotnet-test\",\n \"usage\": {\n \"cpu\": \"0\",\n \"memory\": \"14512Ki\"\n }\n }\n ]\n }\n ]\n}"; // Copy / Paste from metrics server minikube
1818
public const string EmptyPodMetricsResponse = "{\n \"kind\": \"PodMetricsList\",\n \"apiVersion\": \"metrics.k8s.io/v1beta1\",\n \"metadata\": {\n \"selfLink\": \"/apis/metrics.k8s.io/v1beta1/namespaces/empty/pods/\"\n },\n \"items\": []\n}";
19-
// Copy / Paste from metrics server minikube
2019
public const string NonExistingNamespaceResponse = "{\n \"kind\": \"PodMetricsList\",\n \"apiVersion\": \"metrics.k8s.io/v1beta1\",\n \"metadata\": {\n \"selfLink\": \"/apis/metrics.k8s.io/v1beta1/namespaces/nonexisting/pods/\"\n },\n \"items\": []\n}";
2120

2221
public const string DefaultNodeName = "minikube";
@@ -49,6 +48,20 @@ public async Task NodesMetrics()
4948
}
5049
}
5150

51+
[Fact(DisplayName = "Node metrics optionnal property")]
52+
public async Task NodesMetricsOptionalProperty()
53+
{
54+
using (var server = new MockKubeApiServer(testOutput, resp: OptionalPropertyNodeMetricsResponse))
55+
{
56+
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() });
57+
58+
// Should not throw with timespan optional property
59+
var exception = await Record.ExceptionAsync(async () => await client.GetKubernetesNodesMetricsAsync().ConfigureAwait(false)).ConfigureAwait(false);
60+
61+
Assert.Null(exception);
62+
}
63+
}
64+
5265
[Fact(DisplayName = "Pod metrics")]
5366
public async Task PodsMetrics()
5467
{
@@ -73,6 +86,20 @@ public async Task PodsMetrics()
7386
}
7487
}
7588

89+
[Fact(DisplayName = "Pod metrics optionnal property")]
90+
public async Task PodsMetricsOptionalProperty()
91+
{
92+
using (var server = new MockKubeApiServer(testOutput, resp: OptionalPropertyPodMetricsResponse))
93+
{
94+
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() });
95+
96+
// Should not throw with timespan optional property
97+
var exception = await Record.ExceptionAsync(async () => await client.GetKubernetesPodsMetricsAsync().ConfigureAwait(false)).ConfigureAwait(false);
98+
99+
Assert.Null(exception);
100+
}
101+
}
102+
76103
[Fact(DisplayName = "Pod metrics empty response")]
77104
public async Task PodsMetricsEmptyResponse()
78105
{

0 commit comments

Comments
 (0)