Skip to content

Commit 5fc23d3

Browse files
authored
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.
1 parent 3a95bd5 commit 5fc23d3

40 files changed

+1134
-71
lines changed

.config/dotnet-tools.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
"commands": [
88
"docfx"
99
]
10-
},
11-
"kubeops.cli": {
12-
"version": "8.0.0-pre.8",
13-
"commands": [
14-
"kubeops"
15-
]
1610
}
1711
}
18-
}
12+
}

README.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,22 @@
77
This is the repository of "KubeOps" - The dotnet Kubernetes Operator SDK.
88

99
The documentation is provided in the code itself (description of the methods and classes)
10-
and each package contains a README with further information/documentation.
11-
12-
Also, there is a `docfx` site that provides further documentation and examples.
13-
You can find it [here](https://buehler.github.io/dotnet-operator-sdk/).
10+
and each package contains a README with further information/documentation. For a more
11+
detailed documentation, head to the [GitHub Pages](https://buehler.github.io/dotnet-operator-sdk/).
1412

1513
## Packages
1614

1715
The following packages exist:
1816

19-
| Package | Description | Version | Pre Version |
20-
|--------------------------------------------------------------|-----------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
21-
| [KubeOps.Abstractions](./src/KubeOps.Abstractions/README.md) | Contains abstractions, attributes, etc. for the SDK | [![Nuget](https://img.shields.io/nuget/v/KubeOps.Abstractions)](https://www.nuget.org/packages/KubeOps.Abstractions/) | [![Nuget](https://img.shields.io/nuget/vpre/KubeOps.Abstractions?label=nuget%20prerelease)](https://www.nuget.org/packages/KubeOps.Abstractions/absoluteLatest) |
22-
| [KubeOps.Cli](./src/KubeOps.Cli/README.md) | CLI Dotnet Tool to generate stuff | [![Nuget](https://img.shields.io/nuget/v/KubeOps.Cli)](https://www.nuget.org/packages/KubeOps.Cli/) | [![Nuget](https://img.shields.io/nuget/vpre/KubeOps.Cli?label=nuget%20prerelease)](https://www.nuget.org/packages/KubeOps.Cli/absoluteLatest) |
23-
| [KubeOps.Generator](./src/KubeOps.Generator/README.md) | Source Generator for the SDK | [![Nuget](https://img.shields.io/nuget/v/KubeOps.Generator)](https://www.nuget.org/packages/KubeOps.Generator/) | [![Nuget](https://img.shields.io/nuget/vpre/KubeOps.Generator?label=nuget%20prerelease)](https://www.nuget.org/packages/KubeOps.Generator/absoluteLatest) |
24-
| [KubeOps.Operator](./src/KubeOps.Operator/README.md) | Main SDK entrypoint to create an operator | [![Nuget](https://img.shields.io/nuget/v/KubeOps.Operator)](https://www.nuget.org/packages/KubeOps.Operator/) | [![Nuget](https://img.shields.io/nuget/vpre/KubeOps.Operator?label=nuget%20prerelease)](https://www.nuget.org/packages/KubeOps.Operator/absoluteLatest) |
25-
| [KubeOps.Operator.Web](./src/KubeOps.Operator.Web/README.md) | Web part of the operator (for webhooks) | [![Nuget](https://img.shields.io/nuget/v/KubeOps.Operator.Web)](https://www.nuget.org/packages/KubeOps.Operator.Web/) | [![Nuget](https://img.shields.io/nuget/vpre/KubeOps.Operator.Web?label=nuget%20prerelease)](https://www.nuget.org/packages/KubeOps.Operator.Web/absoluteLatest) |
26-
| [KubeOps.Transpiler](./src/KubeOps.Transpiler/README.md) | Transpilation helpers for CRDs and RBAC elements | [![Nuget](https://img.shields.io/nuget/v/KubeOps.Transpiler)](https://www.nuget.org/packages/KubeOps.Transpiler/) | [![Nuget](https://img.shields.io/nuget/vpre/KubeOps.Transpiler?label=nuget%20prerelease)](https://www.nuget.org/packages/KubeOps.Transpiler/absoluteLatest) |
17+
| Package | Description | Framework Support | Latest Version |
18+
|----------------------------------------------------------------------|--------------------------------------------------------|---------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
19+
| [KubeOps.Abstractions](./src/KubeOps.Abstractions/README.md) | Contains abstractions, attributes, etc. for the SDK | netstandard2.0 netstandard2.1 net6.0 net7.0 | [![Nuget](https://img.shields.io/nuget/vpre/KubeOps.Abstractions?label=nuget%20prerelease)](https://www.nuget.org/packages/KubeOps.Abstractions/absoluteLatest) |
20+
| [KubeOps.Cli](./src/KubeOps.Cli/README.md) | CLI Dotnet Tool to generate stuff | net7.0 | [![Nuget](https://img.shields.io/nuget/vpre/KubeOps.Cli?label=nuget%20prerelease)](https://www.nuget.org/packages/KubeOps.Cli/absoluteLatest) |
21+
| [KubeOps.Generator](./src/KubeOps.Generator/README.md) | Source Generator for the SDK | netstandard2.0 | [![Nuget](https://img.shields.io/nuget/vpre/KubeOps.Generator?label=nuget%20prerelease)](https://www.nuget.org/packages/KubeOps.Generator/absoluteLatest) |
22+
| [KubeOps.KubernetesClient](./src/KubeOps.KubernetesClient/README.md) | Extended client to communicate with the Kubernetes API | net6.0 net7.0 | [![Nuget](https://img.shields.io/nuget/vpre/KubeOps.KubernetesClient?label=nuget%20prerelease)](https://www.nuget.org/packages/KubeOps.KubernetesClient/absoluteLatest) |
23+
| [KubeOps.Operator](./src/KubeOps.Operator/README.md) | Main SDK entrypoint to create an operator | net6.0 net7.0 | [![Nuget](https://img.shields.io/nuget/vpre/KubeOps.Operator?label=nuget%20prerelease)](https://www.nuget.org/packages/KubeOps.Operator/absoluteLatest) |
24+
| [KubeOps.Operator.Web](./src/KubeOps.Operator.Web/README.md) | Web part of the operator (for webhooks) | net6.0 net7.0 | [![Nuget](https://img.shields.io/nuget/vpre/KubeOps.Operator.Web?label=nuget%20prerelease)](https://www.nuget.org/packages/KubeOps.Operator.Web/absoluteLatest) |
25+
| [KubeOps.Transpiler](./src/KubeOps.Transpiler/README.md) | Transpilation helpers for CRDs and RBAC elements | netstandard2.0 netstandard2.1 net6.0 net7.0 | [![Nuget](https://img.shields.io/nuget/vpre/KubeOps.Transpiler?label=nuget%20prerelease)](https://www.nuget.org/packages/KubeOps.Transpiler/absoluteLatest) |
2726

2827
## Contribution
2928

examples/Operator/Controller/V1TestEntityController.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,46 @@
11
using KubeOps.Abstractions.Controller;
2+
using KubeOps.Abstractions.Finalizer;
23
using KubeOps.Abstractions.Rbac;
4+
using KubeOps.KubernetesClient;
35

46
using Microsoft.Extensions.Logging;
57

68
using Operator.Entities;
9+
using Operator.Finalizer;
710

811
namespace Operator.Controller;
912

1013
[EntityRbac(typeof(V1TestEntity), Verbs = RbacVerb.All)]
1114
public class V1TestEntityController : IEntityController<V1TestEntity>
1215
{
1316
private readonly ILogger<V1TestEntityController> _logger;
17+
private readonly IKubernetesClient<V1TestEntity> _client;
18+
private readonly EntityFinalizerAttacher<FinalizerOne, V1TestEntity> _finalizer1;
19+
private readonly EntityFinalizerAttacher<FinalizerTwo, V1TestEntity> _finalizer2;
1420

15-
public V1TestEntityController(ILogger<V1TestEntityController> logger)
21+
public V1TestEntityController(
22+
ILogger<V1TestEntityController> logger,
23+
IKubernetesClient<V1TestEntity> client,
24+
EntityFinalizerAttacher<FinalizerOne, V1TestEntity> finalizer1,
25+
EntityFinalizerAttacher<FinalizerTwo, V1TestEntity> finalizer2)
1626
{
1727
_logger = logger;
28+
_client = client;
29+
_finalizer1 = finalizer1;
30+
_finalizer2 = finalizer2;
1831
}
1932

20-
public Task ReconcileAsync(V1TestEntity entity)
33+
public async Task ReconcileAsync(V1TestEntity entity)
2134
{
2235
_logger.LogInformation("Reconciling entity {Entity}.", entity);
23-
return Task.CompletedTask;
36+
37+
entity = await _finalizer1(entity);
38+
entity = await _finalizer2(entity);
39+
40+
entity.Status.Status = "Reconciling";
41+
entity = await _client.UpdateStatus(entity);
42+
entity.Status.Status = "Reconciled";
43+
await _client.UpdateStatus(entity);
2444
}
2545

2646
public Task DeletedAsync(V1TestEntity entity)
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
1-
using k8s;
2-
using k8s.Models;
1+
using k8s.Models;
2+
3+
using KubeOps.Abstractions.Entities;
34

45
namespace Operator.Entities;
56

67
[KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")]
7-
public class V1TestEntity : IKubernetesObject<V1ObjectMeta>, ISpec<V1TestEntitySpec>, IStatus<V1TestEntityStatus>
8+
public class V1TestEntity : CustomKubernetesEntity<V1TestEntitySpec, V1TestEntityStatus>
89
{
9-
public required string ApiVersion { get; set; }
10-
11-
public required string Kind { get; set; }
12-
13-
public V1ObjectMeta Metadata { get; set; } = new();
14-
15-
public V1TestEntitySpec Spec { get; set; } = new();
16-
17-
public V1TestEntityStatus Status { get; set; } = new();
18-
1910
public override string ToString() => $"Test Entity ({Metadata.Name}): {Spec.Username} ({Spec.Email})";
2011
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using KubeOps.Abstractions.Finalizer;
2+
3+
using Operator.Entities;
4+
5+
namespace Operator.Finalizer;
6+
7+
public class FinalizerOne : IEntityFinalizer<V1TestEntity>
8+
{
9+
public Task FinalizeAsync(V1TestEntity entity)
10+
{
11+
return Task.CompletedTask;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using KubeOps.Abstractions.Finalizer;
2+
3+
using Operator.Entities;
4+
5+
namespace Operator.Finalizer;
6+
7+
public class FinalizerTwo : IEntityFinalizer<V1TestEntity>
8+
{
9+
public Task FinalizeAsync(V1TestEntity entity)
10+
{
11+
return Task.CompletedTask;
12+
}
13+
}

examples/Operator/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
builder.Services
1111
.AddKubernetesOperator()
12-
.RegisterResources();
12+
.RegisterComponents();
1313

1414
using var host = builder.Build();
1515
await host.RunAsync();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"Operator": {
5+
"commandName": "Project"
6+
}
7+
}
8+
}

examples/Operator/todos.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
todo:
2-
- finalizer
32
- events
43
- leadership election
54
- requeue
65
- build targets
76
- other CLI commands
7+
- error handling
88
- web: webhooks
9+
- docs
910
- cache?
1011
- try .net 8 AOT?
12+
- client:
13+
// TODO: make all sync calls as well.
14+
// TODO: test list / get call.
15+
// TODO: update list call

renovate.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"extends": ["github>buehler/renovate-config", ":disableMajorUpdates"],
44
"ignorePaths": ["_old/"],
55
"dotnet": {
6-
"ignoreDeps": ["StyleCop.Analyzers", "KubeOps.Cli"]
6+
"ignoreDeps": ["StyleCop.Analyzers"]
77
}
88
}

0 commit comments

Comments
 (0)