Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions examples/patch/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using k8s;
using k8s.Models;
using Microsoft.AspNetCore.JsonPatch;

namespace patch
{
Expand All @@ -16,21 +14,25 @@ private static void Main(string[] args)
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}");
Expand Down
17 changes: 14 additions & 3 deletions src/KubernetesClient/V1Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions src/KubernetesClient/V1PathJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ internal class V1PathJsonConverter : JsonConverter
{
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);
}

Expand Down