File tree Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change 1
1
using System ;
2
2
using System . Threading ;
3
+ using System . Threading . Tasks ;
3
4
using k8s ;
4
5
using k8s . Models ;
6
+ using Microsoft . Rest ;
5
7
6
8
namespace watch
7
9
{
8
10
internal class Program
9
11
{
10
- private static void Main ( string [ ] args )
12
+ private async static Task Main ( string [ ] args )
11
13
{
12
14
var config = KubernetesClientConfiguration . BuildConfigFromConfigFile ( ) ;
13
15
14
16
IKubernetes client = new Kubernetes ( config ) ;
15
17
18
+ var podlistResp = client . ListNamespacedPodWithHttpMessagesAsync ( "default" , watch : true ) ;
19
+ // C# 8 required https://docs.microsoft.com/en-us/archive/msdn-magazine/2019/november/csharp-iterating-with-async-enumerables-in-csharp-8
20
+ await foreach ( var ( type , item ) in podlistResp . WatchAsync < V1Pod , V1PodList > ( ) )
21
+ {
22
+ Console . WriteLine ( "==on watch event==" ) ;
23
+ Console . WriteLine ( type ) ;
24
+ Console . WriteLine ( item . Metadata . Name ) ;
25
+ Console . WriteLine ( "==on watch event==" ) ;
26
+ }
27
+
28
+ // uncomment if you prefer callback api
29
+ // WatchUsingCallback(client);
30
+ }
31
+
32
+ private static void WatchUsingCallback ( IKubernetes client )
33
+ {
16
34
var podlistResp = client . ListNamespacedPodWithHttpMessagesAsync ( "default" , watch : true ) ;
17
35
using ( podlistResp . Watch < V1Pod , V1PodList > ( ( type , item ) =>
18
36
{
Original file line number Diff line number Diff line change @@ -66,6 +66,15 @@ public static Watcher<T> Watch<T, L>(
66
66
return Watch ( Task . FromResult ( response ) , onEvent , onError , onClosed ) ;
67
67
}
68
68
69
+ /// <summary>
70
+ /// create an IAsyncEnumerable from a call to api server with watch=true
71
+ /// see https://docs.microsoft.com/en-us/archive/msdn-magazine/2019/november/csharp-iterating-with-async-enumerables-in-csharp-8
72
+ /// </summary>
73
+ /// <typeparam name="T">type of the event object</typeparam>
74
+ /// <typeparam name="L">type of the HttpOperationResponse object</typeparam>
75
+ /// <param name="responseTask">the api response</param>
76
+ /// <param name="onError">a callbak when any exception was caught during watching</param>
77
+ /// <returns>IAsyncEnumerable of watch events</returns>
69
78
public static IAsyncEnumerable < ( WatchEventType , T ) > WatchAsync < T , L > (
70
79
this Task < HttpOperationResponse < L > > responseTask ,
71
80
Action < Exception > onError = null )
You can’t perform that action at this time.
0 commit comments