Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
fix: not using the K8s options in every case. (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
Silvenga authored May 24, 2022
1 parent e950577 commit 02b4f0b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.user
*.userosscache
*.sln.docstates
.vs/

# Ci things
node_modules/
Expand Down
6 changes: 6 additions & 0 deletions src/DotnetKubernetesClient/DotnetKubernetesClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@
<PackageReference Include="KubernetesClient" Version="7.2.19" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>DotnetKubernetesClient.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

</Project>
23 changes: 18 additions & 5 deletions src/DotnetKubernetesClient/KubernetesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Task<string> GetCurrentNamespace(string downwardApiEnvName = "POD_NAMESPA

if (result is JsonElement element)
{
return element.Deserialize<TResource>();
return Deserialize<TResource>(element);
}

return null;
Expand Down Expand Up @@ -122,7 +122,7 @@ public async Task<IList<TResource>> List<TResource>(

if (result is JsonElement element)
{
var list = element.Deserialize<EntityList<TResource>>();
var list = Deserialize<EntityList<TResource>>(element);
return list?.Items ?? throw new ArgumentException("Could not parse result");
}

Expand Down Expand Up @@ -172,7 +172,7 @@ public async Task<TResource> Create<TResource>(TResource resource)

if (result is JsonElement element)
{
return element.Deserialize<TResource>() ?? throw new ArgumentException("Could not parse result");
return Deserialize<TResource>(element) ?? throw new ArgumentException("Could not parse result");
}

throw new ArgumentException("Could not parse result");
Expand Down Expand Up @@ -200,7 +200,7 @@ public async Task<TResource> Update<TResource>(TResource resource)

if (result is JsonElement element)
{
return element.Deserialize<TResource>() ?? throw new ArgumentException("Could not parse result");
return Deserialize<TResource>(element) ?? throw new ArgumentException("Could not parse result");
}

throw new ArgumentException("Could not parse result");
Expand Down Expand Up @@ -228,7 +228,7 @@ public async Task UpdateStatus<TResource>(TResource resource)

if (result is JsonElement element)
{
var parsed = element.Deserialize<TResource>() ?? throw new ArgumentException("Could not parse result");
var parsed = Deserialize<TResource>(element) ?? throw new ArgumentException("Could not parse result");
resource.Metadata.ResourceVersion = parsed.Metadata.ResourceVersion;
return;
}
Expand Down Expand Up @@ -333,4 +333,17 @@ public Task<Watcher<TResource>> Watch<TResource>(
onError,
onClose));
}

private static T Deserialize<T>(JsonElement element)
{
// If we were able to get the default options used by KubernetesJson,
// deserialize directly, it's going to be faster.
if (KubernetesJsonOptions.DefaultOptions is { } options)
{
element.Deserialize<T>(options);
}

// Else, fallback to the slower but more reliable method.
return KubernetesJson.Deserialize<T>(element.GetRawText());
}
}
36 changes: 36 additions & 0 deletions src/DotnetKubernetesClient/KubernetesJsonOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Reflection;
using System.Text.Json;
using k8s;

#nullable enable

namespace DotnetKubernetesClient
{
internal static class KubernetesJsonOptions
{
private static readonly Lazy<JsonSerializerOptions?> Lazy = new(GetJsonSerializerOptions);

public static JsonSerializerOptions? DefaultOptions => Lazy.Value;

private static JsonSerializerOptions? GetJsonSerializerOptions()
{
try
{
// Get the default (and private) KubernetesJson options.
var fieldInfo = typeof(KubernetesJson).GetField("JsonSerializerOptions", BindingFlags.Static | BindingFlags.NonPublic);
if (fieldInfo != null
&& fieldInfo.GetValue(null) is JsonSerializerOptions options)
{
return options;
}
}
catch
{
// Ignored
}

return null;
}
}
}
16 changes: 16 additions & 0 deletions tests/DotnetKubernetesClient.Test/KubernetesJsonOptions.Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using FluentAssertions;
using Xunit;

namespace DotnetKubernetesClient.Test
{
public class KubernetesJsonOptionsTest
{
[Fact]
public void DefaultOptions_should_not_return_null()
{
var result = KubernetesJsonOptions.DefaultOptions;

result.Should().NotBe(null);
}
}
}

0 comments on commit 02b4f0b

Please sign in to comment.