Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c3b3a08

Browse files
authoredNov 5, 2024··
Enable JSON Patch in AOT (#1588)
* Special case serialization of V1Patch * Add AOT JSON Patch Example
1 parent 541abb0 commit c3b3a08

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
 

‎examples/patch-aot/Program.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using k8s;
2+
using k8s.Models;
3+
4+
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
5+
IKubernetes client = new Kubernetes(config);
6+
Console.WriteLine("Starting Request!");
7+
8+
var pod = client.CoreV1.ListNamespacedPod("default").Items.First();
9+
var name = pod.Metadata.Name;
10+
PrintLabels(pod);
11+
12+
var patchStr = @"
13+
{
14+
""metadata"": {
15+
""labels"": {
16+
""test"": ""test""
17+
}
18+
}
19+
}";
20+
21+
client.CoreV1.PatchNamespacedPod(new V1Patch(patchStr, V1Patch.PatchType.MergePatch), name, "default");
22+
PrintLabels(client.CoreV1.ReadNamespacedPod(name, "default"));
23+
24+
static void PrintLabels(V1Pod pod)
25+
{
26+
Console.WriteLine($"Labels: for {pod.Metadata.Name}");
27+
foreach (var (k, v) in pod.Metadata.Labels)
28+
{
29+
Console.WriteLine($"{k} : {v}");
30+
}
31+
Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=");
32+
}

‎examples/patch-aot/patch-aot.csproj

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<ImplicitUsings>enable</ImplicitUsings>
5+
<Nullable>enable</Nullable>
6+
<PublishAot>true</PublishAot>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\src\KubernetesClient.Aot\KubernetesClient.Aot.csproj"/>
10+
</ItemGroup>
11+
</Project>

‎src/KubernetesClient.Aot/KubernetesJson.cs

+5
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public static TValue Deserialize<TValue>(Stream json, JsonSerializerOptions json
9191

9292
public static string Serialize(object value, JsonSerializerOptions jsonSerializerOptions = null)
9393
{
94+
if (value is V1Patch { Content: string jsonValue })
95+
{
96+
return jsonValue;
97+
}
98+
9499
var info = SourceGenerationContext.Default.GetTypeInfo(value.GetType());
95100
return JsonSerializer.Serialize(value, info);
96101
}

0 commit comments

Comments
 (0)
Please sign in to comment.