-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(finalizer): Rework finalizer in controllers (#625)
This also adds integration tests for the system. (and some todos). BREAKING CHANGE: Finalizers are registered with an identifier now. The identifier is generated by the KubeOps.Generator when used. Finalizers are attached via EntityFinalizerAttacher<> delegates that attach the finalizer to an entity.
- Loading branch information
Showing
40 changed files
with
1,134 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,6 @@ | |
"commands": [ | ||
"docfx" | ||
] | ||
}, | ||
"kubeops.cli": { | ||
"version": "8.0.0-pre.8", | ||
"commands": [ | ||
"kubeops" | ||
] | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,11 @@ | ||
using k8s; | ||
using k8s.Models; | ||
using k8s.Models; | ||
|
||
using KubeOps.Abstractions.Entities; | ||
|
||
namespace Operator.Entities; | ||
|
||
[KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")] | ||
public class V1TestEntity : IKubernetesObject<V1ObjectMeta>, ISpec<V1TestEntitySpec>, IStatus<V1TestEntityStatus> | ||
public class V1TestEntity : CustomKubernetesEntity<V1TestEntitySpec, V1TestEntityStatus> | ||
{ | ||
public required string ApiVersion { get; set; } | ||
|
||
public required string Kind { get; set; } | ||
|
||
public V1ObjectMeta Metadata { get; set; } = new(); | ||
|
||
public V1TestEntitySpec Spec { get; set; } = new(); | ||
|
||
public V1TestEntityStatus Status { get; set; } = new(); | ||
|
||
public override string ToString() => $"Test Entity ({Metadata.Name}): {Spec.Username} ({Spec.Email})"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using KubeOps.Abstractions.Finalizer; | ||
|
||
using Operator.Entities; | ||
|
||
namespace Operator.Finalizer; | ||
|
||
public class FinalizerOne : IEntityFinalizer<V1TestEntity> | ||
{ | ||
public Task FinalizeAsync(V1TestEntity entity) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using KubeOps.Abstractions.Finalizer; | ||
|
||
using Operator.Entities; | ||
|
||
namespace Operator.Finalizer; | ||
|
||
public class FinalizerTwo : IEntityFinalizer<V1TestEntity> | ||
{ | ||
public Task FinalizeAsync(V1TestEntity entity) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"Operator": { | ||
"commandName": "Project" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
todo: | ||
- finalizer | ||
- events | ||
- leadership election | ||
- requeue | ||
- build targets | ||
- other CLI commands | ||
- error handling | ||
- web: webhooks | ||
- docs | ||
- cache? | ||
- try .net 8 AOT? | ||
- client: | ||
// TODO: make all sync calls as well. | ||
// TODO: test list / get call. | ||
// TODO: update list call |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/KubeOps.Abstractions/Finalizer/EntityFinalizerAttacher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using k8s; | ||
using k8s.Models; | ||
|
||
namespace KubeOps.Abstractions.Finalizer; | ||
|
||
/// <summary> | ||
/// <para> | ||
/// Injectable delegate for finalizers. This delegate is used to attach a finalizer | ||
/// with its identifier to an entity. When injected, simply call the delegate with | ||
/// the entity to attach the finalizer. | ||
/// </para> | ||
/// <para> | ||
/// As with other (possibly) mutating calls, use the returned entity for further | ||
/// modification and Kubernetes client interactions, since the resource version | ||
/// is updated each time the entity is modified. | ||
/// </para> | ||
/// </summary> | ||
/// <typeparam name="TImplementation">The type of the entity finalizer.</typeparam> | ||
/// <typeparam name="TEntity">The type of the Kubernetes entity.</typeparam> | ||
/// <param name="entity">The instance of the entity, that the finalizer is attached if needed.</param> | ||
/// <returns>A <see cref="Task"/> that resolves when the finalizer was attached.</returns> | ||
/// <example> | ||
/// <code> | ||
/// [EntityRbac(typeof(V1TestEntity), Verbs = RbacVerb.All)] | ||
/// public class V1TestEntityController : IEntityController<V1TestEntity> | ||
/// { | ||
/// private readonly EntityFinalizerAttacher<FinalizerOne, V1TestEntity> _finalizer1; | ||
/// | ||
/// public V1TestEntityController( | ||
/// EntityFinalizerAttacher<FinalizerOne, V1TestEntity> finalizer1) => _finalizer1 = finalizer1; | ||
/// | ||
/// public async Task ReconcileAsync(V1TestEntity entity) | ||
/// { | ||
/// entity = await _finalizer1(entity); | ||
/// } | ||
/// } | ||
/// </code> | ||
/// </example> | ||
public delegate Task<TEntity> EntityFinalizerAttacher<TImplementation, TEntity>(TEntity entity) | ||
where TImplementation : IEntityFinalizer<TEntity> | ||
where TEntity : IKubernetesObject<V1ObjectMeta>; |
19 changes: 19 additions & 0 deletions
19
src/KubeOps.Abstractions/Finalizer/IEntityFinalizer{TEntity}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using k8s; | ||
using k8s.Models; | ||
|
||
namespace KubeOps.Abstractions.Finalizer; | ||
|
||
/// <summary> | ||
/// Finalizer for an entity. | ||
/// </summary> | ||
/// <typeparam name="TEntity">The type of the entity.</typeparam> | ||
public interface IEntityFinalizer<in TEntity> | ||
where TEntity : IKubernetesObject<V1ObjectMeta> | ||
{ | ||
/// <summary> | ||
/// Finalize an entity that is pending for deletion. | ||
/// </summary> | ||
/// <param name="entity">The kubernetes entity that needs to be finalized.</param> | ||
/// <returns>A task that resolves when the operation is done.</returns> | ||
Task FinalizeAsync(TEntity entity); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"CLI Generate RBAC": { | ||
"commandName": "Project", | ||
"workingDirectory": "$(ProjectDir)", | ||
"commandLineArgs": "gen rbac ../../examples/Operator/Operator.csproj" | ||
}, | ||
"CLI Generate Cert": { | ||
"commandName": "Project", | ||
"workingDirectory": "$(ProjectDir)", | ||
"commandLineArgs": "g cert test-operator" | ||
}, | ||
"CLI Generate CRDs": { | ||
"commandName": "Project", | ||
"workingDirectory": "$(ProjectDir)", | ||
"commandLineArgs": "g c ../../examples/Operator/Operator.csproj" | ||
}, | ||
"CLI Install": { | ||
"commandName": "Project", | ||
"workingDirectory": "$(ProjectDir)", | ||
"commandLineArgs": "i ../../examples/Operator/Operator.csproj -f" | ||
}, | ||
"CLI Uninstall": { | ||
"commandName": "Project", | ||
"workingDirectory": "$(ProjectDir)", | ||
"commandLineArgs": "u ../../examples/Operator/Operator.csproj -f" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.