-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use updated/faster JsonDiffPatch library (#601)
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
1 parent
7812f42
commit 00d67cd
Showing
3 changed files
with
18 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters