diff --git a/src/KubernetesClient/GenericClient.cs b/src/KubernetesClient/GenericClient.cs index e6c48c05d..d8cbebff1 100644 --- a/src/KubernetesClient/GenericClient.cs +++ b/src/KubernetesClient/GenericClient.cs @@ -115,6 +115,34 @@ public async Task ReplaceNamespacedAsync(T obj, string ns, string name, Ca return KubernetesJson.Deserialize(resp.Body.ToString()); } + public IAsyncEnumerable<(WatchEventType, T)> WatchAsync(Action onError = null, CancellationToken cancel = default) + where T : IKubernetesObject + { + var respTask = kubernetes.CustomObjects.ListClusterCustomObjectWithHttpMessagesAsync(group, version, plural, watch: true, cancellationToken: cancel); + return respTask.WatchAsync(); + } + + public IAsyncEnumerable<(WatchEventType, T)> WatchNamespacedAsync(string ns, Action onError = null, CancellationToken cancel = default) + where T : IKubernetesObject + { + var respTask = kubernetes.CustomObjects.ListNamespacedCustomObjectWithHttpMessagesAsync(group, version, ns, plural, watch: true, cancellationToken: cancel); + return respTask.WatchAsync(); + } + + public Watcher Watch(Action onEvent, Action onError = null, Action onClosed = null) + where T : IKubernetesObject + { + var respTask = kubernetes.CustomObjects.ListClusterCustomObjectWithHttpMessagesAsync(group, version, plural, watch: true); + return respTask.Watch(onEvent, onError, onClosed); + } + + public Watcher WatchNamespaced(string ns, Action onEvent, Action onError = null, Action onClosed = null) + where T : IKubernetesObject + { + var respTask = kubernetes.CustomObjects.ListNamespacedCustomObjectWithHttpMessagesAsync(group, version, ns, plural, watch: true); + return respTask.Watch(onEvent, onError, onClosed); + } + public void Dispose() { Dispose(true); diff --git a/src/KubernetesClient/WatcherExt.cs b/src/KubernetesClient/WatcherExt.cs index f4fbd41eb..0ddf302a5 100644 --- a/src/KubernetesClient/WatcherExt.cs +++ b/src/KubernetesClient/WatcherExt.cs @@ -1,6 +1,7 @@ using k8s.Autorest; using k8s.Exceptions; using System.IO; +using System.Threading; using System.Threading.Tasks; namespace k8s @@ -72,12 +73,14 @@ public static Watcher Watch( /// type of the HttpOperationResponse object /// the api response /// a callbak when any exception was caught during watching + /// cancellation token /// IAsyncEnumerable of watch events public static IAsyncEnumerable<(WatchEventType, T)> WatchAsync( this Task> responseTask, - Action onError = null) + Action onError = null, + CancellationToken cancellationToken = default) { - return Watcher.CreateWatchEventEnumerator(MakeStreamReaderCreator(responseTask), onError); + return Watcher.CreateWatchEventEnumerator(MakeStreamReaderCreator(responseTask), onError, cancellationToken); } } }