-
Notifications
You must be signed in to change notification settings - Fork 237
fix: deletion not reconciled when merged into eventFilteringUpdate response #3394
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
Open
xstefank
wants to merge
1
commit into
operator-framework:main
Choose a base branch
from
xstefank:resource-delete-informer-miss
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+261
−1
Open
Changes from all commits
Commits
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
28 changes: 28 additions & 0 deletions
28
...operator/baseapi/deletionduringstatusupdate/DeletionDuringStatusUpdateCustomResource.java
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,28 @@ | ||
| /* | ||
| * Copyright Java Operator SDK Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.javaoperatorsdk.operator.baseapi.deletionduringstatusupdate; | ||
|
|
||
| import io.fabric8.kubernetes.api.model.Namespaced; | ||
| import io.fabric8.kubernetes.client.CustomResource; | ||
| import io.fabric8.kubernetes.model.annotation.Group; | ||
| import io.fabric8.kubernetes.model.annotation.ShortNames; | ||
| import io.fabric8.kubernetes.model.annotation.Version; | ||
|
|
||
| @Group("sample.javaoperatorsdk") | ||
| @Version("v1") | ||
| @ShortNames("ddsu") | ||
| public class DeletionDuringStatusUpdateCustomResource | ||
| extends CustomResource<Void, DeletionDuringStatusUpdateStatus> implements Namespaced {} |
107 changes: 107 additions & 0 deletions
107
...operatorsdk/operator/baseapi/deletionduringstatusupdate/DeletionDuringStatusUpdateIT.java
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,107 @@ | ||
| /* | ||
| * Copyright Java Operator SDK Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.javaoperatorsdk.operator.baseapi.deletionduringstatusupdate; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.Collections; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
| import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.awaitility.Awaitility.await; | ||
|
|
||
| /** | ||
| * Regression test for: deletion event dropped when resource is deleted concurrently with a status | ||
| * update. | ||
| */ | ||
| class DeletionDuringStatusUpdateIT { | ||
|
|
||
| static final String RESOURCE_NAME = "test-resource"; | ||
|
|
||
| @RegisterExtension | ||
| LocallyRunOperatorExtension extension = | ||
| LocallyRunOperatorExtension.builder() | ||
| .withReconciler(new DeletionDuringStatusUpdateReconciler()) | ||
| .build(); | ||
|
|
||
| @AfterEach | ||
| void forceCleanup() { | ||
| // If the test failed, remove the finalizer so the resource can be deleted | ||
| var res = extension.get(DeletionDuringStatusUpdateCustomResource.class, RESOURCE_NAME); | ||
| if (res != null) { | ||
| res.getMetadata().setFinalizers(Collections.emptyList()); | ||
| extension.replace(res); | ||
| extension.delete(res); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void deletionDuringStatusUpdateTriggersCleanup() throws InterruptedException { | ||
| var reconciler = extension.getReconcilerOfType(DeletionDuringStatusUpdateReconciler.class); | ||
|
|
||
| extension.create(testResource()); | ||
|
|
||
| // Wait until the reconciler is inside the update operation (active-update window is open) | ||
| assertThat(reconciler.patchStartedLatch.await(30, TimeUnit.SECONDS)) | ||
| .as("reconciler should enter the patch update operation") | ||
| .isTrue(); | ||
|
|
||
| // Issue delete — K8s sets deletionTimestamp while the active-update window is open | ||
| extension.delete(testResource()); | ||
|
|
||
| // Wait for deletionTimestamp to be confirmed on the resource in K8s | ||
| await() | ||
| .atMost(Duration.ofSeconds(30)) | ||
| .until( | ||
| () -> { | ||
| var res = | ||
| extension.get(DeletionDuringStatusUpdateCustomResource.class, RESOURCE_NAME); | ||
| return res != null && res.isMarkedForDeletion(); | ||
| }); | ||
|
|
||
| // Signal the reconciler to proceed with the actual PATCH. K8s will merge deletionTimestamp | ||
| // into the response - the deletion event (lower RV) is now deferred and will be dropped | ||
| // without the fix. | ||
| reconciler.deleteConfirmedLatch.countDown(); | ||
|
|
||
| // cleanup() must be called — the deletion must not be silently lost | ||
| assertThat(reconciler.cleanupCalledLatch.await(30, TimeUnit.SECONDS)) | ||
| .as("cleanup() must be called after the status update that races with the delete") | ||
| .isTrue(); | ||
|
|
||
| // Resource must eventually disappear (finalizer removed) | ||
| await() | ||
| .atMost(Duration.ofSeconds(30)) | ||
| .untilAsserted( | ||
| () -> | ||
| assertThat( | ||
| extension.get( | ||
| DeletionDuringStatusUpdateCustomResource.class, RESOURCE_NAME)) | ||
| .isNull()); | ||
| } | ||
|
|
||
| DeletionDuringStatusUpdateCustomResource testResource() { | ||
| var resource = new DeletionDuringStatusUpdateCustomResource(); | ||
| resource.setMetadata(new ObjectMetaBuilder().withName(RESOURCE_NAME).build()); | ||
| return resource; | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
...sdk/operator/baseapi/deletionduringstatusupdate/DeletionDuringStatusUpdateReconciler.java
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,79 @@ | ||
| /* | ||
| * Copyright Java Operator SDK Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.javaoperatorsdk.operator.baseapi.deletionduringstatusupdate; | ||
|
|
||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import io.javaoperatorsdk.operator.api.reconciler.Cleaner; | ||
| import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
| import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration; | ||
| import io.javaoperatorsdk.operator.api.reconciler.DeleteControl; | ||
| import io.javaoperatorsdk.operator.api.reconciler.Reconciler; | ||
| import io.javaoperatorsdk.operator.api.reconciler.UpdateControl; | ||
|
|
||
| @ControllerConfiguration | ||
| public class DeletionDuringStatusUpdateReconciler | ||
| implements Reconciler<DeletionDuringStatusUpdateCustomResource>, | ||
| Cleaner<DeletionDuringStatusUpdateCustomResource> { | ||
|
|
||
| final CountDownLatch patchStartedLatch = new CountDownLatch(1); | ||
| final CountDownLatch deleteConfirmedLatch = new CountDownLatch(1); | ||
| final CountDownLatch cleanupCalledLatch = new CountDownLatch(1); | ||
|
|
||
| @Override | ||
| public UpdateControl<DeletionDuringStatusUpdateCustomResource> reconcile( | ||
| DeletionDuringStatusUpdateCustomResource resource, | ||
| Context<DeletionDuringStatusUpdateCustomResource> context) | ||
| throws InterruptedException { | ||
| if (resource.isMarkedForDeletion()) { | ||
| return UpdateControl.noUpdate(); | ||
| } | ||
|
|
||
| var status = new DeletionDuringStatusUpdateStatus(); | ||
| status.setReady(true); | ||
| resource.setStatus(status); | ||
|
|
||
| context | ||
| .resourceOperations() | ||
| .resourcePatch( | ||
| resource, | ||
| r -> { | ||
| patchStartedLatch.countDown(); | ||
| try { | ||
| if (!deleteConfirmedLatch.await(30, TimeUnit.SECONDS)) { | ||
| throw new RuntimeException("Timed out waiting for delete confirmation"); | ||
| } | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new RuntimeException(e); | ||
| } | ||
| r.getMetadata().setResourceVersion(null); | ||
| return context.getClient().resource(r).patchStatus(); | ||
| }, | ||
| context.eventSourceRetriever().getControllerEventSource()); | ||
|
|
||
| return UpdateControl.noUpdate(); | ||
| } | ||
|
|
||
| @Override | ||
| public DeleteControl cleanup( | ||
| DeletionDuringStatusUpdateCustomResource resource, | ||
| Context<DeletionDuringStatusUpdateCustomResource> context) { | ||
| cleanupCalledLatch.countDown(); | ||
| return DeleteControl.defaultDelete(); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
...atorsdk/operator/baseapi/deletionduringstatusupdate/DeletionDuringStatusUpdateStatus.java
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,29 @@ | ||
| /* | ||
| * Copyright Java Operator SDK Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.javaoperatorsdk.operator.baseapi.deletionduringstatusupdate; | ||
|
|
||
| public class DeletionDuringStatusUpdateStatus { | ||
|
|
||
| private boolean ready; | ||
|
|
||
| public boolean isReady() { | ||
| return ready; | ||
| } | ||
|
|
||
| public void setReady(boolean ready) { | ||
| this.ready = ready; | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be triggered with
ResourceAction.DELETEDinstead?