Skip to content

Commit 44bdb3b

Browse files
committed
Add a WatchAsync method.
1 parent e00f67a commit 44bdb3b

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

examples/watch/Program.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ private static void Main(string[] args)
1313

1414
IKubernetes client = new Kubernetes(config);
1515

16-
var podlistResp = client.ListNamespacedPodWithHttpMessagesAsync("default", watch: true).Result;
17-
using (podlistResp.Watch<V1Pod>((type, item) =>
16+
var podlistResp = client.ListNamespacedPodWithHttpMessagesAsync("tester", watch: true);
17+
var watchTask = podlistResp.WatchAsync<V1Pod, V1PodList>((type, item) =>
1818
{
1919
Console.WriteLine("==on watch event==");
2020
Console.WriteLine(type);
2121
Console.WriteLine(item.Metadata.Name);
2222
Console.WriteLine("==on watch event==");
23-
}))
24-
{
25-
Console.WriteLine("press ctrl + c to stop watching");
23+
});
24+
Console.WriteLine("press ctrl + c to stop watching");
25+
26+
var ctrlc = new ManualResetEventSlim(false);
27+
Console.CancelKeyPress += (sender, eventArgs) => ctrlc.Set();
28+
ctrlc.Wait();
2629

27-
var ctrlc = new ManualResetEventSlim(false);
28-
Console.CancelKeyPress += (sender, eventArgs) => ctrlc.Set();
29-
ctrlc.Wait();
30-
}
30+
watchTask.Result.Dispose();
3131
}
3232
}
3333
}

src/KubernetesClient/Watcher.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,17 @@ public static Watcher<T> Watch<T>(this HttpOperationResponse<T> response,
185185
{
186186
return Watch((HttpOperationResponse)response, onEvent, onError, onClosed);
187187
}
188+
189+
public static async Task<Watcher<T>> WatchAsync<T, L>(this Task<HttpOperationResponse<L>> responseTask,
190+
Action<WatchEventType, T> onEvent,
191+
Action<Exception> onError = null,
192+
Action onClosed = null)
193+
{
194+
var response = await responseTask;
195+
196+
return Watch(response, onEvent, onError, onClosed);
197+
}
198+
199+
188200
}
189201
}

0 commit comments

Comments
 (0)