public ValueTask WatchNamespaceChanges(IKubernetesWatcher <Namespace> watcher, CancellationToken cancellationToken = default) { if (watcher == null) { throw new ArgumentNullException(nameof(watcher)); } return(WatchObjectChangesAsync(NamespaceUrls.BaseUrl, cancellationToken, watcher)); }
public ValueTask WatchCustomResourceDefinitionChanges(IKubernetesWatcher <CustomResourceDefinition> watcher, CancellationToken cancellationToken = default) { if (watcher == null) { throw new ArgumentNullException(nameof(watcher)); } return(WatchObjectChangesAsync(CustomResourceDefinitionUrls.BaseUrl, cancellationToken, watcher)); }
public ValueTask WatchCustomObjectChanges <T>(string objectPluralName, IKubernetesWatcher <T> watcher, string version = Version1, CancellationToken cancellationToken = default) where T : CustomObject, new() { if (string.IsNullOrWhiteSpace(objectPluralName)) { throw new ArgumentNullException(nameof(objectPluralName)); } if (watcher == null) { throw new ArgumentNullException(nameof(watcher)); } if (string.IsNullOrWhiteSpace(version)) { throw new ArgumentNullException(nameof(version)); } return(WatchObjectChangesAsync(CustomObjectUrls.BaseUrl(version, this.options.Namespace, objectPluralName), cancellationToken, watcher)); }
public async ValueTask WatchObjectChangesAsync <T>(string url, CancellationToken cancellationToken, IKubernetesWatcher <T> watcher) where T : new() { if (string.IsNullOrWhiteSpace(url)) { throw new ArgumentNullException(nameof(url)); } if (watcher == null) { throw new ArgumentNullException(nameof(watcher)); } using (var stream = await StreamAsync(url)) { using (cancellationToken.Register(() => stream.Dispose())) { using (var streamReader = new StreamReader(stream)) { try { var line = default(string); while ((line = await streamReader.ReadLineAsync().ConfigureAwait(false)) != null) { var ms = new MemoryStream(Encoding.UTF8.GetBytes(line)); var item = await JsonSerializer.DeserializeAsync <Watch <T> >(ms, this.serializerOptions); if (item != null) { await watcher.Change(item).ConfigureAwait(false); } } } catch (IOException) { } // TODO: Check if this happens always or only at tests catch (ObjectDisposedException) { // If cancellation signs the end of reading operation we need to swallow it } catch (OperationCanceledException) { // If cancellation signs the end of reading operation we need to swallow it } catch (Exception ex) { await watcher.Error(ex).ConfigureAwait(false); } } } } }
public async Task WatchObjectChangesAsync <T>(string url, CancellationToken cancellationToken, IKubernetesWatcher <T> watcher) where T : new() { if (string.IsNullOrWhiteSpace(url)) { throw new ArgumentNullException(nameof(url)); } if (watcher == null) { throw new ArgumentNullException(nameof(watcher)); } using (var stream = await StreamAsync(url)) { using (cancellationToken.Register(() => stream.Dispose())) { using (var streamReader = new StreamReader(stream)) { try { var line = default(string); while ((line = await streamReader.ReadLineAsync().ConfigureAwait(false)) != null) { var item = JsonConvert.DeserializeObject <Watch <T> >(line); if (item != null) { await watcher.Change(item).ConfigureAwait(false); } } } catch (ObjectDisposedException) { // If cancellation signs the end of reading operation we need to swallow it } catch (OperationCanceledException) { // If cancellation signs the end of reading operation we need to swallow it } catch (Exception ex) { await watcher.Error(ex).ConfigureAwait(false); } } } } }