Skip to content

Commit

Permalink
feat: use updated/faster JsonDiffPatch library (#601)
Browse files Browse the repository at this point in the history
The main motivation behind this change is to remove the deprecated
transitive dependency `NetCoreApp 1.1` from `JsonDiffPatch`. This is
causing validation issues in built images that are run through Aqua
Trivy vulnerability scanner.

A nice side-effect is that the diff should be much faster as it uses
`System.Text.Json` instead of `Newtonsoft`.

Functionally, there should be no impact as both libraries adhere to `RFC
6902`.
  • Loading branch information
robertcoltheart authored Sep 19, 2023
1 parent 7812f42 commit 00d67cd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
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)
{
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);
}
}
11 changes: 9 additions & 2 deletions tests/KubeOps.Test/Operator/Webhook/KubernetesJsonDiffer.Test.cs
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,8 +16,16 @@ 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\"}]");
}

[Fact]
public void When_diffing_null_objects_then_no_errors_should_be_thrown()
{
var result = KubernetesJsonDiffer.DiffObjects(null, null);

Assert.NotNull(result);
}
}

0 comments on commit 00d67cd

Please sign in to comment.