* first lib model * add missing files * happy test * add vanilla rest for extend * fix new url pattern * address comments * add v to tag * bump ver * add missing file when ren * support multi pkg * fix gh action * fix env var * ren title * use gh action to set ver * remove unused * remove unused
51 lines
1.8 KiB
C#
51 lines
1.8 KiB
C#
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using k8s;
|
|
using k8s.Models;
|
|
|
|
namespace watch
|
|
{
|
|
internal class Program
|
|
{
|
|
private async static Task Main(string[] args)
|
|
{
|
|
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
|
|
|
IKubernetes client = new Kubernetes(config);
|
|
|
|
var podlistResp = client.ListNamespacedPodWithHttpMessagesAsync("default", watch: true);
|
|
// C# 8 required https://docs.microsoft.com/en-us/archive/msdn-magazine/2019/november/csharp-iterating-with-async-enumerables-in-csharp-8
|
|
await foreach (var (type, item) in podlistResp.WatchAsync<V1Pod, V1PodList>())
|
|
{
|
|
Console.WriteLine("==on watch event==");
|
|
Console.WriteLine(type);
|
|
Console.WriteLine(item.Metadata.Name);
|
|
Console.WriteLine("==on watch event==");
|
|
}
|
|
|
|
// uncomment if you prefer callback api
|
|
// WatchUsingCallback(client);
|
|
}
|
|
|
|
private static void WatchUsingCallback(IKubernetes client)
|
|
{
|
|
var podlistResp = client.ListNamespacedPodWithHttpMessagesAsync("default", watch: true);
|
|
using (podlistResp.Watch<V1Pod, V1PodList>((type, item) =>
|
|
{
|
|
Console.WriteLine("==on watch event==");
|
|
Console.WriteLine(type);
|
|
Console.WriteLine(item.Metadata.Name);
|
|
Console.WriteLine("==on watch event==");
|
|
}))
|
|
{
|
|
Console.WriteLine("press ctrl + c to stop watching");
|
|
|
|
var ctrlc = new ManualResetEventSlim(false);
|
|
Console.CancelKeyPress += (sender, eventArgs) => ctrlc.Set();
|
|
ctrlc.Wait();
|
|
}
|
|
}
|
|
}
|
|
}
|