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