add patch example (#189)

* add patch example

* fix typo
This commit is contained in:
Boshi Lian
2018-07-09 21:50:39 +08:00
committed by Brendan Burns
parent 3f69820739
commit d01446718d
3 changed files with 71 additions and 0 deletions

44
examples/patch/Program.cs Normal file
View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using k8s;
using k8s.Models;
using Microsoft.AspNetCore.JsonPatch;
namespace patch
{
internal class Program
{
private static void Main(string[] args)
{
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
IKubernetes client = new Kubernetes(config);
Console.WriteLine("Starting Request!");
var pod = client.ListNamespacedPod("default").Items.First();
var name = pod.Metadata.Name;
PrintLabels(pod);
var newlables = new Dictionary<string, string>(pod.Metadata.Labels)
{
["test"] = "test"
};
var patch = new JsonPatchDocument<V1Pod>();
patch.Replace(e => e.Metadata.Labels, newlables);
client.PatchNamespacedPod(new V1Patch(patch), name, "default");
PrintLabels(client.ReadNamespacedPod(name, "default"));
}
private static void PrintLabels(V1Pod pod)
{
Console.WriteLine($"Lables: for {pod.Metadata.Name}");
foreach (var (k, v) in pod.Metadata.Labels)
{
Console.WriteLine($"{k} : {v}");
}
Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=");
}
}
}

View File

@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\KubernetesClient\KubernetesClient.csproj" />
</ItemGroup>
</Project>