Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/prometheus/Prometheus.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using k8s;
using Prometheus;
using System;
using System.Net.Http;
using System.Threading;

namespace prom
Expand All @@ -12,7 +11,7 @@ private static void Main(string[] args)
{
var config = KubernetesClientConfiguration.BuildDefaultConfig();
var handler = new PrometheusHandler();
IKubernetes client = new Kubernetes(config, new DelegatingHandler[] { handler });
IKubernetes client = new Kubernetes(config, handler);

var server = new MetricServer(hostname: "localhost", port: 1234);
server.Start();
Expand Down
15 changes: 15 additions & 0 deletions src/KubernetesClient.Models/KubernetesJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ static KubernetesJson()
JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
}

/// <summary>
/// Configures <see cref="JsonSerializerOptions"/> for the <see cref="JsonSerializer"/>.
/// The <see cref="JsonSerializerOptions"/> can only be modified before the first serialization or deserialization takes place.
/// To override existing converters, add them to the top of the <see cref="JsonSerializerOptions.Converters"/> list
/// e.g. as follows: <code>options.Converters.Insert(index: 0, new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));</code>
/// </summary>
/// <param name="configure">An <see cref="Action"/> to configure the <see cref="JsonSerializerOptions"/>.</param>
public static void AddJsonOptions(Action<JsonSerializerOptions> configure)
{
if (configure is not null)
{
configure(JsonSerializerOptions);
}
}

public static TValue Deserialize<TValue>(string json)
{
return JsonSerializer.Deserialize<TValue>(json, JsonSerializerOptions);
Expand Down
2 changes: 0 additions & 2 deletions src/KubernetesClient/Kubernetes.ConfigInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ private void CreateHttpClient(DelegatingHandler[] handlers, KubernetesClientConf
};
}



/// <summary>
/// Set credentials for the Client
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ public static Process CreateRunnableExternalProcess(ExternalExecution config)
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;

return process;
}

Expand Down
6 changes: 6 additions & 0 deletions src/KubernetesClient/KubernetesClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,11 @@ public partial class KubernetesClientConfiguration
/// Do not use http2 even it is available
/// </summary>
public bool DisableHttp2 { get; set; } = false;

/// <inheritdoc cref="KubernetesJson.AddJsonOptions(Action{JsonSerializerOptions})"/>
public void AddJsonOptions(Action<JsonSerializerOptions> configure)
{
KubernetesJson.AddJsonOptions(configure);
Comment thread
tg123 marked this conversation as resolved.
Outdated
}
}
}
50 changes: 50 additions & 0 deletions tests/KubernetesClient.Tests/SerializationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using k8s.Tests.Mock;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace k8s.Tests
{
public class SerializationTests
{
private readonly ITestOutputHelper testOutput;

private enum Animals
{
Dog,
Cat,
Mouse,
}

public SerializationTests(ITestOutputHelper testOutput)
{
this.testOutput = testOutput;
}

[Fact]
public async Task SerializeEnumUsingCamelCase()
{
using var server = new MockKubeApiServer(testOutput);

var config = new KubernetesClientConfiguration { Host = server.Uri.ToString() };
config.AddJsonOptions(options =>
{
// Insert the converter at the front of the list so it overrides
// the default JsonStringEnumConverter without namingPolicy.
options.Converters.Insert(index: 0, new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
});
var client = new Kubernetes(config);

var customObject = Animals.Dog;

var result = await client.CustomObjects.CreateNamespacedCustomObjectWithHttpMessagesAsync(customObject, "TestGroup", "TestVersion", "TestNamespace", "TestPlural").ConfigureAwait(false);
var content = await result.Request.Content.ReadAsStringAsync();
Assert.Equal(@"""dog""", content);

string animal = KubernetesJson.Serialize(Animals.Cat);
Assert.Equal(@"""cat""", animal);
}
}
}
13 changes: 7 additions & 6 deletions tests/KubernetesClient.Tests/WatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,8 @@ public async Task TestWatchWithHandlers()
var handler1 = new DummyHandler();
var handler2 = new DummyHandler();

var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() }, handler1,
handler2);
var client = new Kubernetes(
new KubernetesClientConfiguration { Host = server.Uri.ToString() }, handler1, handler2);

Assert.False(handler1.Called);
Assert.False(handler2.Called);
Expand Down Expand Up @@ -732,12 +732,13 @@ public async Task MustHttp2VersionSet()
return false;
});

var h = new CheckHeaderDelegatingHandler();
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() }, h);
var handler = new CheckHeaderDelegatingHandler();
var client = new Kubernetes(
new KubernetesClientConfiguration { Host = server.Uri.ToString() }, handler);

Assert.Null(h.Version);
Assert.Null(handler.Version);
await client.CoreV1.ListNamespacedPodWithHttpMessagesAsync("default", watch: true).ConfigureAwait(false);
Assert.Equal(HttpVersion.Version20, h.Version);
Assert.Equal(HttpVersion.Version20, handler.Version);
}
}
}