2018-09-27 10:50:39 -07:00
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
2021-09-29 15:34:44 -07:00
|
|
|
using System.Threading.Tasks;
|
2018-09-27 10:50:39 -07:00
|
|
|
using k8s;
|
|
|
|
|
using k8s.Models;
|
2021-09-29 15:34:44 -07:00
|
|
|
using Microsoft.Rest;
|
2018-09-27 10:50:39 -07:00
|
|
|
|
|
|
|
|
namespace watch
|
|
|
|
|
{
|
|
|
|
|
internal class Program
|
|
|
|
|
{
|
2021-09-29 15:34:44 -07:00
|
|
|
private async static Task Main(string[] args)
|
2018-09-27 10:50:39 -07:00
|
|
|
{
|
|
|
|
|
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
|
|
|
|
|
|
|
|
|
IKubernetes client = new Kubernetes(config);
|
|
|
|
|
|
2021-09-29 15:34:44 -07:00
|
|
|
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)
|
|
|
|
|
{
|
2019-10-22 16:02:13 -07:00
|
|
|
var podlistResp = client.ListNamespacedPodWithHttpMessagesAsync("default", watch: true);
|
|
|
|
|
using (podlistResp.Watch<V1Pod, V1PodList>((type, item) =>
|
2018-09-27 10:50:39 -07:00
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|