add async foreach example (#715)
This commit is contained in:
@@ -1,18 +1,36 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using k8s;
|
using k8s;
|
||||||
using k8s.Models;
|
using k8s.Models;
|
||||||
|
using Microsoft.Rest;
|
||||||
|
|
||||||
namespace watch
|
namespace watch
|
||||||
{
|
{
|
||||||
internal class Program
|
internal class Program
|
||||||
{
|
{
|
||||||
private static void Main(string[] args)
|
private async static Task Main(string[] args)
|
||||||
{
|
{
|
||||||
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
||||||
|
|
||||||
IKubernetes client = new Kubernetes(config);
|
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);
|
var podlistResp = client.ListNamespacedPodWithHttpMessagesAsync("default", watch: true);
|
||||||
using (podlistResp.Watch<V1Pod, V1PodList>((type, item) =>
|
using (podlistResp.Watch<V1Pod, V1PodList>((type, item) =>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -66,6 +66,15 @@ namespace k8s
|
|||||||
return Watch(Task.FromResult(response), onEvent, onError, onClosed);
|
return Watch(Task.FromResult(response), onEvent, onError, onClosed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// create an IAsyncEnumerable from a call to api server with watch=true
|
||||||
|
/// see https://docs.microsoft.com/en-us/archive/msdn-magazine/2019/november/csharp-iterating-with-async-enumerables-in-csharp-8
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">type of the event object</typeparam>
|
||||||
|
/// <typeparam name="L">type of the HttpOperationResponse object</typeparam>
|
||||||
|
/// <param name="responseTask">the api response</param>
|
||||||
|
/// <param name="onError">a callbak when any exception was caught during watching</param>
|
||||||
|
/// <returns>IAsyncEnumerable of watch events</returns>
|
||||||
public static IAsyncEnumerable<(WatchEventType, T)> WatchAsync<T, L>(
|
public static IAsyncEnumerable<(WatchEventType, T)> WatchAsync<T, L>(
|
||||||
this Task<HttpOperationResponse<L>> responseTask,
|
this Task<HttpOperationResponse<L>> responseTask,
|
||||||
Action<Exception> onError = null)
|
Action<Exception> onError = null)
|
||||||
|
|||||||
Reference in New Issue
Block a user