예제 #1
0
        private async Task DeleteObjectAsync <T>(
            T value,
            DeleteObjectAsyncDelegate deleteAction,
            WatchObjectAsyncDelegate <T> watchAction,
            TimeSpan timeout,
            CancellationToken cancellationToken)
            where T : IKubernetesObject <V1ObjectMeta>
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            CancellationTokenSource cts = new CancellationTokenSource();

            cancellationToken.Register(cts.Cancel);

            var watchTask = watchAction(
                value,
                onEvent: (type, updatedValue) =>
            {
                value = updatedValue;

                if (type == WatchEventType.Deleted)
                {
                    return(Task.FromResult(WatchResult.Stop));
                }

                return(Task.FromResult(WatchResult.Continue));
            },
                cts.Token);

            await deleteAction(
                value.Metadata.Name,
                cancellationToken : cancellationToken).ConfigureAwait(false);

            if (await Task.WhenAny(watchTask, Task.Delay(timeout)).ConfigureAwait(false) != watchTask)
            {
                cts.Cancel();
                throw new KubernetesException($"The {value.Kind} '{value.Metadata.Name}' was not deleted within a timeout of {timeout.TotalSeconds} seconds.");
            }

            var result = await watchTask.ConfigureAwait(false);

            if (result != WatchExitReason.ClientDisconnected)
            {
                throw new KubernetesException($"The API server unexpectedly closed the connection while watching {value.Kind} '{value.Metadata.Name}'.");
            }
        }
예제 #2
0
 /// <summary>
 /// Asynchronously deletes a namespaced object, and waits for the delete operation
 /// to complete.
 /// </summary>
 /// <typeparam name="T">
 /// The type of the Kubernetes object to delete.
 /// </typeparam>
 /// <param name="value">
 /// The object to delete.
 /// </param>
 /// <param name="deleteAction">
 /// A delegate to a method which schedules the deletion.
 /// </param>
 /// <param name="watchAction">
 /// A delegate to a method which creates a watcher for the object.
 /// Used to monitor the progress of the delete operation.
 /// </param>
 /// <param name="timeout">
 /// The amount of time to wait for the object to be deleted.
 /// </param>
 /// <param name="cancellationToken">
 /// A <see cref="CancellationToken"/> which can be used to cancel the
 /// asynchronous operation.
 /// </param>
 /// <returns>
 /// A <see cref="Task"/> representing the asynchronous operation.
 /// </returns>
 public Task DeleteNamespacedObjectAsync <T>(
     T value,
     DeleteNamespacedObjectAsyncDelegate <T> deleteAction,
     WatchObjectAsyncDelegate <T> watchAction,
     TimeSpan timeout,
     CancellationToken cancellationToken)
     where T : IKubernetesObject <V1ObjectMeta>
 {
     return(this.DeleteNamespacedObjectAsync(
                value,
                options: null,
                deleteAction,
                watchAction,
                timeout,
                cancellationToken));
 }