-
-
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.
- Loading branch information
1 parent
98c6f3f
commit 2f29822
Showing
3 changed files
with
10 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