fix patch example. (#517)
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using k8s;
|
||||
using k8s.Models;
|
||||
using Microsoft.AspNetCore.JsonPatch;
|
||||
|
||||
namespace patch
|
||||
{
|
||||
@@ -16,21 +14,25 @@ namespace patch
|
||||
Console.WriteLine("Starting Request!");
|
||||
|
||||
var pod = client.ListNamespacedPod("default").Items.First();
|
||||
|
||||
var name = pod.Metadata.Name;
|
||||
PrintLabels(pod);
|
||||
|
||||
var newlabels = new Dictionary<string, string>(pod.Metadata.Labels) { ["test"] = "test" };
|
||||
var patch = new JsonPatchDocument<V1Pod>();
|
||||
patch.Replace(e => e.Metadata.Labels, newlabels);
|
||||
client.PatchNamespacedPod(new V1Patch(patch), name, "default");
|
||||
var patchStr = @"
|
||||
{
|
||||
""metadata"": {
|
||||
""labels"": {
|
||||
""test"": ""test""
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
client.PatchNamespacedPod(new V1Patch(patchStr, V1Patch.PatchType.MergePatch), name, "default");
|
||||
PrintLabels(client.ReadNamespacedPod(name, "default"));
|
||||
}
|
||||
|
||||
private static void PrintLabels(V1Pod pod)
|
||||
{
|
||||
Console.WriteLine($"Lables: for {pod.Metadata.Name}");
|
||||
Console.WriteLine($"Labels: for {pod.Metadata.Name}");
|
||||
foreach (var (k, v) in pod.Metadata.Labels)
|
||||
{
|
||||
Console.WriteLine($"{k} : {v}");
|
||||
|
||||
@@ -7,25 +7,36 @@ namespace k8s.Models
|
||||
[JsonConverter(typeof(V1PathJsonConverter))]
|
||||
public partial class V1Patch
|
||||
{
|
||||
public enum PathType
|
||||
public enum PatchType
|
||||
{
|
||||
JsonPatch,
|
||||
MergePatch,
|
||||
StrategicMergePatch,
|
||||
}
|
||||
|
||||
public PathType Type { get; private set; }
|
||||
public PatchType Type { get; private set; }
|
||||
|
||||
public V1Patch(IJsonPatchDocument jsonPatch)
|
||||
: this((object)jsonPatch)
|
||||
{
|
||||
}
|
||||
|
||||
public V1Patch(String body, PatchType type)
|
||||
: this(body)
|
||||
{
|
||||
this.Type = type;
|
||||
}
|
||||
|
||||
partial void CustomInit()
|
||||
{
|
||||
if (Content is IJsonPatchDocument)
|
||||
{
|
||||
Type = PathType.JsonPatch;
|
||||
Type = PatchType.JsonPatch;
|
||||
return;
|
||||
}
|
||||
|
||||
if (Content is String)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,13 @@ namespace k8s.Models
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
var content = (value as V1Patch)?.Content;
|
||||
if (content is String || content is string)
|
||||
{
|
||||
writer.WriteRaw((string)content);
|
||||
return;
|
||||
}
|
||||
|
||||
serializer.Serialize(writer, (value as V1Patch)?.Content);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user