Skip to content

Commit e18ec35

Browse files
fix patch example. (#517)
1 parent 19854da commit e18ec35

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

examples/patch/Program.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
43
using k8s;
54
using k8s.Models;
6-
using Microsoft.AspNetCore.JsonPatch;
75

86
namespace patch
97
{
@@ -16,21 +14,25 @@ private static void Main(string[] args)
1614
Console.WriteLine("Starting Request!");
1715

1816
var pod = client.ListNamespacedPod("default").Items.First();
19-
2017
var name = pod.Metadata.Name;
2118
PrintLabels(pod);
2219

23-
var newlabels = new Dictionary<string, string>(pod.Metadata.Labels) { ["test"] = "test" };
24-
var patch = new JsonPatchDocument<V1Pod>();
25-
patch.Replace(e => e.Metadata.Labels, newlabels);
26-
client.PatchNamespacedPod(new V1Patch(patch), name, "default");
20+
var patchStr = @"
21+
{
22+
""metadata"": {
23+
""labels"": {
24+
""test"": ""test""
25+
}
26+
}
27+
}";
2728

29+
client.PatchNamespacedPod(new V1Patch(patchStr, V1Patch.PatchType.MergePatch), name, "default");
2830
PrintLabels(client.ReadNamespacedPod(name, "default"));
2931
}
3032

3133
private static void PrintLabels(V1Pod pod)
3234
{
33-
Console.WriteLine($"Lables: for {pod.Metadata.Name}");
35+
Console.WriteLine($"Labels: for {pod.Metadata.Name}");
3436
foreach (var (k, v) in pod.Metadata.Labels)
3537
{
3638
Console.WriteLine($"{k} : {v}");

src/KubernetesClient/V1Patch.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,36 @@ namespace k8s.Models
77
[JsonConverter(typeof(V1PathJsonConverter))]
88
public partial class V1Patch
99
{
10-
public enum PathType
10+
public enum PatchType
1111
{
1212
JsonPatch,
1313
MergePatch,
1414
StrategicMergePatch,
1515
}
1616

17-
public PathType Type { get; private set; }
17+
public PatchType Type { get; private set; }
1818

1919
public V1Patch(IJsonPatchDocument jsonPatch)
2020
: this((object)jsonPatch)
2121
{
2222
}
2323

24+
public V1Patch(String body, PatchType type)
25+
: this(body)
26+
{
27+
this.Type = type;
28+
}
29+
2430
partial void CustomInit()
2531
{
2632
if (Content is IJsonPatchDocument)
2733
{
28-
Type = PathType.JsonPatch;
34+
Type = PatchType.JsonPatch;
35+
return;
36+
}
37+
38+
if (Content is String)
39+
{
2940
return;
3041
}
3142

src/KubernetesClient/V1PathJsonConverter.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ internal class V1PathJsonConverter : JsonConverter
77
{
88
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
99
{
10+
var content = (value as V1Patch)?.Content;
11+
if (content is String || content is string)
12+
{
13+
writer.WriteRaw((string)content);
14+
return;
15+
}
16+
1017
serializer.Serialize(writer, (value as V1Patch)?.Content);
1118
}
1219

0 commit comments

Comments
 (0)