Skip to content

Commit

Permalink
feat: Add RemoveFinalizerAsync method to finalizer manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
buehler committed Aug 25, 2021
1 parent 4decc49 commit fc79bf5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
26 changes: 25 additions & 1 deletion docs/docs/finalizer.md
Expand Up @@ -44,7 +44,7 @@ as scoped resources in DI.
## Register a finalizer

To attach a finalizer for a resource, call the
<xref:KubeOps.Operator.Finalizer.IFinalizerManager`1.RegisterFinalizerAsync``1(`0)>
<xref:KubeOps.Operator.Finalizer.IFinalizerManager` 1.RegisterFinalizerAsync``1( `0)>
method in the controller during reconciliation.

```csharp
Expand Down Expand Up @@ -86,3 +86,27 @@ public class TestController : IResourceController<V1TestEntity>
}
}
```

## Unregistering a finalizer

When a resource is finalized, the finalizer is removed automatically.
However, if you want to remove a finalizer before a resource is deleted/finalized,
you can use <xref:KubeOps.Operator.Finalizer.IFinalizerManager` 1.RemoveFinalizerAsync``1( `0)>.

```csharp
public class TestController : IResourceController<V1TestEntity>
{
private readonly IFinalizerManager<V1TestEntity> _manager;

public TestController(IFinalizerManager<V1TestEntity> manager)
{
_manager = manager;
}

public async Task<ResourceControllerResult> CreatedAsync(V1TestEntity resource)
{
await _manager.RemoveFinalizerAsync<MyFinalizer>(resource);
return null;
}
}
```
30 changes: 24 additions & 6 deletions src/KubeOps/Operator/Finalizer/FinalizerManager{TEntity}.cs
Expand Up @@ -25,13 +25,9 @@ internal class FinalizerManager<TEntity> : IFinalizerManager<TEntity>
_finalizerInstanceBuilder = finalizerInstanceBuilder;
}

public async Task RegisterFinalizerAsync<TFinalizer>(TEntity entity)
public Task RegisterFinalizerAsync<TFinalizer>(TEntity entity)
where TFinalizer : IResourceFinalizer<TEntity>
{
var finalizer = _finalizerInstanceBuilder.BuildFinalizer<TEntity, TFinalizer>();

await RegisterFinalizerInternalAsync(entity, finalizer);
}
=> RegisterFinalizerInternalAsync(entity, _finalizerInstanceBuilder.BuildFinalizer<TEntity, TFinalizer>());

public async Task RegisterAllFinalizersAsync(TEntity entity)
{
Expand All @@ -40,6 +36,28 @@ public async Task RegisterAllFinalizersAsync(TEntity entity)
.Select(f => RegisterFinalizerInternalAsync(entity, f)));
}

public async Task RemoveFinalizerAsync<TFinalizer>(TEntity entity)
where TFinalizer : IResourceFinalizer<TEntity>
{
var finalizer = _finalizerInstanceBuilder.BuildFinalizer<TEntity, TFinalizer>();

_logger.LogTrace(
@"Try to add finalizer ""{finalizer}"" on entity ""{kind}/{name}"".",
finalizer.Identifier,
entity.Kind,
entity.Name());

if (entity.RemoveFinalizer(finalizer.Identifier))
{
_logger.LogInformation(
@"Removed finalizer ""{finalizer}"" on entity ""{kind}/{name}"".",
finalizer.Identifier,
entity.Kind,
entity.Name());
await _client.Update(entity);
}
}

async Task IFinalizerManager<TEntity>.FinalizeAsync(TEntity entity)
{
var semaphore = new SemaphoreSlim(1);
Expand Down
14 changes: 12 additions & 2 deletions src/KubeOps/Operator/Finalizer/IFinalizerManager{TEntity}.cs
Expand Up @@ -12,7 +12,7 @@ public interface IFinalizerManager<TEntity>
where TEntity : IKubernetesObject<V1ObjectMeta>
{
/// <summary>
/// Register a finalizer for the entity type on the provided instance.
/// Register a <see cref="IResourceFinalizer{TEntity}"/> for the entity type on the provided instance.
/// </summary>
/// <param name="entity">The entity that will receive the finalizer.</param>
/// <typeparam name="TFinalizer">The type of the finalizer.</typeparam>
Expand All @@ -21,12 +21,22 @@ Task RegisterFinalizerAsync<TFinalizer>(TEntity entity)
where TFinalizer : IResourceFinalizer<TEntity>;

/// <summary>
/// Register all known finalizers for the entity type on the provided instance.
/// Register all known <see cref="IResourceFinalizer{TEntity}"/> for the entity type on the provided instance.
/// </summary>
/// <param name="entity">The entity that will receive the finalizers.</param>
/// <returns>A task when the operation is done.</returns>
Task RegisterAllFinalizersAsync(TEntity entity);

/// <summary>
/// Remove a given <see cref="IResourceFinalizer{TEntity}"/> from an entity, if
/// it is registered.
/// </summary>
/// <param name="entity">The entity that should have the finalizer removed.</param>
/// <typeparam name="TFinalizer">The type of the finalizer.</typeparam>
/// <returns>A task when the operation is done.</returns>
Task RemoveFinalizerAsync<TFinalizer>(TEntity entity)
where TFinalizer : IResourceFinalizer<TEntity>;

internal Task FinalizeAsync(TEntity entity);
}
}

0 comments on commit fc79bf5

Please sign in to comment.