Repository navigation
fix(reconciler): prevent unnecessary updates when auto-attaching finalizers #1057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kimpenhaus
merged 15 commits into
dotnet:main
from
kimpenhaus:bugfix/409-no-finalizers-auto-attach
Feb 25, 2026
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
33ff70d
fix: optimize finalizer attachment and test status change in integrat…
kimpenhaus 1784e21
Merge branch 'main' into bugfix/409-no-finalizers-auto-attach
kimpenhaus 54c2668
revert: checking for any finalizer - as this is covered by kubernetes
kimpenhaus 74f73fb
fix: whitspacing/line breaks
kimpenhaus 2358728
fix: improve formatting of finalizer service check in Reconciler
kimpenhaus 4bfd4f5
fix: add missing newline at end of Reconciler.cs file
kimpenhaus 3332039
Update Reconciler.cs
kimpenhaus fd2646e
Merge branch 'main' into bugfix/409-no-finalizers-auto-attach
kimpenhaus 13d1a63
fix(reconciler): update finalizer attachment logic to prevent unneces…
kimpenhaus f03b8a5
Merge branch 'dotnet:main' into bugfix/409-no-finalizers-auto-attach
kimpenhaus 31de306
test(finalizer): add integration test for auto-attach logic
kimpenhaus 7d2fa88
Merge branch 'main' into bugfix/409-no-finalizers-auto-attach
kimpenhaus e7d20e8
Merge branch 'main' into bugfix/409-no-finalizers-auto-attach
kimpenhaus f5091c9
Update test/KubeOps.Operator.Test/Finalizer/EntityFinalizer.AutoAttac…
kimpenhaus 5cd6e2e
test(finalizer): verify status is set to 'reconciled'
kimpenhaus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
120 changes: 120 additions & 0 deletions
120
test/KubeOps.Operator.Test/Finalizer/EntityFinalizer.AutoAttachIntegration.Test.cs
This file contains hidden or 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,120 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using FluentAssertions; | ||
|
|
||
| using KubeOps.Abstractions.Reconciliation; | ||
| using KubeOps.Abstractions.Reconciliation.Controller; | ||
| using KubeOps.Abstractions.Reconciliation.Finalizer; | ||
| using KubeOps.KubernetesClient; | ||
| using KubeOps.Operator.Test.TestEntities; | ||
|
|
||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| namespace KubeOps.Operator.Test.Finalizer; | ||
|
|
||
| public sealed class EntityFinalizerAutoAttachIntegrationTest : IntegrationTestBase | ||
| { | ||
| private readonly InvocationCounter<V1OperatorIntegrationTestEntity> _mock = new(); | ||
| private readonly IKubernetesClient _client = new KubernetesClient.KubernetesClient(); | ||
| private readonly TestNamespaceProvider _ns = new(); | ||
|
|
||
| [Fact] | ||
| public async Task Should_Attach_Finalizer_On_Entity_And_Call_Reconcile_Once() | ||
| { | ||
| var watcherCounter = new InvocationCounter<V1OperatorIntegrationTestEntity> { TargetInvocationCount = 3 }; | ||
| using var watcher = | ||
| _client.Watch<V1OperatorIntegrationTestEntity>( | ||
| (_, e) => watcherCounter.Invocation(e), | ||
| @namespace: _ns.Namespace, | ||
| cancellationToken: TestContext.Current.CancellationToken); | ||
|
|
||
| await _client.CreateAsync( | ||
| new V1OperatorIntegrationTestEntity("first", "first", _ns.Namespace, couldChangeStatus: true), | ||
| TestContext.Current.CancellationToken); | ||
| await _mock.WaitForInvocations; | ||
| await watcherCounter.WaitForInvocations; | ||
|
|
||
| var result = await _client.GetAsync<V1OperatorIntegrationTestEntity>( | ||
| "first", | ||
| _ns.Namespace, | ||
| TestContext.Current.CancellationToken); | ||
|
|
||
| result.Should().NotBeNull(); | ||
| result.Metadata.Finalizers.Should().Contain("operator.test/testfinalizer"); | ||
| result.Status.Status.Should().Be("reconciled"); | ||
| } | ||
|
|
||
| public override async ValueTask InitializeAsync() | ||
| { | ||
| await base.InitializeAsync(); | ||
| await _ns.InitializeAsync(); | ||
| } | ||
|
|
||
| public override async ValueTask DisposeAsync() | ||
| { | ||
| await base.DisposeAsync(); | ||
| var entities = await _client.ListAsync<V1OperatorIntegrationTestEntity>(_ns.Namespace); | ||
| foreach (var e in entities) | ||
| { | ||
| if (e.Metadata.Finalizers is null) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| e.Metadata.Finalizers.Clear(); | ||
| await _client.UpdateAsync(e); | ||
| } | ||
|
|
||
| await _ns.DisposeAsync(); | ||
| _client.Dispose(); | ||
| } | ||
|
|
||
| protected override void ConfigureHost(HostApplicationBuilder builder) | ||
| { | ||
| builder.Services | ||
| .AddSingleton(_mock) | ||
| .AddKubernetesOperator(s => { s.Namespace = _ns.Namespace; }) | ||
| .AddController<TestController, V1OperatorIntegrationTestEntity>() | ||
| .AddFinalizer<TestFinalizer, V1OperatorIntegrationTestEntity>("operator.test/testfinalizer"); | ||
| } | ||
|
|
||
| private class TestController( | ||
| InvocationCounter<V1OperatorIntegrationTestEntity> svc, | ||
| IKubernetesClient client) | ||
| : IEntityController<V1OperatorIntegrationTestEntity> | ||
| { | ||
| public async Task<ReconciliationResult<V1OperatorIntegrationTestEntity>> ReconcileAsync(V1OperatorIntegrationTestEntity entity, CancellationToken cancellationToken) | ||
| { | ||
| if (entity.Spec.CouldChangeStatus) | ||
| { | ||
| // status change: issue 1001 (https://github.com/dotnet/dotnet-operator-sdk/issues/1001) | ||
| entity.Status.Status = "reconciled"; | ||
| entity = await client.UpdateStatusAsync(entity, cancellationToken); | ||
| } | ||
|
|
||
| svc.Invocation(entity); | ||
|
|
||
| return ReconciliationResult<V1OperatorIntegrationTestEntity>.Success(entity); | ||
| } | ||
|
|
||
| public Task<ReconciliationResult<V1OperatorIntegrationTestEntity>> DeletedAsync(V1OperatorIntegrationTestEntity entity, CancellationToken cancellationToken) | ||
| { | ||
| svc.Invocation(entity); | ||
| return Task.FromResult(ReconciliationResult<V1OperatorIntegrationTestEntity>.Success(entity)); | ||
| } | ||
| } | ||
|
|
||
| private class TestFinalizer( | ||
| InvocationCounter<V1OperatorIntegrationTestEntity> svc) | ||
| : IEntityFinalizer<V1OperatorIntegrationTestEntity> | ||
| { | ||
| public Task<ReconciliationResult<V1OperatorIntegrationTestEntity>> FinalizeAsync(V1OperatorIntegrationTestEntity entity, CancellationToken cancellationToken) | ||
| { | ||
| svc.Invocation(entity); | ||
| return Task.FromResult(ReconciliationResult<V1OperatorIntegrationTestEntity>.Success(entity)); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.