add async foreach example (#715)

This commit is contained in:
Boshi Lian
2021-09-29 15:34:44 -07:00
committed by GitHub
parent 041f127136
commit 6d90ddb722
2 changed files with 28 additions and 1 deletions

View File

@@ -1,18 +1,36 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using k8s;
using k8s.Models;
using Microsoft.Rest;
namespace watch
{
internal class Program
{
private static void Main(string[] args)
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) =>
{