Skip to content

Commit

Permalink
Added CancellationToken support for async operations
Browse files Browse the repository at this point in the history
Added the `System.Threading` namespace to several files to enable the use of the `CancellationToken` class. Updated numerous methods across various files and namespaces to accept a `CancellationToken` parameter, allowing for cancellation of asynchronous operations. This includes methods in `IResourceController.cs`, `ResourceControllerBase.cs`, `FinalizerManager.cs`, `IFinalizerManager.cs`, `EntityFilter.cs`, `Neon.Operator.ResourceManager`, `TestDatabaseController.cs`, `TestResourceController.cs`, and others. Also, moved the `Ok()` method and added `StartAsync()` and `StatusModifiedAsync()` methods in `ResourceControllerBase.cs`.
  • Loading branch information
marcusbooyah committed Apr 1, 2024
1 parent 06d8304 commit 9fed39c
Show file tree
Hide file tree
Showing 24 changed files with 247 additions and 150 deletions.
4 changes: 3 additions & 1 deletion src/Neon.Operator.Core/Controllers/IResourceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.

using System;
using System.Threading;
using System.Threading.Tasks;

using Neon.Operator.Attributes;
Expand All @@ -42,7 +43,8 @@ public interface IResourceController
/// Starts the controller.
/// </summary>
/// <param name="serviceProvider">The <see cref="IServiceProvider"/>.</param>
/// <param name="cancellationToken"></param>
/// <returns>The tracking <see cref="Task"/>.</returns>
public Task StartAsync(IServiceProvider serviceProvider);
public Task StartAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken = default);
}
}
22 changes: 15 additions & 7 deletions src/Neon.Operator.Core/Controllers/IResourceControllerT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.

using System;
using System.Threading;
using System.Threading.Tasks;

using k8s;
Expand Down Expand Up @@ -44,53 +45,60 @@ public interface IResourceController<TEntity> : IResourceController
/// is modified.
/// </summary>
/// <param name="entity">The new or modified resource.</param>
/// <param name="cancellationToken"></param>
/// <returns>
/// A <see cref="ResourceControllerResult"/> indicating the the current event or possibly a new event is
/// to be requeue with a possible delay. <c>null</c> may also bne returned, indicating that
/// the event is not to be requeued.
/// </returns>
public Task<ResourceControllerResult> ReconcileAsync(TEntity entity);
public Task<ResourceControllerResult> ReconcileAsync(TEntity entity, CancellationToken cancellationToken = default);

/// <summary>
/// Called when the status part of a resource has been modified.
/// </summary>
/// <param name="entity">The modified resource.</param>
/// <param name="cancellationToken"></param>
/// <returns>The tracking <see cref="Task"/>.</returns>
public Task StatusModifiedAsync(TEntity entity);
public Task StatusModifiedAsync(TEntity entity, CancellationToken cancellationToken = default);

/// <summary>
/// Called when a resource has been deleted.
/// </summary>
/// <param name="entity">The deleted resource.</param>
/// <param name="cancellationToken"></param>
/// <returns>The tracking <see cref="Task"/>.</returns>
public Task DeletedAsync(TEntity entity);
public Task DeletedAsync(TEntity entity, CancellationToken cancellationToken = default);

/// <summary>
/// Called when the instance has a Leader Elector and this instance has
/// assumed leadership.
/// </summary>
public Task OnPromotionAsync();
/// <param name="cancellationToken"></param>
public Task OnPromotionAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Called when the instance has a Leader Elector this instance has
/// been demoted.
/// </summary>
public Task OnDemotionAsync();
/// <param name="cancellationToken"></param>
public Task OnDemotionAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Called when the instance has a Leader Elector and a new leader has
/// been elected.
/// </summary>
/// <param name="identity">Identifies the new leader.</param>
public Task OnNewLeaderAsync(string identity);
/// <param name="cancellationToken"></param>
public Task OnNewLeaderAsync(string identity, CancellationToken cancellationToken = default);

/// <summary>
/// Called when an exception is thrown. This allows the operator to define the retry policy.
/// </summary>
/// <param name="entity">Specifies the type of the entity.</param>
/// <param name="attempt">Specifies the number of times the operation has been attempted.</param>
/// <param name="exception">Specifies the exception.</param>
/// <param name="cancellationToken"></param>
/// <returns>The <see cref="ErrorPolicyResult"/>.</returns>
public Task<ErrorPolicyResult> ErrorPolicyAsync(TEntity entity, int attempt, Exception exception);
public Task<ErrorPolicyResult> ErrorPolicyAsync(TEntity entity, int attempt, Exception exception, CancellationToken cancellationToken = default);
}
}
49 changes: 25 additions & 24 deletions src/Neon.Operator.Core/Controllers/ResourceControllerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.

using System;
using System.Threading;
using System.Threading.Tasks;

using k8s;
Expand Down Expand Up @@ -59,47 +60,59 @@ public string LeaseName
private string leaseName;

/// <inheritdoc/>
public virtual Task DeletedAsync(T entity)
public virtual Task DeletedAsync(T entity, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}

/// <inheritdoc/>
public virtual Task<ErrorPolicyResult> ErrorPolicyAsync(T entity, int attempt, Exception exception)
public virtual Task<ErrorPolicyResult> ErrorPolicyAsync(T entity, int attempt, Exception exception, CancellationToken cancellationToken = default)
{
return Task.FromResult(new ErrorPolicyResult());
}

/// <summary>
/// Returns <see cref="ResourceControllerResult.Ok()"/>
/// </summary>
/// <returns></returns>
public ResourceControllerResult Ok() => null;

/// <inheritdoc/>
public virtual Task OnDemotionAsync()
public virtual Task OnDemotionAsync(CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}

/// <inheritdoc/>
public virtual Task OnNewLeaderAsync(string identity)
public virtual Task OnNewLeaderAsync(string identity, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}

/// <inheritdoc/>
public virtual Task OnPromotionAsync()
public virtual Task OnPromotionAsync(CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}

/// <inheritdoc/>
public virtual Task<ResourceControllerResult> ReconcileAsync(T entity)
public virtual Task<ResourceControllerResult> ReconcileAsync(T entity, CancellationToken cancellationToken = default)
{
return Task.FromResult<ResourceControllerResult>(null);
}

/// <inheritdoc/>
public virtual Task StartAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}

/// <inheritdoc/>
public virtual Task StatusModifiedAsync(T entity, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}

/// <summary>
/// Returns <see cref="ResourceControllerResult.Ok()"/>
/// </summary>
/// <returns></returns>
public ResourceControllerResult Ok() => null;

/// <summary>
/// Returns <see cref="ResourceControllerResult.RequeueEvent(TimeSpan)"/>
/// </summary>
Expand All @@ -111,17 +124,5 @@ public virtual Task<ResourceControllerResult> ReconcileAsync(T entity)
/// </summary>
/// <returns></returns>
public ResourceControllerResult RequeueEvent(TimeSpan delay, WatchEventType eventType) => ResourceControllerResult.RequeueEvent(delay, eventType);

/// <inheritdoc/>
public virtual Task StartAsync(IServiceProvider serviceProvider)
{
return Task.CompletedTask;
}

/// <inheritdoc/>
public virtual Task StatusModifiedAsync(T entity)
{
return Task.CompletedTask;
}
}
}
4 changes: 3 additions & 1 deletion src/Neon.Operator.Core/Finalizers/IResourceFinalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Threading;
using System.Threading.Tasks;

using k8s;
Expand Down Expand Up @@ -42,7 +43,8 @@ public interface IResourceFinalizer<TEntity>
/// Called when the entity needs to be finalized.
/// </summary>
/// <param name="entity">Specifies the entity being finalized.</param>
/// <param name="cancellationToken"></param>
/// <returns>The tracking <see cref="Task"/>.</returns>
Task FinalizeAsync(TEntity entity);
Task FinalizeAsync(TEntity entity, CancellationToken cancellationToken = default);
}
}
4 changes: 3 additions & 1 deletion src/Neon.Operator.Core/Finalizers/ResourceFinalizerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.

using System;
using System.Threading;
using System.Threading.Tasks;

using k8s;
Expand Down Expand Up @@ -76,8 +77,9 @@ public string Identifier
/// Called when the entity needs to be finalized.
/// </summary>
/// <param name="entity">Specifies the entity being finalized.</param>
/// <param name="cancellationToken"></param>
/// <returns>The tracking <see cref="Task"/>.</returns>
public virtual Task FinalizeAsync(TEntity entity)
public virtual Task FinalizeAsync(TEntity entity, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Threading;
using System.Threading.Tasks;

using k8s;
Expand Down Expand Up @@ -70,7 +71,7 @@ public interface IAdmissionWebhook<TEntity, TResult>
public TResult Create(TEntity newEntity, bool dryRun);

/// <inheritdoc cref="Create"/>
public Task<TResult> CreateAsync(TEntity newEntity, bool dryRun);
public Task<TResult> CreateAsync(TEntity newEntity, bool dryRun, CancellationToken cancellationToken = default);

/// <summary>
/// Operation for <see cref="AdmissionOperations.Update"/>.
Expand All @@ -82,7 +83,7 @@ public interface IAdmissionWebhook<TEntity, TResult>
public TResult Update(TEntity oldEntity, TEntity newEntity, bool dryRun);

/// <inheritdoc cref="Update"/>
public Task<TResult> UpdateAsync(TEntity oldEntity, TEntity newEntity, bool dryRun);
public Task<TResult> UpdateAsync(TEntity oldEntity, TEntity newEntity, bool dryRun, CancellationToken cancellationToken = default);

/// <summary>
/// Operation for <see cref="AdmissionOperations.Delete"/>.
Expand All @@ -93,7 +94,7 @@ public interface IAdmissionWebhook<TEntity, TResult>
public TResult Delete(TEntity oldEntity, bool dryRun);

/// <inheritdoc cref="Delete"/>
public Task<TResult> DeleteAsync(TEntity oldEntity, bool dryRun);
public Task<TResult> DeleteAsync(TEntity oldEntity, bool dryRun, CancellationToken cancellationToken = default);

/// <summary>
/// $todo(marcusbooyah): documentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

using System;
using System.Text.Json.JsonDiffPatch.Diffs.Formatters;
using System.Threading;
using System.Threading.Tasks;

using k8s;
Expand Down Expand Up @@ -55,8 +56,9 @@ V1MutatingWebhookConfiguration WebhookConfiguration(
/// $todo(marcusbooyah): Documentation
/// </summary>
/// <param name="serviceProvider"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task CreateAsync(IServiceProvider serviceProvider);
Task CreateAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken = default);

/// <summary>
/// Gets the webhook endpoint string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.

using System;
using System.Threading;
using System.Threading.Tasks;

using k8s;
Expand Down Expand Up @@ -49,8 +50,9 @@ public V1ValidatingWebhookConfiguration WebhookConfiguration(
/// Used to create the webhook in the Kubernetes API server.
/// </summary>
/// <param name="serviceProvider"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task CreateAsync(IServiceProvider serviceProvider);
Task CreateAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken = default);

/// <summary>
/// Gets the webhook endpoint string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Text.Json.JsonDiffPatch;
using System.Text.Json.JsonDiffPatch.Diffs.Formatters;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;

using k8s;
Expand Down Expand Up @@ -181,7 +182,7 @@ public virtual V1MutatingWebhookConfiguration WebhookConfiguration(


/// <inheritdoc/>
public virtual async Task CreateAsync(IServiceProvider serviceProvider)
public virtual async Task CreateAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken = default)
{
var operatorSettings = serviceProvider.GetRequiredService<OperatorSettings>();
var certManagerOptions = serviceProvider.GetService<CertManagerOptions>();
Expand Down Expand Up @@ -235,7 +236,7 @@ public virtual MutationResult Create(TEntity newEntity, bool dryRun)
}

/// <inheritdoc/>
public async virtual Task<MutationResult> CreateAsync(TEntity newEntity, bool dryRun)
public async virtual Task<MutationResult> CreateAsync(TEntity newEntity, bool dryRun, CancellationToken cancellationToken = default)
{
return await Task.FromResult(MutationResult.NoChanges());
}
Expand All @@ -247,7 +248,7 @@ public virtual MutationResult Update(TEntity oldEntity, TEntity newEntity, bool
}

/// <inheritdoc/>
public async virtual Task<MutationResult> UpdateAsync(TEntity oldEntity, TEntity newEntity, bool dryRun)
public async virtual Task<MutationResult> UpdateAsync(TEntity oldEntity, TEntity newEntity, bool dryRun, CancellationToken cancellationToken = default)
{
return await Task.FromResult(MutationResult.NoChanges());
}
Expand All @@ -259,7 +260,7 @@ public virtual MutationResult Delete(TEntity oldEntity, bool dryRun)
}

/// <inheritdoc/>
public async virtual Task<MutationResult> DeleteAsync(TEntity oldEntity, bool dryRun)
public async virtual Task<MutationResult> DeleteAsync(TEntity oldEntity, bool dryRun, CancellationToken cancellationToken = default)
{
return await Task.FromResult(MutationResult.NoChanges());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

using k8s;
Expand Down Expand Up @@ -146,7 +147,7 @@ public V1ValidatingWebhookConfiguration WebhookConfiguration(
WebhookType IAdmissionWebhook<TEntity, ValidationResult>.WebhookType => throw new NotImplementedException();

/// <inheritdoc/>
public async Task CreateAsync(IServiceProvider serviceProvider)
public async Task CreateAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken = default)
{
var operatorSettings = serviceProvider.GetRequiredService<OperatorSettings>();
var certManagerOptions = serviceProvider.GetService<CertManagerOptions>();
Expand Down Expand Up @@ -200,7 +201,7 @@ public virtual ValidationResult Create(TEntity newEntity, bool dryRun)
}

/// <inheritdoc/>
public virtual async Task<ValidationResult> CreateAsync(TEntity newEntity, bool dryRun)
public virtual async Task<ValidationResult> CreateAsync(TEntity newEntity, bool dryRun, CancellationToken cancellationToken = default)
{
return await Task.FromResult(ValidationResult.Success());
}
Expand All @@ -212,7 +213,7 @@ public virtual ValidationResult Update(TEntity oldEntity, TEntity newEntity, boo
}

/// <inheritdoc/>
public virtual async Task<ValidationResult> UpdateAsync(TEntity oldEntity, TEntity newEntity, bool dryRun)
public virtual async Task<ValidationResult> UpdateAsync(TEntity oldEntity, TEntity newEntity, bool dryRun, CancellationToken cancellationToken = default)
{
return await Task.FromResult(ValidationResult.Success());
}
Expand All @@ -224,7 +225,7 @@ public virtual ValidationResult Delete(TEntity oldEntity, bool dryRun)
}

/// <inheritdoc/>
public virtual async Task<ValidationResult> DeleteAsync(TEntity oldEntity, bool dryRun)
public virtual async Task<ValidationResult> DeleteAsync(TEntity oldEntity, bool dryRun, CancellationToken cancellationToken = default)
{
return await Task.FromResult(ValidationResult.Success());
}
Expand Down
Loading

0 comments on commit 9fed39c

Please sign in to comment.