From 05ed1f35966e625d1c45f246df303d5cb03a651f Mon Sep 17 00:00:00 2001 From: Hugo Woodiwiss Date: Wed, 18 Sep 2024 20:53:07 +0100 Subject: [PATCH 1/2] Special case serialization of V1Patch --- src/KubernetesClient.Aot/KubernetesJson.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/KubernetesClient.Aot/KubernetesJson.cs b/src/KubernetesClient.Aot/KubernetesJson.cs index cfa69ec0..f6285ccc 100644 --- a/src/KubernetesClient.Aot/KubernetesJson.cs +++ b/src/KubernetesClient.Aot/KubernetesJson.cs @@ -91,6 +91,11 @@ public static TValue Deserialize(Stream json, JsonSerializerOptions json public static string Serialize(object value, JsonSerializerOptions jsonSerializerOptions = null) { + if (value is V1Patch { Content: string jsonValue }) + { + return jsonValue; + } + var info = SourceGenerationContext.Default.GetTypeInfo(value.GetType()); return JsonSerializer.Serialize(value, info); } From 1eb62a85893919df989b38b7df0ec8e1aea93993 Mon Sep 17 00:00:00 2001 From: Hugo Woodiwiss Date: Wed, 23 Oct 2024 08:51:15 +0100 Subject: [PATCH 2/2] Add AOT JSON Patch Example --- examples/patch-aot/Program.cs | 32 +++++++++++++++++++++++++++++ examples/patch-aot/patch-aot.csproj | 11 ++++++++++ 2 files changed, 43 insertions(+) create mode 100644 examples/patch-aot/Program.cs create mode 100644 examples/patch-aot/patch-aot.csproj diff --git a/examples/patch-aot/Program.cs b/examples/patch-aot/Program.cs new file mode 100644 index 00000000..06a693dd --- /dev/null +++ b/examples/patch-aot/Program.cs @@ -0,0 +1,32 @@ +using k8s; +using k8s.Models; + +var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); +IKubernetes client = new Kubernetes(config); +Console.WriteLine("Starting Request!"); + +var pod = client.CoreV1.ListNamespacedPod("default").Items.First(); +var name = pod.Metadata.Name; +PrintLabels(pod); + +var patchStr = @" +{ + ""metadata"": { + ""labels"": { + ""test"": ""test"" + } + } +}"; + +client.CoreV1.PatchNamespacedPod(new V1Patch(patchStr, V1Patch.PatchType.MergePatch), name, "default"); +PrintLabels(client.CoreV1.ReadNamespacedPod(name, "default")); + +static void PrintLabels(V1Pod pod) +{ + Console.WriteLine($"Labels: for {pod.Metadata.Name}"); + foreach (var (k, v) in pod.Metadata.Labels) + { + Console.WriteLine($"{k} : {v}"); + } + Console.WriteLine("=-=-=-=-=-=-=-=-=-=-="); +} diff --git a/examples/patch-aot/patch-aot.csproj b/examples/patch-aot/patch-aot.csproj new file mode 100644 index 00000000..c2c80621 --- /dev/null +++ b/examples/patch-aot/patch-aot.csproj @@ -0,0 +1,11 @@ + + + Exe + enable + enable + true + + + + +