Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use updated/faster JsonDiffPatch library #601

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 1 addition & 1 deletion src/KubeOps/KubeOps.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

<ItemGroup>
<PackageReference Include="CompareNETObjects" Version="4.79.0" />
<PackageReference Include="JsonDiffPatch" Version="2.0.61" />
<PackageReference Include="Localtunnel" Version="1.0.5" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.0.2" />
<PackageReference Include="McMaster.Extensions.Hosting.CommandLine" Version="4.0.2" />
Expand All @@ -29,6 +28,7 @@
<PackageReference Include="prometheus-net.AspNetCore.HealthChecks" Version="7.0.0" />
<PackageReference Include="SimpleBase" Version="4.0.0" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
<PackageReference Include="SystemTextJson.JsonDiffPatch" Version="1.3.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
Expand Down
16 changes: 8 additions & 8 deletions src/KubeOps/Operator/Webhooks/KubernetesJsonDiffer.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
using JsonDiffPatch;
using System.Text.Json.JsonDiffPatch;
using System.Text.Json.JsonDiffPatch.Diffs.Formatters;
using System.Text.Json.Nodes;
using k8s;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace KubeOps.Operator.Webhooks;

internal static class KubernetesJsonDiffer
{
private static readonly JsonDiffer JsonDiffer = new();
private static readonly JsonPatchDeltaFormatter Formatter = new();

public static PatchDocument DiffObjects(object? from, object? to)
public static JsonNode? DiffObjects(object? from, object? to)
{
buehler marked this conversation as resolved.
Show resolved Hide resolved
var fromToken = GetJToken(from);
var toToken = GetJToken(to);

return JsonDiffer.Diff(fromToken, toToken, false);
return fromToken.Diff(toToken, Formatter);
}

private static JToken GetJToken(object? o)
private static JsonNode? GetJToken(object? o)
{
// Use the K8s Serializer to ensure we match their naming conventions
// (and handle object conversions correctly).
var json = KubernetesJson.Serialize(o);
return JToken.ReadFrom(new JsonTextReader(new StringReader(json)));
return JsonNode.Parse(json);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using FluentAssertions;
using k8s.Models;
using KubeOps.Operator.Webhooks;
using Newtonsoft.Json;
using Xunit;

namespace KubeOps.Test.Operator.Webhook;
Expand All @@ -17,7 +16,7 @@ public void When_diffing_objects_then_kubernetes_naming_conventions_should_be_us
var result = KubernetesJsonDiffer.DiffObjects(left, right);

// Should be all lowercase.
result.ToString(Formatting.None)
result.ToJsonString()
.Should()
.Be("[{\"op\":\"replace\",\"path\":\"/status/reason\",\"value\":\"bar\"}]");
}
Expand Down
Loading